Skip to content

Commit 85896ef

Browse files
committed
Set useBuiltIns to false by default and allow to set corejs version
1 parent 29db4b1 commit 85896ef

File tree

6 files changed

+440
-281
lines changed

6 files changed

+440
-281
lines changed

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,11 @@ class Encore {
780780
* // automatically import polyfills where they
781781
* // are needed
782782
* useBuiltIns: 'usage'
783+
*
784+
* // if you set useBuiltIns you also have to add
785+
* // core-js to your project using Yarn or npm and
786+
* // inform Babel of the version it will use.
787+
* corejs: 3
783788
* });
784789
*
785790
* Supported options:
@@ -793,14 +798,18 @@ class Encore {
793798
* If set that option will include the given Node modules to
794799
* the files that are processed by Babel. Cannot be used if
795800
* the "exclude" option is also set.
796-
* * {'usage'|'entry'|false} useBuiltIns (default='entry')
801+
* * {'usage'|'entry'|false} useBuiltIns (default=false)
797802
* Set the "useBuiltIns" option of @babel/preset-env that changes
798803
* how it handles polyfills (https://babeljs.io/docs/en/babel-preset-env#usebuiltins)
799-
* Using it with 'entry' will require you to import @babel/polyfill
804+
* Using it with 'entry' will require you to import core-js
800805
* once in your whole app and will result in that import being replaced
801806
* by individual polyfills. Using it with 'usage' will try to
802807
* automatically detect which polyfills are needed for each file and
803808
* add them accordingly.
809+
* * {number|string} corejs (default=not set)
810+
* Set the "corejs" option of @babel/preset-env.
811+
* It should contain the version of core-js you added to your project
812+
* if useBuiltIns isn't set to false.
804813
*
805814
* @param {function} callback
806815
* @param {object} encoreOptions

lib/WebpackConfig.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ class WebpackConfig {
8383
};
8484
this.babelOptions = {
8585
exclude: /(node_modules|bower_components)/,
86-
useBuiltIns: 'entry',
86+
useBuiltIns: false,
87+
corejs: null,
8788
};
8889

8990
// Features/Loaders options callbacks

lib/loaders/babel.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,20 @@ module.exports = {
3131
// configure babel (unless the user is specifying .babelrc)
3232
// todo - add a sanity check for their babelrc contents
3333
if (!webpackConfig.doesBabelRcFileExist()) {
34+
const presetEnvOptions = {
35+
// modules don't need to be transformed - webpack will parse
36+
// the modules for us. This is a performance improvement
37+
// https://babeljs.io/docs/en/babel-preset-env#modules
38+
modules: false,
39+
targets: {},
40+
forceAllTransforms: webpackConfig.isProduction(),
41+
useBuiltIns: webpackConfig.babelOptions.useBuiltIns,
42+
corejs: webpackConfig.babelOptions.corejs,
43+
};
44+
3445
Object.assign(babelConfig, {
3546
presets: [
36-
['@babel/preset-env', {
37-
// modules don't need to be transformed - webpack will parse
38-
// the modules for us. This is a performance improvement
39-
// https://babeljs.io/docs/en/babel-preset-env#modules
40-
modules: false,
41-
targets: {},
42-
forceAllTransforms: webpackConfig.isProduction(),
43-
useBuiltIns: webpackConfig.babelOptions.useBuiltIns,
44-
}]
47+
['@babel/preset-env', presetEnvOptions]
4548
],
4649
plugins: ['@babel/plugin-syntax-dynamic-import']
4750
});

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
},
2626
"homepage": "https://github.com/symfony/webpack-encore",
2727
"dependencies": {
28-
"@babel/core": "^7.0.0",
28+
"@babel/core": "^7.4.0",
2929
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
30-
"@babel/preset-env": "^7.0.0",
30+
"@babel/preset-env": "^7.4.0",
3131
"assets-webpack-plugin": "^3.9.7",
3232
"babel-loader": "^8.0.0",
3333
"chalk": "^2.4.1",
@@ -57,7 +57,6 @@
5757
"yargs-parser": "^12.0.0"
5858
},
5959
"devDependencies": {
60-
"@babel/core": "^7.0.0",
6160
"@babel/plugin-transform-react-jsx": "^7.0.0",
6261
"@babel/preset-react": "^7.0.0",
6362
"autoprefixer": "^8.5.0",

test/config-generator.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ describe('The config-generator function', () => {
856856

857857
const babelLoader = jsRule.use.find(loader => loader.loader === 'babel-loader');
858858
const babelEnvPreset = babelLoader.options.presets.find(([name]) => name === '@babel/preset-env');
859-
expect(babelEnvPreset[1].useBuiltIns).to.equal('entry');
859+
expect(babelEnvPreset[1].useBuiltIns).to.equal(false);
860860
});
861861

862862
it('with configureBabel() and a different exclude rule', () => {
@@ -897,7 +897,8 @@ describe('The config-generator function', () => {
897897
config.publicPath = '/public-path';
898898
config.addEntry('main', './main');
899899
config.configureBabel(() => { }, {
900-
useBuiltIns: 'usage'
900+
useBuiltIns: 'usage',
901+
corejs: 3,
901902
});
902903

903904
const actualConfig = configGenerator(config);
@@ -906,6 +907,7 @@ describe('The config-generator function', () => {
906907
const babelLoader = jsRule.use.find(loader => loader.loader === 'babel-loader');
907908
const babelEnvPreset = babelLoader.options.presets.find(([name]) => name === '@babel/preset-env');
908909
expect(babelEnvPreset[1].useBuiltIns).to.equal('usage');
910+
expect(babelEnvPreset[1].corejs).to.equal(3);
909911
});
910912
});
911913

0 commit comments

Comments
 (0)