Skip to content
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

Set useBuiltIns to false by default and allow to set core-js version #545

Merged
merged 1 commit into from Mar 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions index.js
Expand Up @@ -780,6 +780,11 @@ class Encore {
* // automatically import polyfills where they
* // are needed
* useBuiltIns: 'usage'
*
* // if you set useBuiltIns you also have to add
* // core-js to your project using Yarn or npm and
* // inform Babel of the version it will use.
* corejs: 3
* });
*
* Supported options:
Expand All @@ -793,14 +798,18 @@ class Encore {
* If set that option will include the given Node modules to
* the files that are processed by Babel. Cannot be used if
* the "exclude" option is also set.
* * {'usage'|'entry'|false} useBuiltIns (default='entry')
* * {'usage'|'entry'|false} useBuiltIns (default=false)
* Set the "useBuiltIns" option of @babel/preset-env that changes
* how it handles polyfills (https://babeljs.io/docs/en/babel-preset-env#usebuiltins)
* Using it with 'entry' will require you to import @babel/polyfill
* Using it with 'entry' will require you to import core-js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't core-js also required when using the option usage?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is required (= you have to yarn add core-js) but you don't need to import it into your files.

* once in your whole app and will result in that import being replaced
* by individual polyfills. Using it with 'usage' will try to
* automatically detect which polyfills are needed for each file and
* add them accordingly.
* * {number|string} corejs (default=not set)
* Set the "corejs" option of @babel/preset-env.
* It should contain the version of core-js you added to your project
* if useBuiltIns isn't set to false.
*
* @param {function} callback
* @param {object} encoreOptions
Expand Down
3 changes: 2 additions & 1 deletion lib/WebpackConfig.js
Expand Up @@ -83,7 +83,8 @@ class WebpackConfig {
};
this.babelOptions = {
exclude: /(node_modules|bower_components)/,
useBuiltIns: 'entry',
useBuiltIns: false,
corejs: null,
};

// Features/Loaders options callbacks
Expand Down
21 changes: 12 additions & 9 deletions lib/loaders/babel.js
Expand Up @@ -31,17 +31,20 @@ module.exports = {
// configure babel (unless the user is specifying .babelrc)
// todo - add a sanity check for their babelrc contents
if (!webpackConfig.doesBabelRcFileExist()) {
const 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
modules: false,
targets: {},
forceAllTransforms: webpackConfig.isProduction(),
useBuiltIns: webpackConfig.babelOptions.useBuiltIns,
corejs: webpackConfig.babelOptions.corejs,
};

Object.assign(babelConfig, {
presets: [
['@babel/preset-env', {
// 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
modules: false,
targets: {},
forceAllTransforms: webpackConfig.isProduction(),
useBuiltIns: webpackConfig.babelOptions.useBuiltIns,
}]
['@babel/preset-env', presetEnvOptions]
],
plugins: ['@babel/plugin-syntax-dynamic-import']
});
Expand Down
5 changes: 2 additions & 3 deletions package.json
Expand Up @@ -25,9 +25,9 @@
},
"homepage": "https://github.com/symfony/webpack-encore",
"dependencies": {
"@babel/core": "^7.0.0",
"@babel/core": "^7.4.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-env": "^7.4.0",
"assets-webpack-plugin": "^3.9.7",
"babel-loader": "^8.0.0",
"chalk": "^2.4.1",
Expand Down Expand Up @@ -57,7 +57,6 @@
"yargs-parser": "^12.0.0"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/plugin-transform-react-jsx": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"autoprefixer": "^8.5.0",
Expand Down
6 changes: 4 additions & 2 deletions test/config-generator.js
Expand Up @@ -856,7 +856,7 @@ describe('The config-generator function', () => {

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

it('with configureBabel() and a different exclude rule', () => {
Expand Down Expand Up @@ -897,7 +897,8 @@ describe('The config-generator function', () => {
config.publicPath = '/public-path';
config.addEntry('main', './main');
config.configureBabel(() => { }, {
useBuiltIns: 'usage'
useBuiltIns: 'usage',
corejs: 3,
});

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

Expand Down