setup the babel plugin to transform the babel-polyfill calls + pass the data option to the plugin from the preset
This commit is contained in:
parent
60efc0dd10
commit
6c58d93602
@ -1,4 +1,5 @@
|
||||
import pluginList from "../data/plugins.json";
|
||||
import builtInsList from "../data/builtIns.json";
|
||||
import browserslist from "browserslist";
|
||||
import transformPolyfillRequirePlugin from "./transformPolyfillRequirePlugin";
|
||||
|
||||
@ -146,6 +147,14 @@ export default function buildPreset(context, opts = {}) {
|
||||
let transformations = Object.keys(pluginList)
|
||||
.filter((pluginName) => isPluginRequired(targets, pluginList[pluginName]));
|
||||
|
||||
let polyfills;
|
||||
if (useBuiltIns) {
|
||||
polyfills = Object.keys(builtInsList)
|
||||
.filter((builtInName) => isPluginRequired(targets, builtInsList[builtInName]));
|
||||
console.log(polyfills);
|
||||
console.log(polyfills.length, Object.keys(builtInsList).length);
|
||||
}
|
||||
|
||||
if (debug && !hasBeenLogged) {
|
||||
hasBeenLogged = true;
|
||||
|
||||
@ -182,7 +191,7 @@ export default function buildPreset(context, opts = {}) {
|
||||
plugins: [
|
||||
...modules,
|
||||
...transformations,
|
||||
// useBuiltIns && transformPolyfillRequirePlugin
|
||||
]
|
||||
useBuiltIns === true && [transformPolyfillRequirePlugin, { polyfills }]
|
||||
].filter(Boolean)
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,12 +1,66 @@
|
||||
export default function (babel) {
|
||||
const { types: t } = babel;
|
||||
// Should throw if no babel-polyfill import is found in all files
|
||||
// or if more than one is found
|
||||
|
||||
// const builtIns = require('../data/builtIns.json');
|
||||
const builtIns = {
|
||||
"typed/int8-array": {
|
||||
"chrome": 5,
|
||||
"opera": 12,
|
||||
"firefox": 4,
|
||||
"safari": 5,
|
||||
"node": 0.12,
|
||||
"ie": 10,
|
||||
"android": 4,
|
||||
"ios": 6
|
||||
}
|
||||
};
|
||||
|
||||
const polyfillSource = "babel-polyfill";
|
||||
let numPolyfillImports = 0;
|
||||
|
||||
export default function ({ types: t }) {
|
||||
function checkNumPolyfillImports() {
|
||||
numPolyfillImports++;
|
||||
if (numPolyfillImports > 1) {
|
||||
console.log("multiple babel-polyfill imports found");
|
||||
//throw new Error("multiple babel-polyfill imports found");
|
||||
}
|
||||
}
|
||||
|
||||
function isRequire(path, source) {
|
||||
return t.isExpressionStatement(path.node) &&
|
||||
t.isCallExpression(path.node.expression) &&
|
||||
t.isIdentifier(path.node.expression.callee) &&
|
||||
path.node.expression.callee.name === "require" &&
|
||||
path.node.expression.arguments.length === 1 &&
|
||||
t.isStringLiteral(path.node.expression.arguments[0]) &&
|
||||
path.node.expression.arguments[0].value === source;
|
||||
}
|
||||
|
||||
const isPolyfillImport = {
|
||||
ImportDeclaration(path, state) {
|
||||
if (path.node.specifiers.length === 0 &&
|
||||
path.node.source.value === polyfillSource) {
|
||||
checkNumPolyfillImports();
|
||||
|
||||
// change
|
||||
// path.node.source.value = "changed";
|
||||
}
|
||||
},
|
||||
Program(path, state) {
|
||||
path.get("body").forEach((bodyPath) => {
|
||||
if (isRequire(bodyPath, polyfillSource)) {
|
||||
checkNumPolyfillImports();
|
||||
|
||||
// change
|
||||
// bodyPath.node.expression.arguments[0].value = "changed";
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
name: "ast-transform", // not required
|
||||
visitor: {
|
||||
Identifier(path) {
|
||||
|
||||
}
|
||||
}
|
||||
visitor: isPolyfillImport
|
||||
};
|
||||
}
|
||||
|
||||
1
experimental/babel-preset-env/test/fixtures/preset-options/use-builtins-chrome-55/actual.js
vendored
Normal file
1
experimental/babel-preset-env/test/fixtures/preset-options/use-builtins-chrome-55/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
1 ** 2;
|
||||
@ -0,0 +1 @@
|
||||
1 ** 2;
|
||||
11
experimental/babel-preset-env/test/fixtures/preset-options/use-builtins-chrome-55/options.json
vendored
Normal file
11
experimental/babel-preset-env/test/fixtures/preset-options/use-builtins-chrome-55/options.json
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"presets": [
|
||||
["../../../../lib", {
|
||||
"targets": {
|
||||
"chrome": 54
|
||||
},
|
||||
"modules": false,
|
||||
"useBuiltIns": true
|
||||
}]
|
||||
]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user