Skip to content

Commit 5414a1d

Browse files
committed
feature #612 Don't make @babel/preset-env force all transforms in prod (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
2 parents 54cb1b8 + b2809f5 commit 5414a1d

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

fixtures/js/async_function.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
async function foo() {
2+
console.log('foo');
3+
}
4+
5+
foo().then(() => {
6+
console.log('bar');
7+
});

lib/loaders/babel.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ module.exports = {
4242
// https://babeljs.io/docs/en/babel-preset-env#modules
4343
modules: false,
4444
targets: {},
45-
forceAllTransforms: webpackConfig.isProduction(),
4645
useBuiltIns: webpackConfig.babelOptions.useBuiltIns,
4746
corejs: webpackConfig.babelOptions.corejs,
4847
};

test/functional.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,41 @@ module.exports = {
11571157
});
11581158
});
11591159

1160+
it('Babel does not force transforms if they are not needed', (done) => {
1161+
const cwd = process.cwd();
1162+
after(() => {
1163+
process.chdir(cwd);
1164+
});
1165+
1166+
const appDir = testSetup.createTestAppDir();
1167+
process.chdir(appDir);
1168+
1169+
fs.writeFileSync(
1170+
path.join(appDir, 'package.json'),
1171+
1172+
// Chrome 55 supports async and arrow functions
1173+
'{"browserslist": "Chrome 55"}'
1174+
);
1175+
1176+
const config = createWebpackConfig('www/build', 'prod');
1177+
config.setPublicPath('/build');
1178+
config.addEntry('async', './js/async_function.js');
1179+
config.configureBabel(null, {
1180+
useBuiltIns: 'usage',
1181+
corejs: 3,
1182+
});
1183+
1184+
testSetup.runWebpack(config, async(webpackAssert) => {
1185+
webpackAssert.assertOutputFileContains(
1186+
'async.js',
1187+
'async function(){console.log("foo")}().then(()=>{console.log("bar")})'
1188+
);
1189+
1190+
done();
1191+
});
1192+
});
1193+
1194+
11601195
it('When enabled, react JSX is transformed!', (done) => {
11611196
const config = createWebpackConfig('www/build', 'dev');
11621197
config.setPublicPath('/build');

0 commit comments

Comments
 (0)