From e26d443c3f9327bae3e89a08517789ce00bcf1f0 Mon Sep 17 00:00:00 2001 From: Samuel Scheiderich Date: Wed, 28 Mar 2018 00:53:21 -0400 Subject: [PATCH] Add useBuiltIns babel-preset-env option (fix #843) (#878) --- package.json | 1 + src/transforms/babel.js | 11 +++++++---- test/integration/babel-polyfill/.eslintrc.json | 6 ++++++ test/integration/babel-polyfill/index.js | 3 +++ test/integration/babel-polyfill/package.json | 4 ++++ test/javascript.js | 8 ++++++++ 6 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 test/integration/babel-polyfill/.eslintrc.json create mode 100644 test/integration/babel-polyfill/index.js create mode 100644 test/integration/babel-polyfill/package.json diff --git a/package.json b/package.json index 45735bca187..3d17935678f 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ "@vue/component-compiler-utils": "^1.0.0", "babel-cli": "^6.26.0", "babel-plugin-transform-async-super": "^1.0.0", + "babel-polyfill": "^6.26.0", "babel-register": "^6.26.0", "bsb-js": "^1.0.1", "codecov": "^3.0.0", diff --git a/src/transforms/babel.js b/src/transforms/babel.js index 4e25ac382c5..90cc6caa1c6 100644 --- a/src/transforms/babel.js +++ b/src/transforms/babel.js @@ -217,7 +217,7 @@ function shouldIgnoreBabelrc(filename, babelrc) { async function getEnvConfig(asset, isSourceModule) { // Load the target engines for the app and generate a babel-preset-env config let targetEngines = await getTargetEngines(asset, true); - let targetEnv = await getEnvPlugins(targetEngines); + let targetEnv = await getEnvPlugins(targetEngines, true); if (!targetEnv) { return null; } @@ -226,7 +226,7 @@ async function getEnvConfig(asset, isSourceModule) { // Otherwise, load the source engines and generate a babel-present-env config. if (asset.name.includes(NODE_MODULES) && !isSourceModule) { let sourceEngines = await getTargetEngines(asset, false); - let sourceEnv = (await getEnvPlugins(sourceEngines)) || targetEnv; + let sourceEnv = (await getEnvPlugins(sourceEngines, false)) || targetEnv; // Do a diff of the returned plugins. We only need to process the remaining plugins to get to the app target. let sourcePlugins = new Set(sourceEnv.map(p => p[0])); @@ -240,7 +240,7 @@ async function getEnvConfig(asset, isSourceModule) { const envCache = new Map(); -async function getEnvPlugins(targets) { +async function getEnvPlugins(targets, useBuiltIns = false) { if (!targets) { return null; } @@ -250,7 +250,10 @@ async function getEnvPlugins(targets) { return envCache.get(key); } - let plugins = presetEnv.default({}, {targets, modules: false}).plugins; + let plugins = presetEnv.default( + {}, + {targets, modules: false, useBuiltIns: useBuiltIns ? 'entry' : false} + ).plugins; envCache.set(key, plugins); return plugins; } diff --git a/test/integration/babel-polyfill/.eslintrc.json b/test/integration/babel-polyfill/.eslintrc.json new file mode 100644 index 00000000000..f89e231fa93 --- /dev/null +++ b/test/integration/babel-polyfill/.eslintrc.json @@ -0,0 +1,6 @@ +{ + "extends": "../.eslintrc.json", + "parserOptions": { + "sourceType": "module" + } +} \ No newline at end of file diff --git a/test/integration/babel-polyfill/index.js b/test/integration/babel-polyfill/index.js new file mode 100644 index 00000000000..b126e3d152f --- /dev/null +++ b/test/integration/babel-polyfill/index.js @@ -0,0 +1,3 @@ +import 'babel-polyfill'; + +export async function Bar() {} diff --git a/test/integration/babel-polyfill/package.json b/test/integration/babel-polyfill/package.json new file mode 100644 index 00000000000..b487878df2e --- /dev/null +++ b/test/integration/babel-polyfill/package.json @@ -0,0 +1,4 @@ +{ + "name": "parcel-test-babel-polyfill-browserslist", + "browserslist": ["last 2 Chrome versions"] +} diff --git a/test/javascript.js b/test/javascript.js index 9147408bb47..05f02369491 100644 --- a/test/javascript.js +++ b/test/javascript.js @@ -628,6 +628,14 @@ describe('javascript', function() { assert(!file.includes('class Bar {}')); }); + it('should support splitting babel-polyfill using browserlist', async function() { + await bundle(__dirname + '/integration/babel-polyfill/index.js'); + + let file = fs.readFileSync(__dirname + '/dist/index.js', 'utf8'); + assert(file.includes('async function Bar() {}')); + assert(!file.includes('regenerator')); + }); + it('should support compiling with babel using browserslist for different environments', async function() { async function testBrowserListMultipleEnv(projectBasePath) { // Transpiled destructuring, like r = p.prop1, o = p.prop2, a = p.prop3;