Skip to content

Commit

Permalink
feature #612 Don't make @babel/preset-env force all transforms in pro…
Browse files Browse the repository at this point in the history
…d (Lyrkan)

This PR was merged into the master branch.

Discussion
----------

Don't make @babel/preset-env force all transforms in prod

This PR removes the `forceAllTransforms: webpackConfig.isProduction()` line that is currently added to `@babel/preset-env`'s options.

That setting was previously needed because UglifyJS did not support ES6 which meant that if you, for instance, had `async` functions in your code and targeted a recent browsers, they would have to be transformed for the minification process to be successful.

Nowadays we're using Terser which supports ES6, so there shouldn't be any issue keeping these kind of things in the final code anymore.

Commits
-------

b2809f5 Don't make @babel/preset-env force all transforms in prod
  • Loading branch information
weaverryan committed Aug 9, 2019
2 parents 54cb1b8 + b2809f5 commit 5414a1d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
7 changes: 7 additions & 0 deletions fixtures/js/async_function.js
@@ -0,0 +1,7 @@
async function foo() {
console.log('foo');
}

foo().then(() => {
console.log('bar');
});
1 change: 0 additions & 1 deletion lib/loaders/babel.js
Expand Up @@ -42,7 +42,6 @@ module.exports = {
// https://babeljs.io/docs/en/babel-preset-env#modules
modules: false,
targets: {},
forceAllTransforms: webpackConfig.isProduction(),
useBuiltIns: webpackConfig.babelOptions.useBuiltIns,
corejs: webpackConfig.babelOptions.corejs,
};
Expand Down
35 changes: 35 additions & 0 deletions test/functional.js
Expand Up @@ -1157,6 +1157,41 @@ module.exports = {
});
});

it('Babel does not force transforms if they are not needed', (done) => {
const cwd = process.cwd();
after(() => {
process.chdir(cwd);
});

const appDir = testSetup.createTestAppDir();
process.chdir(appDir);

fs.writeFileSync(
path.join(appDir, 'package.json'),

// Chrome 55 supports async and arrow functions
'{"browserslist": "Chrome 55"}'
);

const config = createWebpackConfig('www/build', 'prod');
config.setPublicPath('/build');
config.addEntry('async', './js/async_function.js');
config.configureBabel(null, {
useBuiltIns: 'usage',
corejs: 3,
});

testSetup.runWebpack(config, async(webpackAssert) => {
webpackAssert.assertOutputFileContains(
'async.js',
'async function(){console.log("foo")}().then(()=>{console.log("bar")})'
);

done();
});
});


it('When enabled, react JSX is transformed!', (done) => {
const config = createWebpackConfig('www/build', 'dev');
config.setPublicPath('/build');
Expand Down

0 comments on commit 5414a1d

Please sign in to comment.