Skip to content

Commit 89929bb

Browse files
committed
feature #642 Allow to configure babel env preset (ihmels)
This PR was merged into the master branch. Discussion ---------- Allow to configure babel env preset This PR allows you to configure `@babel/preset-env` by calling `Encore.configureBabelPresetEnv()` without the need to create a `.babelrc` and lose the ability to activate presets automatically. ```js Encore .configureBabelPresetEnv((config) => { config.corejs = 3; config.useBuiltIns = 'usage'; config.include = ['foo-plugin']; }); ``` The options `corejs` and `useBuiltIns` of `Encore.configureBabel()` should be deprecated. Commits ------- 261aa81 Allow to configure babel env preset
2 parents 0a294c8 + 261aa81 commit 89929bb

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

lib/WebpackConfig.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class WebpackConfig {
140140
this.lessLoaderOptionsCallback = () => {};
141141
this.stylusLoaderOptionsCallback = () => {};
142142
this.babelConfigurationCallback = () => {};
143+
this.babelPresetEnvOptionsCallback = () => {};
143144
this.cssLoaderConfigurationCallback = () => {};
144145
this.splitChunksConfigurationCallback = () => {};
145146
this.watchOptionsConfigurationCallback = () => {};
@@ -404,6 +405,10 @@ class WebpackConfig {
404405
normalizedOptionKey = 'includeNodeModules';
405406
}
406407

408+
if (['useBuiltIns', 'corejs'].includes(optionKey)) {
409+
logger.deprecation(`configureBabel: "${optionKey}" is deprecated. Please use configureBabelPresetEnv() instead.`);
410+
}
411+
407412
if (this.doesBabelRcFileExist() && !allowedOptionsWithExternalConfig.includes(normalizedOptionKey)) {
408413
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").`);
409414
continue;
@@ -446,6 +451,14 @@ class WebpackConfig {
446451
}
447452
}
448453

454+
configureBabelPresetEnv(callback) {
455+
if (typeof callback !== 'function') {
456+
throw new Error('Argument 1 to configureBabelPresetEnv() must be a callback function.');
457+
}
458+
459+
this.babelPresetEnvOptionsCallback = callback;
460+
}
461+
449462
configureCssLoader(callback) {
450463
if (typeof callback !== 'function') {
451464
throw new Error('Argument 1 to configureCssLoader() must be a callback function.');

lib/loaders/babel.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = {
3636
// configure babel (unless the user is specifying .babelrc)
3737
// todo - add a sanity check for their babelrc contents
3838
if (!webpackConfig.doesBabelRcFileExist()) {
39-
const presetEnvOptions = {
39+
let presetEnvOptions = {
4040
// modules don't need to be transformed - webpack will parse
4141
// the modules for us. This is a performance improvement
4242
// https://babeljs.io/docs/en/babel-preset-env#modules
@@ -46,6 +46,11 @@ module.exports = {
4646
corejs: webpackConfig.babelOptions.corejs,
4747
};
4848

49+
presetEnvOptions = applyOptionsCallback(
50+
webpackConfig.babelPresetEnvOptionsCallback,
51+
presetEnvOptions
52+
);
53+
4954
Object.assign(babelConfig, {
5055
presets: [
5156
['@babel/preset-env', presetEnvOptions]

test/loaders/babel.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,24 @@ describe('loaders/babel', () => {
146146
'foo'
147147
]);
148148
});
149+
150+
it('getLoaders() with configured babel env preset', () => {
151+
const config = createConfig();
152+
config.runtimeConfig.babelRcFileExists = false;
153+
154+
config.configureBabel(function(config) {
155+
config.corejs = null;
156+
});
157+
158+
config.configureBabelPresetEnv(function(config) {
159+
config.corejs = 3;
160+
config.include = ['bar'];
161+
});
162+
163+
const actualLoaders = babelLoader.getLoaders(config);
164+
165+
// options are overridden
166+
expect(actualLoaders[0].options.presets[0][1].corejs).to.equal(3);
167+
expect(actualLoaders[0].options.presets[0][1].include).to.have.members(['bar']);
168+
});
149169
});

0 commit comments

Comments
 (0)