Skip to content

Commit 8829d3f

Browse files
committed
Set useBuiltIns to false by default and allow to set corejs version
1 parent 0facc4d commit 8829d3f

File tree

6 files changed

+439
-280
lines changed

6 files changed

+439
-280
lines changed

index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,11 @@ class Encore {
799799
* // automatically import polyfills where they
800800
* // are needed
801801
* useBuiltIns: 'usage'
802+
*
803+
* // if you set useBuiltIns you also have to add
804+
* // core-js to your project using Yarn or npm and
805+
* // inform Babel of the version it will use.
806+
* corejs: 3
802807
* });
803808
*
804809
* Supported options:
@@ -812,14 +817,18 @@ class Encore {
812817
* If set that option will include the given Node modules to
813818
* the files that are processed by Babel. Cannot be used if
814819
* the "exclude" option is also set.
815-
* * {'usage'|'entry'|false} useBuiltIns (default='entry')
820+
* * {'usage'|'entry'|false} useBuiltIns (default=false)
816821
* Set the "useBuiltIns" option of @babel/preset-env that changes
817822
* how it handles polyfills (https://babeljs.io/docs/en/babel-preset-env#usebuiltins)
818823
* Using it with 'entry' will require you to import @babel/polyfill
819824
* once in your whole app and will result in that import being replaced
820825
* by individual polyfills. Using it with 'usage' will try to
821826
* automatically detect which polyfills are needed for each file and
822827
* add them accordingly.
828+
* * {2|3} corejs (default=not set)
829+
* Set the "corejs" option of @babel/preset-env.
830+
* It should contain the version of core-js you added to your project
831+
* if useBuiltIns isn't set to false.
823832
*
824833
* @param {function} callback
825834
* @param {object} encoreOptions

lib/WebpackConfig.js

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

8889
// Features/Loaders options callbacks

lib/loaders/babel.js

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

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
@@ -865,7 +865,7 @@ describe('The config-generator function', () => {
865865

866866
const babelLoader = jsRule.use.find(loader => loader.loader === 'babel-loader');
867867
const babelEnvPreset = babelLoader.options.presets.find(([name]) => name === '@babel/preset-env');
868-
expect(babelEnvPreset[1].useBuiltIns).to.equal('entry');
868+
expect(babelEnvPreset[1].useBuiltIns).to.equal(false);
869869
});
870870

871871
it('with configureBabel() and a different exclude rule', () => {
@@ -906,7 +906,8 @@ describe('The config-generator function', () => {
906906
config.publicPath = '/public-path';
907907
config.addEntry('main', './main');
908908
config.configureBabel(() => { }, {
909-
useBuiltIns: 'usage'
909+
useBuiltIns: 'usage',
910+
corejs: 3,
910911
});
911912

912913
const actualConfig = configGenerator(config);
@@ -915,6 +916,7 @@ describe('The config-generator function', () => {
915916
const babelLoader = jsRule.use.find(loader => loader.loader === 'babel-loader');
916917
const babelEnvPreset = babelLoader.options.presets.find(([name]) => name === '@babel/preset-env');
917918
expect(babelEnvPreset[1].useBuiltIns).to.equal('usage');
919+
expect(babelEnvPreset[1].corejs).to.equal(3);
918920
});
919921
});
920922

0 commit comments

Comments
 (0)