Skip to content
This repository has been archived by the owner on May 11, 2018. It is now read-only.

Commit

Permalink
separate default builtins for platforms (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Shustov authored and existentialism committed Mar 27, 2017
1 parent a4d585c commit 48a329b
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/default-includes.js
@@ -1,4 +1,4 @@
export default [
export const defaultWebIncludes = [
"web.timers",
"web.immediate",
"web.dom.iterable"
Expand Down
14 changes: 11 additions & 3 deletions src/index.js
@@ -1,6 +1,6 @@
import browserslist from "browserslist";
import builtInsList from "../data/built-ins.json";
import defaultInclude from "./default-includes";
import { defaultWebIncludes } from "./default-includes";
import moduleTransformations from "./module-transformations";
import normalizeOptions, { getElectronChromeVersion } from "./normalize-options.js";
import pluginList from "../data/plugins.json";
Expand Down Expand Up @@ -139,7 +139,7 @@ const logPlugin = (plugin, targets, list) => {
};

const filterItem = (targets, exclusions, list, item) => {
const isDefault = defaultInclude.indexOf(item) >= 0;
const isDefault = defaultWebIncludes.indexOf(item) >= 0;
const notExcluded = exclusions.indexOf(item) === -1;

if (isDefault) return notExcluded;
Expand All @@ -161,6 +161,14 @@ export const transformIncludesAndExcludes = (opts) => ({
builtIns: opts.filter((opt) => opt.match(/^(es\d+|web)\./))
});

function getPlatformSpecificDefaultFor(targets) {
const targetNames = Object.keys(targets);
const isAnyTarget = !targetNames.length;
const isWebTarget = targetNames.some((name) => name !== "node");

return (isAnyTarget || isWebTarget) ? defaultWebIncludes : [];
}

export default function buildPreset(context, opts = {}) {
const validatedOptions = normalizeOptions(opts);
const { debug, loose, moduleType, useBuiltIns } = validatedOptions;
Expand All @@ -181,7 +189,7 @@ export default function buildPreset(context, opts = {}) {
polyfillTargets = getBuiltInTargets(targets);
const filterBuiltIns = filterItem.bind(null, polyfillTargets, exclude.builtIns, builtInsList);
polyfills = Object.keys(builtInsList)
.concat(defaultInclude)
.concat(getPlatformSpecificDefaultFor(polyfillTargets))
.filter(filterBuiltIns)
.concat(include.builtIns);
}
Expand Down
4 changes: 2 additions & 2 deletions src/normalize-options.js
@@ -1,15 +1,15 @@
import invariant from "invariant";
import { electronToChromium } from "electron-to-chromium";
import builtInsList from "../data/built-ins.json";
import defaultInclude from "./default-includes";
import { defaultWebIncludes } from "./default-includes";
import moduleTransformations from "./module-transformations";
import pluginFeatures from "../data/plugin-features";

const validIncludesAndExcludes = [
...Object.keys(pluginFeatures),
...Object.keys(moduleTransformations).map((m) => moduleTransformations[m]),
...Object.keys(builtInsList),
...defaultInclude
...defaultWebIncludes,
];

let hasBeenWarned = false;
Expand Down
@@ -0,0 +1 @@
import "babel-polyfill";
@@ -0,0 +1,7 @@
import "core-js/modules/es7.string.pad-start";
import "core-js/modules/es7.string.pad-end";
import "core-js/modules/web.timers";
import "core-js/modules/web.immediate";
import "core-js/modules/web.dom.iterable";
import "regenerator-runtime/runtime";

13 changes: 13 additions & 0 deletions test/fixtures/preset-options/use-builtins-node-web/options.json
@@ -0,0 +1,13 @@
{
"presets": [
["../../../../lib", {
"targets": {
"chrome": 55,
"node": 7.6,
"uglify": true
},
"modules": false,
"useBuiltIns": true
}]
]
}
1 change: 1 addition & 0 deletions test/fixtures/preset-options/use-builtins-node/actual.js
@@ -0,0 +1 @@
import "babel-polyfill";
4 changes: 4 additions & 0 deletions test/fixtures/preset-options/use-builtins-node/expected.js
@@ -0,0 +1,4 @@
import "core-js/modules/es7.string.pad-start";
import "core-js/modules/es7.string.pad-end";
import "regenerator-runtime/runtime";

12 changes: 12 additions & 0 deletions test/fixtures/preset-options/use-builtins-node/options.json
@@ -0,0 +1,12 @@
{
"presets": [
["../../../../lib", {
"targets": {
"node": 7.6,
"uglify": true
},
"modules": false,
"useBuiltIns": true
}]
]
}

0 comments on commit 48a329b

Please sign in to comment.