Skip to content

Commit

Permalink
Add useBuiltIns babel-preset-env option (fix #843) (#878)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsch authored and devongovett committed Mar 28, 2018
1 parent 6e8ae0d commit e26d443
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 7 additions & 4 deletions src/transforms/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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]));
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions test/integration/babel-polyfill/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../.eslintrc.json",
"parserOptions": {
"sourceType": "module"
}
}
3 changes: 3 additions & 0 deletions test/integration/babel-polyfill/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'babel-polyfill';

export async function Bar() {}
4 changes: 4 additions & 0 deletions test/integration/babel-polyfill/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "parcel-test-babel-polyfill-browserslist",
"browserslist": ["last 2 Chrome versions"]
}
8 changes: 8 additions & 0 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e26d443

Please sign in to comment.