diff --git a/packages/babel-cli/test/index.js b/packages/babel-cli/test/index.js
index 2bd463282e..50dd88d0cd 100644
--- a/packages/babel-cli/test/index.js
+++ b/packages/babel-cli/test/index.js
@@ -1,4 +1,3 @@
-const includes = require("lodash/includes");
const readdir = require("fs-readdir-recursive");
const helper = require("@babel/helper-fixtures");
const rimraf = require("rimraf");
@@ -51,7 +50,7 @@ const assertTest = function(stdout, stderr, opts) {
if (opts.stderr) {
if (opts.stderrContains) {
- expect(includes(stderr, expectStderr)).toBe(true);
+ expect(stderr).toContain(expectStderr);
} else {
expect(stderr).toBe(expectStderr);
}
@@ -65,7 +64,7 @@ const assertTest = function(stdout, stderr, opts) {
if (opts.stdout) {
if (opts.stdoutContains) {
- expect(includes(stdout, expectStdout)).toBe(true);
+ expect(stdout).toContain(expectStdout);
} else {
expect(stdout).toBe(expectStdout);
}
diff --git a/packages/babel-core/src/config/config-chain.js b/packages/babel-core/src/config/config-chain.js
index e185f60df7..73345d0cfb 100644
--- a/packages/babel-core/src/config/config-chain.js
+++ b/packages/babel-core/src/config/config-chain.js
@@ -46,8 +46,9 @@ export type PresetInstance = {
};
export type ConfigContext = {
- filename: string | null,
+ filename: string | void,
cwd: string,
+ root: string,
envName: string,
};
@@ -130,18 +131,15 @@ export function buildRootChain(
);
if (!programmaticChain) return null;
- const { root: rootDir = ".", configFile: configFileName = true } = opts;
- let { babelrc, babelrcRoots } = opts;
-
- const absoluteRoot = path.resolve(context.cwd, rootDir);
-
let configFile;
- if (typeof configFileName === "string") {
- configFile = loadConfig(configFileName, context.cwd, context.envName);
- } else if (configFileName === true) {
- configFile = findRootConfig(absoluteRoot, context.envName);
+ if (typeof opts.configFile === "string") {
+ configFile = loadConfig(opts.configFile, context.cwd, context.envName);
+ } else if (opts.configFile !== false) {
+ configFile = findRootConfig(context.root, context.envName);
}
+ let { babelrc, babelrcRoots } = opts;
+
const configFileChain = emptyChain();
if (configFile) {
const validatedFile = validateConfigFile(configFile);
@@ -171,7 +169,7 @@ export function buildRootChain(
if (
(babelrc === true || babelrc === undefined) &&
pkgData &&
- babelrcLoadEnabled(context, pkgData, babelrcRoots, absoluteRoot)
+ babelrcLoadEnabled(context, pkgData, babelrcRoots)
) {
({ ignore: ignoreFile, config: babelrcFile } = findRelativeConfig(
pkgData,
@@ -214,10 +212,11 @@ function babelrcLoadEnabled(
context: ConfigContext,
pkgData: FilePackageData,
babelrcRoots: BabelrcSearch | void,
- absoluteRoot: string,
): boolean {
if (typeof babelrcRoots === "boolean") return babelrcRoots;
+ const absoluteRoot = context.root;
+
// Fast path to avoid having to load micromatch if the babelrc is just
// loading in the standard root directory.
if (babelrcRoots === undefined) {
@@ -576,7 +575,7 @@ function configFieldIsApplicable(
test: ConfigApplicableTest,
dirname: string,
): boolean {
- if (context.filename === null) {
+ if (typeof context.filename !== "string") {
throw new Error(
`Configuration contains explicit test/include/exclude checks, but no filename was passed to Babel`,
);
@@ -602,7 +601,7 @@ function shouldIgnore(
dirname: string,
): boolean {
if (ignore) {
- if (context.filename === null) {
+ if (typeof context.filename !== "string") {
throw new Error(
`Configuration contains ignore checks, but no filename was passed to Babel`,
);
@@ -621,7 +620,7 @@ function shouldIgnore(
}
if (only) {
- if (context.filename === null) {
+ if (typeof context.filename !== "string") {
throw new Error(
`Configuration contains ignore checks, but no filename was passed to Babel`,
);
@@ -696,7 +695,7 @@ function matchesPatterns(
const getPossibleDirs = makeWeakCache((context: ConfigContextNamed) => {
let current = context.filename;
- if (current === null) return [];
+ if (typeof current !== "string") return [];
const possibleDirs = [current];
while (true) {
diff --git a/packages/babel-core/src/config/partial.js b/packages/babel-core/src/config/partial.js
index c38ea87c08..0aae841ffd 100644
--- a/packages/babel-core/src/config/partial.js
+++ b/packages/babel-core/src/config/partial.js
@@ -28,12 +28,17 @@ export default function loadPrivatePartialConfig(
const args = inputOpts ? validate("arguments", inputOpts) : {};
- const { envName = getEnv(), cwd = "." } = args;
+ const { envName = getEnv(), cwd = ".", root: rootDir = "." } = args;
const absoluteCwd = path.resolve(cwd);
+ const absoluteRootDir = path.resolve(absoluteCwd, rootDir);
const context: ConfigContext = {
- filename: args.filename ? path.resolve(cwd, args.filename) : null,
+ filename:
+ typeof args.filename === "string"
+ ? path.resolve(cwd, args.filename)
+ : undefined,
cwd: absoluteCwd,
+ root: absoluteRootDir,
envName,
};
@@ -50,9 +55,14 @@ export default function loadPrivatePartialConfig(
// to not change behavior.
options.babelrc = false;
options.configFile = false;
- options.envName = envName;
- options.cwd = absoluteCwd;
options.passPerPreset = false;
+ options.envName = context.envName;
+ options.cwd = context.cwd;
+ options.root = context.root;
+ options.filename =
+ typeof context.filename === "string"
+ ? path.relative(context.cwd, context.filename)
+ : undefined;
options.plugins = configChain.plugins.map(descriptor =>
createItemFromDescriptor(descriptor),
diff --git a/packages/babel-core/src/transformation/plugin-pass.js b/packages/babel-core/src/transformation/plugin-pass.js
index bd44495cfe..a30803bde0 100644
--- a/packages/babel-core/src/transformation/plugin-pass.js
+++ b/packages/babel-core/src/transformation/plugin-pass.js
@@ -7,14 +7,19 @@ export default class PluginPass {
key: ?string;
file: File;
opts: Object;
+
+ // The working directory that Babel's options are loaded relative to.
+ cwd: string;
+
+ // The path of the file being compiled, relative to the working directory.
filename: string | void;
constructor(file: File, key: ?string, options: ?Object) {
this.key = key;
this.file = file;
this.opts = options || {};
- this.filename =
- typeof file.opts.filename === "string" ? file.opts.filename : undefined;
+ this.cwd = file.opts.cwd;
+ this.filename = file.opts.filename;
}
set(key: mixed, val: mixed) {
diff --git a/packages/babel-core/test/config-chain.js b/packages/babel-core/test/config-chain.js
index 63f22da52b..2d4fcedfd2 100644
--- a/packages/babel-core/test/config-chain.js
+++ b/packages/babel-core/test/config-chain.js
@@ -906,6 +906,7 @@ describe("buildConfigChain", function() {
babelrc: false,
configFile: false,
cwd: process.cwd(),
+ root: process.cwd(),
envName: "development",
passPerPreset: false,
plugins: [],
@@ -935,8 +936,9 @@ describe("buildConfigChain", function() {
}),
).toEqual({
...getDefaults(),
- filename,
+ filename: path.basename(filename),
cwd: path.dirname(filename),
+ root: path.dirname(filename),
comments: true,
});
});
@@ -946,8 +948,9 @@ describe("buildConfigChain", function() {
expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({
...getDefaults(),
- filename,
+ filename: path.basename(filename),
cwd: path.dirname(filename),
+ root: path.dirname(filename),
comments: true,
});
});
@@ -957,8 +960,9 @@ describe("buildConfigChain", function() {
expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({
...getDefaults(),
- filename,
+ filename: path.basename(filename),
cwd: path.dirname(filename),
+ root: path.dirname(filename),
comments: true,
});
});
@@ -998,8 +1002,9 @@ describe("buildConfigChain", function() {
expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({
...getDefaults(),
- filename,
+ filename: path.basename(filename),
cwd: path.dirname(filename),
+ root: path.dirname(filename),
comments: true,
});
});
diff --git a/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/exec.js b/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/exec.js
index e44c3e5e7e..397fa2915c 100644
--- a/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/exec.js
+++ b/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/exec.js
@@ -1,10 +1,10 @@
var actual = transform(
'var x = ',
- Object.assign({}, opts, { filename: '/fake/path/mock.js' })
+ Object.assign({}, opts, { filename: 'fake/path/mock.js' })
).code;
var expected = multiline([
- 'var _jsxFileName = "/fake/path/mock.js";',
+ 'var _jsxFileName = "fake/path/mock.js";',
'var x = ',
- Object.assign({}, opts, { filename: '/fake/path/mock.js' })
+ Object.assign({}, opts, { filename: 'fake/path/mock.js' })
).code;
const expected = multiline([
- 'var _jsxFileName = "/fake/path/mock.js";',
+ 'var _jsxFileName = "fake/path/mock.js";',
'React.createElement(Foo, {',
' bar: "baz",',
' __source: {',