Skip to content

Fix babel config file detection #738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ class WebpackConfig {
}

if (this.doesBabelRcFileExist()) {
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.');
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.');
}
}

Expand All @@ -415,7 +415,7 @@ class WebpackConfig {
}

if (this.doesBabelRcFileExist() && !allowedOptionsWithExternalConfig.includes(normalizedOptionKey)) {
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").`);
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").`);
continue;
}

Expand Down Expand Up @@ -462,7 +462,7 @@ class WebpackConfig {
}

if (this.doesBabelRcFileExist()) {
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").');
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").');
}

this.babelPresetEnvOptionsCallback = callback;
Expand Down
20 changes: 10 additions & 10 deletions lib/config/parse-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,21 @@ module.exports = function(argv, cwd) {

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

return runtimeConfig;
};
24 changes: 24 additions & 0 deletions test/config/parse-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ describe('parse-runtime', () => {
expect(config.context).to.equal('/tmp/custom-context');
});

it('babel config in package.json detected when present', () => {
const projectDir = createTestDirectory();
fs.writeFileSync(
path.join(projectDir, 'package.json'),
'{"babel": {}}'
);

const config = parseArgv(createArgv(['dev']), projectDir);

expect(config.babelRcFileExists).to.be.true;
});

it('.babelrc detected when present', () => {
const projectDir = createTestDirectory();
fs.writeFileSync(
Expand All @@ -108,6 +120,18 @@ describe('parse-runtime', () => {
expect(config.babelRcFileExists).to.be.true;
});

it('babel.config.json detected when present', () => {
const projectDir = createTestDirectory();
fs.writeFileSync(
path.join(projectDir, 'babel.config.json'),
'{}'
);

const config = parseArgv(createArgv(['dev']), projectDir);

expect(config.babelRcFileExists).to.be.true;
});

it('dev-server command hot', () => {
const testDir = createTestDirectory();
const config = parseArgv(createArgv(['dev-server', '--hot']), testDir);
Expand Down