update runtime transformer to modular core-js - fixes #1206

This commit is contained in:
Sebastian McKenzie
2015-04-11 19:07:39 -07:00
parent 1a30f1aafc
commit 3561efdb86
10 changed files with 170 additions and 48 deletions

View File

@@ -0,0 +1,99 @@
{
"builtins": {
"Symbol": "symbol",
"Promise": "promise",
"Map": "map",
"WeakMap": "weak-map",
"Set": "set",
"WeakSet": "weak-set"
},
"methods": {
"Array": {
"concat": "array/concat",
"copyWithin": "array/copy-within",
"entries": "array/entries",
"every": "array/every",
"fill": "array/fill",
"filter": "array/filter",
"findIndex": "array/find-index",
"find": "array/find",
"forEach": "array/for-each",
"from": "array/from",
"includes": "array/includes",
"indexOf": "array/index-of",
"join": "array/join",
"keys": "array/keys",
"lastIndexOf": "array/last-index-of",
"map": "array/map",
"of": "array/of",
"pop": "array/pop",
"push": "array/push",
"reduceRight": "array/reduce-right",
"reduce": "array/reduce",
"reverse": "array/reverse",
"shift": "array/shift",
"slice": "array/slice",
"some": "array/some",
"sort": "array/sort",
"splice": "array/splice",
"turn": "array/turn",
"unshift": "array/unshift",
"values": "array/values"
},
"Object": {
"assign": "object/assign",
"classof": "object/classof",
"define": "object/define",
"entries": "object/entries",
"freeze": "object/freeze",
"getOwnPropertyDescriptor": "object/get-own-property-descriptor",
"getOwnPropertyDescriptors": "object/get-own-property-descriptors",
"getOwnPropertyNames": "object/get-own-property-names",
"getOwnPropertySymbols": "object/get-own-property-symbols",
"getPrototypePf": "object/get-prototype-of",
"index": "object/index",
"isExtensible": "object/is-extensible",
"isFrozen": "object/is-frozen",
"isObject": "object/is-object",
"isSealed": "object/is-sealed",
"is": "object/is",
"keys": "object/keys",
"make": "object/make",
"preventExtensions": "object/prevent-extensions",
"seal": "object/seal",
"setPrototypeOf": "object/set-prototype-of",
"values": "object/values"
},
"RegExp": {
"escape": "regexp/escape"
},
"Function": {
"only": "function/only",
"part": "function/part"
},
"Math": {
"acosh": "math/acosh",
"asinh": "math/asinh",
"atanh": "math/atanh",
"cbrt": "math/cbrt",
"clz32": "math/clz32",
"cosh": "math/cosh",
"expm1": "math/expm1",
"fround": "math/fround",
"pot": "math/pot",
"imul": "math/imul",
"log10": "math/log10",
"log1p": "math/log1p",
"log2": "math/log2",
"sign": "math/sign",
"sinh": "math/sinh",
"tanh": "math/tanh",
"trunc": "math/trunc"
},
"Date": {
"addLocale": "date/add-locale",
"formatUTC": "date/format-utc",
"format": "date/format"
}
}
}

View File

@@ -1,22 +1,15 @@
import core from "core-js/library";
import includes from "lodash/collection/includes";
import * as util from "../../../util";
import * as util from "../../../../util";
import has from "lodash/object/has";
import * as t from "../../../types";
import * as t from "../../../../types";
import definitions from "./definitions";
var isSymbolIterator = t.buildMatchMemberExpression("Symbol.iterator");
var ALIASABLE_CONSTRUCTORS = [
"Symbol",
"Promise",
"Map",
"WeakMap",
"Set",
"WeakSet"
];
const RUNTIME_MODULE_NAME = "babel-runtime";
function coreHas(node) {
return node.name !== "_" && has(core, node.name);
function getForPath(file) {
return file.addImport(`${RUNTIME_MODULE_NAME}/core-js/$for`, "iterator", true);
}
var astVisitor = {
@@ -30,13 +23,17 @@ var astVisitor = {
if (!t.isReferenced(obj, node)) return;
if (!node.computed && coreHas(obj) && has(core[obj.name], prop.name) && !scope.getBindingIdentifier(obj.name)) {
this.skip();
return t.prependToMemberExpression(node, file.get("coreIdentifier"));
}
} else if (this.isReferencedIdentifier() && !t.isMemberExpression(parent) && includes(ALIASABLE_CONSTRUCTORS, node.name) && !scope.getBindingIdentifier(node.name)) {
if (node.computed) return;
if (!has(definitions.methods, obj.name)) return;
if (!has(definitions.methods[obj.name], prop.name)) return;
if (scope.getBindingIdentifier(obj.name)) return;
var modulePath = definitions.methods[obj.name][prop.name];
return file.addImport(`${RUNTIME_MODULE_NAME}/core-js/${modulePath}`, `${obj.name}$${prop.name}`, true);
} else if (this.isReferencedIdentifier() && !t.isMemberExpression(parent) && has(definitions.builtins, node.name) && !scope.getBindingIdentifier(node.name)) {
// Symbol() -> _core.Symbol(); new Promise -> new _core.Promise
return t.memberExpression(file.get("coreIdentifier"), node);
var modulePath = definitions.builtins[node.name];
return file.addImport(`${RUNTIME_MODULE_NAME}/core-js/${modulePath}`, node.name, true);
} else if (this.isCallExpression()) {
// arr[Symbol.iterator]() -> _core.$for.getIterator(arr)
@@ -50,7 +47,7 @@ var astVisitor = {
if (!isSymbolIterator(prop)) return false;
return util.template("corejs-iterator", {
CORE_ID: file.get("coreIdentifier"),
CORE_ID: getForPath(file),
VALUE: callee.object
});
} else if (this.isBinaryExpression()) {
@@ -62,7 +59,7 @@ var astVisitor = {
if (!isSymbolIterator(left)) return;
return util.template("corejs-is-iterator", {
CORE_ID: file.get("coreIdentifier"),
CORE_ID: getForPath(file),
VALUE: node.right
});
}
@@ -79,15 +76,11 @@ exports.Program = function (node, parent, scope, file) {
exports.pre = function (file) {
file.set("helperGenerator", function (name) {
return file.addImport(`babel-runtime/helpers/${name}`, name, true);
});
file.setDynamic("coreIdentifier", function () {
return file.addImport("babel-runtime/core-js", "core", true);
return file.addImport(`${RUNTIME_MODULE_NAME}/helpers/${name}`, name, true);
});
file.setDynamic("regeneratorIdentifier", function () {
return file.addImport("babel-runtime/regenerator", "regeneratorRuntime", true);
return file.addImport(`${RUNTIME_MODULE_NAME}/regenerator`, "regeneratorRuntime", true);
});
};