Skip to content

Allow to configure babel env preset #642

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 1 commit into from
Oct 7, 2019
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
13 changes: 13 additions & 0 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class WebpackConfig {
this.lessLoaderOptionsCallback = () => {};
this.stylusLoaderOptionsCallback = () => {};
this.babelConfigurationCallback = () => {};
this.babelPresetEnvOptionsCallback = () => {};
this.cssLoaderConfigurationCallback = () => {};
this.splitChunksConfigurationCallback = () => {};
this.watchOptionsConfigurationCallback = () => {};
Expand Down Expand Up @@ -404,6 +405,10 @@ class WebpackConfig {
normalizedOptionKey = 'includeNodeModules';
}

if (['useBuiltIns', 'corejs'].includes(optionKey)) {
logger.deprecation(`configureBabel: "${optionKey}" is deprecated. Please use configureBabelPresetEnv() instead.`);
}

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").`);
continue;
Expand Down Expand Up @@ -446,6 +451,14 @@ class WebpackConfig {
}
}

configureBabelPresetEnv(callback) {
if (typeof callback !== 'function') {
throw new Error('Argument 1 to configureBabelPresetEnv() must be a callback function.');
}

this.babelPresetEnvOptionsCallback = callback;
}

configureCssLoader(callback) {
if (typeof callback !== 'function') {
throw new Error('Argument 1 to configureCssLoader() must be a callback function.');
Expand Down
7 changes: 6 additions & 1 deletion lib/loaders/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = {
// configure babel (unless the user is specifying .babelrc)
// todo - add a sanity check for their babelrc contents
if (!webpackConfig.doesBabelRcFileExist()) {
const presetEnvOptions = {
let presetEnvOptions = {
// modules don't need to be transformed - webpack will parse
// the modules for us. This is a performance improvement
// https://babeljs.io/docs/en/babel-preset-env#modules
Expand All @@ -46,6 +46,11 @@ module.exports = {
corejs: webpackConfig.babelOptions.corejs,
};

presetEnvOptions = applyOptionsCallback(
webpackConfig.babelPresetEnvOptionsCallback,
presetEnvOptions
);

Object.assign(babelConfig, {
presets: [
['@babel/preset-env', presetEnvOptions]
Expand Down
20 changes: 20 additions & 0 deletions test/loaders/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,24 @@ describe('loaders/babel', () => {
'foo'
]);
});

it('getLoaders() with configured babel env preset', () => {
const config = createConfig();
config.runtimeConfig.babelRcFileExists = false;

config.configureBabel(function(config) {
config.corejs = null;
});

config.configureBabelPresetEnv(function(config) {
config.corejs = 3;
config.include = ['bar'];
});

const actualLoaders = babelLoader.getLoaders(config);

// options are overridden
expect(actualLoaders[0].options.presets[0][1].corejs).to.equal(3);
expect(actualLoaders[0].options.presets[0][1].include).to.have.members(['bar']);
});
});