Skip to content

Commit 5ce489e

Browse files
jdreesenweaverryan
authored andcommitted
Fix babel config file detection
1 parent 134f47a commit 5ce489e

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

lib/WebpackConfig.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ class WebpackConfig {
396396
}
397397

398398
if (this.doesBabelRcFileExist()) {
399-
logger.warning('The "callback" argument of configureBabel() will not be used because your app already provides an external Babel configuration (a ".babelrc" file, ".babelrc.js" file or "babel" key in "package.json"). Use null as a first argument to remove that warning.');
399+
logger.warning('The "callback" argument of configureBabel() will not be used because your app already provides an external Babel configuration (e.g. a ".babelrc" or "babelrc.config.js" file or "babel" key in "package.json"). Use null as a first argument to remove that warning.');
400400
}
401401
}
402402

@@ -415,7 +415,7 @@ class WebpackConfig {
415415
}
416416

417417
if (this.doesBabelRcFileExist() && !allowedOptionsWithExternalConfig.includes(normalizedOptionKey)) {
418-
logger.warning(`The "${normalizedOptionKey}" option of configureBabel() will not be used because your app already provides an external Babel configuration (a ".babelrc" file, ".babelrc.js" file or "babel" key in "package.json").`);
418+
logger.warning(`The "${normalizedOptionKey}" option of configureBabel() will not be used because your app already provides an external Babel configuration (e.g. a ".babelrc" or "babelrc.config.js" file or "babel" key in "package.json").`);
419419
continue;
420420
}
421421

@@ -462,7 +462,7 @@ class WebpackConfig {
462462
}
463463

464464
if (this.doesBabelRcFileExist()) {
465-
throw new Error('The "callback" argument of configureBabelPresetEnv() will not be used because your app already provides an external Babel configuration (a ".babelrc" file, ".babelrc.js" file or "babel" key in "package.json").');
465+
throw new Error('The "callback" argument of configureBabelPresetEnv() will not be used because your app already provides an external Babel configuration (e.g. a ".babelrc" or "babelrc.config.js" file or "babel" key in "package.json").');
466466
}
467467

468468
this.babelPresetEnvOptionsCallback = callback;

lib/config/parse-runtime.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,21 @@ module.exports = function(argv, cwd) {
8787

8888
const partialConfig = babel.loadPartialConfig({
8989
/*
90-
* This is a small mystery. Even if we set the cwd & root
91-
* options, deep in babel, if the filename option is not
92-
* set, then it doesn't see the "cwd" directory as a valid
93-
* directory where it should look for the .babelrc file.
94-
* The fact that this is set to webpack.config.js is not
95-
* significant at all - you could even invent a filename.
96-
* However, as I'm not sure the side effects (the filename
97-
* option is documented as "for error messages"), we're
98-
* setting it to a realistic filename.
90+
* There are two types of babel configuration:
91+
* - project-wide configuration in babel.config.* files
92+
* - file-relative configuration in .babelrc.* files
93+
* or package.json files with a "babel" key
94+
*
95+
* To detect the file-relative configuration we need
96+
* to set the following values. The filename is needed
97+
* for Babel as an example so that it knows where it
98+
* needs to search the relative config for.
9999
*/
100100
root: cwd,
101101
cwd: cwd,
102102
filename: path.join(cwd, 'webpack.config.js')
103103
});
104-
runtimeConfig.babelRcFileExists = (typeof partialConfig.babelrc === 'string');
104+
runtimeConfig.babelRcFileExists = partialConfig.hasFilesystemConfig();
105105

106106
return runtimeConfig;
107107
};

test/config/parse-runtime.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ describe('parse-runtime', () => {
9696
expect(config.context).to.equal('/tmp/custom-context');
9797
});
9898

99+
it('babel config in package.json detected when present', () => {
100+
const projectDir = createTestDirectory();
101+
fs.writeFileSync(
102+
path.join(projectDir, 'package.json'),
103+
'{"babel": {}}'
104+
);
105+
106+
const config = parseArgv(createArgv(['dev']), projectDir);
107+
108+
expect(config.babelRcFileExists).to.be.true;
109+
});
110+
99111
it('.babelrc detected when present', () => {
100112
const projectDir = createTestDirectory();
101113
fs.writeFileSync(
@@ -108,6 +120,18 @@ describe('parse-runtime', () => {
108120
expect(config.babelRcFileExists).to.be.true;
109121
});
110122

123+
it('babel.config.json detected when present', () => {
124+
const projectDir = createTestDirectory();
125+
fs.writeFileSync(
126+
path.join(projectDir, 'babel.config.json'),
127+
'{}'
128+
);
129+
130+
const config = parseArgv(createArgv(['dev']), projectDir);
131+
132+
expect(config.babelRcFileExists).to.be.true;
133+
});
134+
111135
it('dev-server command hot', () => {
112136
const testDir = createTestDirectory();
113137
const config = parseArgv(createArgv(['dev-server', '--hot']), testDir);

0 commit comments

Comments
 (0)