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:
Henry Zhu 2016-12-02 13:04:21 -05:00
parent 60efc0dd10
commit 6c58d93602
5 changed files with 85 additions and 9 deletions

View File

@ -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)
};
}

View File

@ -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
};
}

View File

@ -0,0 +1,11 @@
{
"presets": [
["../../../../lib", {
"targets": {
"chrome": 54
},
"modules": false,
"useBuiltIns": true
}]
]
}