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;