Split @babel/runtime into 2 modules via @babel/runtime-corejs2 (#8266)
In Babel 6, we had `@babel/runtime` which _always_ uses `core-js` in its helpers, meaning that anyone who wants to use our helpers in a centralized modular way had to also use core-js, which isn't always ideal.
Early in the Babel 7 alpha, we added a `useBuiltins: true` option that would use separate copies of the helpers that would skip loading `core-js`, but this _still_ requires users to have an indirect npm dependency on `core-js`.
This PR splits our setup into two separate runtimes:
* ```
'plugins': [
['transform-runtime'],
]
```
with `npm install --save @babel/runtime` that just includes our helpers, and relies on users to globally polyfill any APIs they need.
* ```
'plugins': [
['transform-runtime', { corejs: 2 }],
]
```
with `npm install --save @babel/runtime-corejs2` that includes everything `@babel/runtime` does, but also includes a passthrough API for `corejs@2.x` and rewrites all of the helper modules to reference `core-js`.
This commit is contained in:
commit
8cffbbae75
@ -4,8 +4,9 @@
|
||||
package.json
|
||||
lerna.json
|
||||
packages/babel-runtime
|
||||
!packages/babel-runtime/scripts
|
||||
!packages/babel-runtime/core-js.js
|
||||
!packages/babel-plugin-transform-runtime/scripts
|
||||
!packages/babel-runtime-corejs2/core-js.js
|
||||
packages/babel-runtime-corejs2
|
||||
packages/*/node_modules
|
||||
packages/*/lib
|
||||
packages/*/dist
|
||||
|
||||
31
.gitignore
vendored
31
.gitignore
vendored
@ -12,24 +12,27 @@ coverage
|
||||
dist
|
||||
/.package.json
|
||||
package-lock.json
|
||||
/packages/babel-runtime/core-js/**/*.js
|
||||
!/packages/babel-runtime/core-js/map.js
|
||||
|
||||
/packages/babel-runtime/helpers/*.js
|
||||
!/packages/babel-runtime/helpers/toArray.js
|
||||
!/packages/babel-runtime/helpers/iterableToArray.js
|
||||
!/packages/babel-runtime/helpers/temporalRef.js
|
||||
/packages/babel-runtime/helpers/builtin/*.js
|
||||
!/packages/babel-runtime/helpers/builtin/toArray.js
|
||||
!/packages/babel-runtime/helpers/builtin/iterableToArray.js
|
||||
!/packages/babel-runtime/helpers/builtin/temporalRef.js
|
||||
/packages/babel-runtime/helpers/builtin/es6/*.js
|
||||
!/packages/babel-runtime/helpers/builtin/es6/toArray.js
|
||||
!/packages/babel-runtime/helpers/builtin/es6/iterableToArray.js
|
||||
!/packages/babel-runtime/helpers/builtin/es6/temporalRef.js
|
||||
/packages/babel-runtime/helpers/es6/*.js
|
||||
!/packages/babel-runtime/helpers/es6/toArray.js
|
||||
!/packages/babel-runtime/helpers/es6/iterableToArray.js
|
||||
!/packages/babel-runtime/helpers/es6/temporalRef.js
|
||||
/packages/babel-runtime/helpers/esm/*.js
|
||||
!/packages/babel-runtime/helpers/esm/toArray.js
|
||||
!/packages/babel-runtime/helpers/esm/iterableToArray.js
|
||||
!/packages/babel-runtime/helpers/esm/temporalRef.js
|
||||
|
||||
/packages/babel-runtime-corejs2/helpers/*.js
|
||||
!/packages/babel-runtime-corejs2/helpers/toArray.js
|
||||
!/packages/babel-runtime-corejs2/helpers/iterableToArray.js
|
||||
!/packages/babel-runtime-corejs2/helpers/temporalRef.js
|
||||
/packages/babel-runtime-corejs2/helpers/esm/*.js
|
||||
!/packages/babel-runtime-corejs2/helpers/esm/toArray.js
|
||||
!/packages/babel-runtime-corejs2/helpers/esm/iterableToArray.js
|
||||
!/packages/babel-runtime-corejs2/helpers/esm/temporalRef.js
|
||||
/packages/babel-runtime-corejs2/core-js/**/*.js
|
||||
!/packages/babel-runtime-corejs2/core-js/map.js
|
||||
|
||||
/packages/babel-register/test/.babel
|
||||
/packages/babel-cli/test/tmp
|
||||
/packages/babel-node/test/tmp
|
||||
|
||||
7
Makefile
7
Makefile
@ -31,7 +31,7 @@ build-preset-env-standalone:
|
||||
build-dist: build
|
||||
cd packages/babel-polyfill; \
|
||||
scripts/build-dist.sh
|
||||
cd packages/babel-runtime; \
|
||||
cd packages/babel-plugin-transform-runtime; \
|
||||
node scripts/build-dist.js
|
||||
|
||||
watch: clean clean-lib
|
||||
@ -108,7 +108,8 @@ publish:
|
||||
git pull --rebase
|
||||
make clean-lib
|
||||
rm -rf packages/babel-runtime/helpers
|
||||
rm -rf packages/babel-runtime/core-js
|
||||
rm -rf packages/babel-runtime-corejs2/helpers
|
||||
rm -rf packages/babel-runtime-corejs2/core-js
|
||||
BABEL_ENV=production make build-dist
|
||||
make test
|
||||
# not using lerna independent mode atm, so only update packages that have changed since we use ^
|
||||
@ -120,7 +121,7 @@ bootstrap: clean-all
|
||||
yarn --ignore-engines
|
||||
./node_modules/.bin/lerna bootstrap -- --ignore-engines
|
||||
make build
|
||||
cd packages/babel-runtime; \
|
||||
cd packages/babel-plugin-transform-runtime; \
|
||||
node scripts/build-dist.js
|
||||
|
||||
clean-lib:
|
||||
|
||||
@ -1,12 +1,10 @@
|
||||
var _regeneratorRuntime = require("@babel/runtime/regenerator");
|
||||
|
||||
var _Object$keys = require("@babel/runtime/core-js/object/keys");
|
||||
|
||||
var _marked =
|
||||
/*#__PURE__*/
|
||||
_regeneratorRuntime.mark(fn);
|
||||
|
||||
_Object$keys({});
|
||||
Object.keys({});
|
||||
|
||||
function fn() {
|
||||
return _regeneratorRuntime.wrap(function fn$(_context) {
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
scripts
|
||||
src
|
||||
test
|
||||
*.log
|
||||
|
||||
@ -17,6 +17,11 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "7.0.0-beta.55",
|
||||
"@babel/helper-plugin-test-runner": "7.0.0-beta.55"
|
||||
"@babel/helpers": "7.0.0-beta.55",
|
||||
"@babel/helper-plugin-test-runner": "7.0.0-beta.55",
|
||||
"@babel/plugin-transform-runtime": "7.0.0-beta.55",
|
||||
"@babel/template": "7.0.0-beta.55",
|
||||
"@babel/preset-env": "7.0.0-beta.55",
|
||||
"@babel/types": "7.0.0-beta.53"
|
||||
}
|
||||
}
|
||||
|
||||
158
packages/babel-plugin-transform-runtime/scripts/build-dist.js
Normal file
158
packages/babel-plugin-transform-runtime/scripts/build-dist.js
Normal file
@ -0,0 +1,158 @@
|
||||
"use strict";
|
||||
|
||||
const path = require("path");
|
||||
const outputFile = require("output-file-sync");
|
||||
const helpers = require("@babel/helpers");
|
||||
const babel = require("@babel/core");
|
||||
const template = require("@babel/template");
|
||||
const t = require("@babel/types");
|
||||
|
||||
const transformRuntime = require("../");
|
||||
const corejs2Definitions = require("../lib/definitions").default;
|
||||
|
||||
writeHelpers("@babel/runtime");
|
||||
|
||||
writeCoreJS2("@babel/runtime-corejs2");
|
||||
writeHelpers("@babel/runtime-corejs2", { corejs: 2 });
|
||||
|
||||
function writeCoreJS2(runtimeName) {
|
||||
const pkgDirname = getRuntimeRoot(runtimeName);
|
||||
|
||||
const paths = ["is-iterable", "get-iterator"];
|
||||
|
||||
Object.keys(corejs2Definitions.builtins).forEach(key => {
|
||||
const path = corejs2Definitions.builtins[key];
|
||||
paths.push(path);
|
||||
});
|
||||
|
||||
Object.keys(corejs2Definitions.methods).forEach(key => {
|
||||
const props = corejs2Definitions.methods[key];
|
||||
Object.keys(props).forEach(key2 => {
|
||||
paths.push(props[key2]);
|
||||
});
|
||||
});
|
||||
|
||||
paths.forEach(function(corePath) {
|
||||
outputFile(
|
||||
path.join(pkgDirname, "core-js", `${corePath}.js`),
|
||||
`module.exports = require("core-js/library/fn/${corePath}");`
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function writeHelpers(runtimeName, { corejs } = {}) {
|
||||
writeHelperFiles(runtimeName, { corejs, esm: false });
|
||||
writeHelperFiles(runtimeName, { corejs, esm: true });
|
||||
}
|
||||
|
||||
function writeHelperFiles(runtimeName, { esm, corejs }) {
|
||||
const pkgDirname = getRuntimeRoot(runtimeName);
|
||||
|
||||
for (const helperName of helpers.list) {
|
||||
const helperFilename = path.join(
|
||||
pkgDirname,
|
||||
"helpers",
|
||||
esm ? "esm" : "",
|
||||
`${helperName}.js`
|
||||
);
|
||||
|
||||
outputFile(
|
||||
helperFilename,
|
||||
buildHelper(runtimeName, pkgDirname, helperFilename, helperName, {
|
||||
esm,
|
||||
corejs,
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function getRuntimeRoot(runtimeName) {
|
||||
return path.resolve(
|
||||
__dirname,
|
||||
"..",
|
||||
"..",
|
||||
runtimeName.replace(/^@babel\//, "babel-")
|
||||
);
|
||||
}
|
||||
|
||||
function buildHelper(
|
||||
runtimeName,
|
||||
pkgDirname,
|
||||
helperFilename,
|
||||
helperName,
|
||||
{ esm, corejs }
|
||||
) {
|
||||
const tree = t.program([], [], esm ? "module" : "script");
|
||||
const dependencies = {};
|
||||
let bindings = null;
|
||||
|
||||
if (!esm) {
|
||||
bindings = [];
|
||||
for (const dep of helpers.getDependencies(helperName)) {
|
||||
const id = (dependencies[dep] = t.identifier(t.toIdentifier(dep)));
|
||||
tree.body.push(template.statement.ast`
|
||||
var ${id} = require("${`./${dep}`}");
|
||||
`);
|
||||
bindings.push(id.name);
|
||||
}
|
||||
}
|
||||
|
||||
const helper = helpers.get(
|
||||
helperName,
|
||||
dep => dependencies[dep],
|
||||
esm ? null : template.expression.ast`module.exports`,
|
||||
bindings
|
||||
);
|
||||
tree.body.push(...helper.nodes);
|
||||
|
||||
return babel.transformFromAst(tree, null, {
|
||||
presets: [[require("@babel/preset-env"), { modules: false }]],
|
||||
plugins: [
|
||||
[transformRuntime, { corejs, useESModules: esm }],
|
||||
buildRuntimeRewritePlugin(
|
||||
runtimeName,
|
||||
path.relative(path.dirname(helperFilename), pkgDirname),
|
||||
helperName
|
||||
),
|
||||
],
|
||||
}).code;
|
||||
}
|
||||
|
||||
function buildRuntimeRewritePlugin(runtimeName, relativePath, helperName) {
|
||||
function adjustImportPath(node, relativePath) {
|
||||
node.value =
|
||||
helpers.list.indexOf(node.value) !== -1
|
||||
? `./${node.value}`
|
||||
: node.value.replace(runtimeName + "/", relativePath + "/");
|
||||
}
|
||||
|
||||
return {
|
||||
pre(file) {
|
||||
const original = file.get("helperGenerator");
|
||||
file.set("helperGenerator", name => {
|
||||
// make sure that helpers won't insert circular references to themselves
|
||||
if (name === helperName) return false;
|
||||
|
||||
return original(name);
|
||||
});
|
||||
},
|
||||
visitor: {
|
||||
ImportDeclaration(path) {
|
||||
adjustImportPath(path.get("source").node, relativePath);
|
||||
},
|
||||
CallExpression(path) {
|
||||
if (
|
||||
!path.get("callee").isIdentifier({ name: "require" }) ||
|
||||
path.get("arguments").length !== 1 ||
|
||||
!path.get("arguments")[0].isStringLiteral()
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
// replace any reference to @babel/runtime and other helpers
|
||||
// with a relative path
|
||||
adjustImportPath(path.get("arguments")[0].node, relativePath);
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
@ -8,29 +8,84 @@ export default declare((api, options) => {
|
||||
api.assertVersion(7);
|
||||
|
||||
const {
|
||||
helpers,
|
||||
moduleName = "@babel/runtime",
|
||||
polyfill,
|
||||
regenerator,
|
||||
useBuiltIns,
|
||||
useESModules,
|
||||
corejs: corejsVersion = false,
|
||||
helpers: useRuntimeHelpers = true,
|
||||
regenerator: useRuntimeRegenerator = true,
|
||||
useESModules = false,
|
||||
version: runtimeVersion = "7.0.0-beta.0",
|
||||
} = options;
|
||||
const regeneratorEnabled = regenerator !== false;
|
||||
const notPolyfillOrDoesUseBuiltIns = polyfill === false || useBuiltIns;
|
||||
const isPolyfillAndUseBuiltIns = polyfill && useBuiltIns;
|
||||
const baseHelpersDir = useBuiltIns ? "helpers/builtin" : "helpers";
|
||||
const helpersDir = useESModules ? `${baseHelpersDir}/es6` : baseHelpersDir;
|
||||
|
||||
if (typeof useRuntimeRegenerator !== "boolean") {
|
||||
throw new Error(
|
||||
"The 'regenerator' option must be undefined, or a boolean.",
|
||||
);
|
||||
}
|
||||
if (typeof useRuntimeHelpers !== "boolean") {
|
||||
throw new Error("The 'helpers' option must be undefined, or a boolean.");
|
||||
}
|
||||
if (typeof useESModules !== "boolean") {
|
||||
throw new Error(
|
||||
"The 'useESModules' option must be undefined, or a boolean.",
|
||||
);
|
||||
}
|
||||
if (
|
||||
corejsVersion !== false &&
|
||||
(typeof corejsVersion !== "number" || corejsVersion !== 2) &&
|
||||
(typeof corejsVersion !== "string" || corejsVersion !== "2")
|
||||
) {
|
||||
throw new Error(
|
||||
`The 'corejs' option must be undefined, false, or 2, or '2', ` +
|
||||
`but got ${JSON.stringify(corejsVersion)}.`,
|
||||
);
|
||||
}
|
||||
|
||||
function has(obj, key) {
|
||||
return Object.prototype.hasOwnProperty.call(obj, key);
|
||||
}
|
||||
if (has(options, "useBuiltIns")) {
|
||||
if (options.useBuiltIns) {
|
||||
throw new Error(
|
||||
"The 'useBuiltIns' option has been removed. The @babel/runtime " +
|
||||
"module now uses builtins by default.",
|
||||
);
|
||||
} else {
|
||||
throw new Error(
|
||||
"The 'useBuiltIns' option has been removed. Use the 'corejs'" +
|
||||
"option with value '2' to polyfill with CoreJS 2.x via @babel/runtime.",
|
||||
);
|
||||
}
|
||||
}
|
||||
if (has(options, "polyfill")) {
|
||||
if (options.polyfill === false) {
|
||||
throw new Error(
|
||||
"The 'polyfill' option has been removed. The @babel/runtime " +
|
||||
"module now skips polyfilling by default.",
|
||||
);
|
||||
} else {
|
||||
throw new Error(
|
||||
"The 'polyfill' option has been removed. Use the 'corejs'" +
|
||||
"option with value '2' to polyfill with CoreJS 2.x via @babel/runtime.",
|
||||
);
|
||||
}
|
||||
}
|
||||
if (has(options, "moduleName")) {
|
||||
throw new Error(
|
||||
"The 'moduleName' option has been removed. @babel/transform-runtime " +
|
||||
"no longer supports arbitrary runtimes.",
|
||||
);
|
||||
}
|
||||
|
||||
const helpersDir = useESModules ? "helpers/esm" : "helpers";
|
||||
const injectCoreJS2 = `${corejsVersion}` === "2";
|
||||
const moduleName = injectCoreJS2
|
||||
? "@babel/runtime-corejs2"
|
||||
: "@babel/runtime";
|
||||
|
||||
const HEADER_HELPERS = ["interopRequireWildcard", "interopRequireDefault"];
|
||||
|
||||
return {
|
||||
pre(file) {
|
||||
if (helpers !== false) {
|
||||
if (useRuntimeHelpers) {
|
||||
file.set("helperGenerator", name => {
|
||||
// If the helper didn't exist yet at the version given, we bail
|
||||
// out and let Babel either insert it directly, or throw an error
|
||||
@ -58,14 +113,6 @@ export default declare((api, options) => {
|
||||
});
|
||||
}
|
||||
|
||||
if (isPolyfillAndUseBuiltIns) {
|
||||
throw new Error(
|
||||
"The polyfill option conflicts with useBuiltIns; use one or the other",
|
||||
);
|
||||
}
|
||||
|
||||
this.moduleName = moduleName;
|
||||
|
||||
const cache = new Map();
|
||||
|
||||
this.addDefaultImport = (source, nameHint, blockHoist) => {
|
||||
@ -94,17 +141,17 @@ export default declare((api, options) => {
|
||||
visitor: {
|
||||
ReferencedIdentifier(path) {
|
||||
const { node, parent, scope } = path;
|
||||
if (node.name === "regeneratorRuntime" && regeneratorEnabled) {
|
||||
if (node.name === "regeneratorRuntime" && useRuntimeRegenerator) {
|
||||
path.replaceWith(
|
||||
this.addDefaultImport(
|
||||
`${this.moduleName}/regenerator`,
|
||||
`${moduleName}/regenerator`,
|
||||
"regeneratorRuntime",
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (notPolyfillOrDoesUseBuiltIns) return;
|
||||
if (!injectCoreJS2) return;
|
||||
|
||||
if (t.isMemberExpression(parent)) return;
|
||||
if (!has(definitions.builtins, node.name)) return;
|
||||
@ -121,7 +168,7 @@ export default declare((api, options) => {
|
||||
|
||||
// arr[Symbol.iterator]() -> _core.$for.getIterator(arr)
|
||||
CallExpression(path) {
|
||||
if (notPolyfillOrDoesUseBuiltIns) return;
|
||||
if (!injectCoreJS2) return;
|
||||
|
||||
// we can't compile this
|
||||
if (path.node.arguments.length) return;
|
||||
@ -146,7 +193,7 @@ export default declare((api, options) => {
|
||||
|
||||
// Symbol.iterator in arr -> core.$for.isIterable(arr)
|
||||
BinaryExpression(path) {
|
||||
if (notPolyfillOrDoesUseBuiltIns) return;
|
||||
if (!injectCoreJS2) return;
|
||||
|
||||
if (path.node.operator !== "in") return;
|
||||
if (!path.get("left").matchesPattern("Symbol.iterator")) return;
|
||||
@ -165,7 +212,7 @@ export default declare((api, options) => {
|
||||
// Array.from -> _core.Array.from
|
||||
MemberExpression: {
|
||||
enter(path) {
|
||||
if (notPolyfillOrDoesUseBuiltIns) return;
|
||||
if (!injectCoreJS2) return;
|
||||
if (!path.isReferenced()) return;
|
||||
|
||||
const { node } = path;
|
||||
@ -203,7 +250,7 @@ export default declare((api, options) => {
|
||||
},
|
||||
|
||||
exit(path) {
|
||||
if (notPolyfillOrDoesUseBuiltIns) return;
|
||||
if (!injectCoreJS2) return;
|
||||
if (!path.isReferenced()) return;
|
||||
|
||||
const { node } = path;
|
||||
@ -227,5 +274,3 @@ export default declare((api, options) => {
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
export { definitions };
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"plugins": ["transform-runtime", "transform-regenerator"]
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
obj.constructor === Object;
|
||||
obj.constructor === Promise;
|
||||
|
||||
Symbol();
|
||||
Symbol("test");
|
||||
|
||||
new Map();
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": [["transform-runtime", { "corejs": 2 }], "transform-regenerator"]
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
var _Map = require("@babel/runtime-corejs2/core-js/map");
|
||||
|
||||
var _Symbol = require("@babel/runtime-corejs2/core-js/symbol");
|
||||
|
||||
var _Promise = require("@babel/runtime-corejs2/core-js/promise");
|
||||
|
||||
obj.constructor === Object;
|
||||
obj.constructor === _Promise;
|
||||
|
||||
_Symbol();
|
||||
|
||||
_Symbol("test");
|
||||
|
||||
new _Map();
|
||||
1
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/catch-all/input.js
vendored
Normal file
1
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/catch-all/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
Promise.resolve;
|
||||
3
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/catch-all/options.json
vendored
Normal file
3
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/catch-all/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": [["transform-runtime", { "corejs": 2 }], "transform-regenerator"]
|
||||
}
|
||||
3
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/catch-all/output.js
vendored
Normal file
3
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/catch-all/output.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var _Promise = require("@babel/runtime-corejs2/core-js/promise");
|
||||
|
||||
_Promise.resolve;
|
||||
1
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/class/input.js
vendored
Normal file
1
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/class/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
class Foo {}
|
||||
3
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/class/options.json
vendored
Normal file
3
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/class/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": [["transform-runtime", { "corejs": 2 }], "transform-classes"]
|
||||
}
|
||||
7
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/class/output.js
vendored
Normal file
7
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/class/output.js
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
var _classCallCheck = require("@babel/runtime-corejs2/helpers/classCallCheck");
|
||||
|
||||
let Foo = function Foo() {
|
||||
"use strict";
|
||||
|
||||
_classCallCheck(this, Foo);
|
||||
};
|
||||
3
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/es6-for-of/input.js
vendored
Normal file
3
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/es6-for-of/input.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
for (var i of arr) {
|
||||
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-for-of", ["transform-runtime", { "corejs": 2 }]]
|
||||
}
|
||||
24
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/es6-for-of/output.js
vendored
Normal file
24
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/es6-for-of/output.js
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
var _getIterator = require("@babel/runtime-corejs2/core-js/get-iterator");
|
||||
|
||||
var _iteratorNormalCompletion = true;
|
||||
var _didIteratorError = false;
|
||||
var _iteratorError = undefined;
|
||||
|
||||
try {
|
||||
for (var _iterator = _getIterator(arr), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
||||
var i = _step.value;
|
||||
}
|
||||
} catch (err) {
|
||||
_didIteratorError = true;
|
||||
_iteratorError = err;
|
||||
} finally {
|
||||
try {
|
||||
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
||||
_iterator.return();
|
||||
}
|
||||
} finally {
|
||||
if (_didIteratorError) {
|
||||
throw _iteratorError;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/full/options.json
vendored
Normal file
3
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/full/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": [["transform-runtime", { "corejs": 2 }], "transform-regenerator"]
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
import _regeneratorRuntime from "foo/regenerator";
|
||||
import _Symbol from "foo/core-js/symbol";
|
||||
import _regeneratorRuntime from "@babel/runtime-corejs2/regenerator";
|
||||
import _Symbol from "@babel/runtime-corejs2/core-js/symbol";
|
||||
|
||||
var _marked =
|
||||
/*#__PURE__*/
|
||||
@ -0,0 +1,5 @@
|
||||
import foo from "foo";
|
||||
|
||||
class Example {
|
||||
method() {}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
{
|
||||
"plugins": [
|
||||
["transform-runtime", { "corejs": 2 }],
|
||||
"transform-modules-commonjs",
|
||||
"transform-classes"
|
||||
]
|
||||
}
|
||||
23
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/modules-helpers/output.js
vendored
Normal file
23
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/modules-helpers/output.js
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
|
||||
var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
|
||||
|
||||
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/classCallCheck"));
|
||||
|
||||
var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/createClass"));
|
||||
|
||||
var _foo = _interopRequireDefault(require("foo"));
|
||||
|
||||
let Example =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
function Example() {
|
||||
(0, _classCallCheck2.default)(this, Example);
|
||||
}
|
||||
|
||||
(0, _createClass2.default)(Example, [{
|
||||
key: "method",
|
||||
value: function method() {}
|
||||
}]);
|
||||
return Example;
|
||||
}();
|
||||
4
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/modules/input.mjs
vendored
Normal file
4
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/modules/input.mjs
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
import foo from "bar";
|
||||
foo;
|
||||
|
||||
export * from "mod";
|
||||
3
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/modules/options.json
vendored
Normal file
3
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/modules/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": [["transform-runtime", { "corejs": 2 }], "transform-modules-commonjs"]
|
||||
}
|
||||
28
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/modules/output.js
vendored
Normal file
28
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/modules/output.js
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
"use strict";
|
||||
|
||||
var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
|
||||
|
||||
var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");
|
||||
|
||||
var _Object$keys = require("@babel/runtime-corejs2/core-js/object/keys");
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _bar = _interopRequireDefault(require("bar"));
|
||||
|
||||
var _mod = require("mod");
|
||||
|
||||
_Object$keys(_mod).forEach(function (key) {
|
||||
if (key === "default" || key === "__esModule") return;
|
||||
|
||||
_Object$defineProperty(exports, key, {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _mod[key];
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
_bar.default;
|
||||
1
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/no-helpers/input.js
vendored
Normal file
1
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/no-helpers/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
class Foo {}
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": [["transform-runtime", { "helpers": false, "corejs": 2 }], "transform-classes"]
|
||||
}
|
||||
7
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/no-helpers/output.js
vendored
Normal file
7
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/no-helpers/output.js
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
let Foo = function Foo() {
|
||||
"use strict";
|
||||
|
||||
_classCallCheck(this, Foo);
|
||||
};
|
||||
@ -0,0 +1,2 @@
|
||||
void function* () {
|
||||
};
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": [["transform-runtime", { "corejs": 2 }], "transform-regenerator"]
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
var _regeneratorRuntime = require("@babel/runtime-corejs2/regenerator");
|
||||
|
||||
void
|
||||
/*#__PURE__*/
|
||||
_regeneratorRuntime.mark(function _callee() {
|
||||
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
||||
while (1) switch (_context.prev = _context.next) {
|
||||
case 0:
|
||||
case "end":
|
||||
return _context.stop();
|
||||
}
|
||||
}, _callee, this);
|
||||
});
|
||||
@ -0,0 +1 @@
|
||||
Symbol.iterator in Object(arr);
|
||||
@ -0,0 +1,6 @@
|
||||
{
|
||||
"plugins": [
|
||||
["transform-runtime", { "corejs": 2 }],
|
||||
"transform-regenerator"
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
var _isIterable = require("@babel/runtime-corejs2/core-js/is-iterable");
|
||||
|
||||
_isIterable(Object(arr));
|
||||
@ -0,0 +1 @@
|
||||
Symbol.iterator;
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": [["transform-runtime", { "corejs": 2 }], "transform-regenerator"]
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
var _Symbol$iterator = require("@babel/runtime-corejs2/core-js/symbol/iterator");
|
||||
|
||||
_Symbol$iterator;
|
||||
@ -1,14 +1,5 @@
|
||||
var _Map = require("@babel/runtime/core-js/map");
|
||||
|
||||
var _Symbol = require("@babel/runtime/core-js/symbol");
|
||||
|
||||
var _Promise = require("@babel/runtime/core-js/promise");
|
||||
|
||||
obj.constructor === Object;
|
||||
obj.constructor === _Promise;
|
||||
|
||||
_Symbol();
|
||||
|
||||
_Symbol("test");
|
||||
|
||||
new _Map();
|
||||
obj.constructor === Promise;
|
||||
Symbol();
|
||||
Symbol("test");
|
||||
new Map();
|
||||
|
||||
@ -1,3 +1 @@
|
||||
var _Promise = require("@babel/runtime/core-js/promise");
|
||||
|
||||
_Promise.resolve;
|
||||
Promise.resolve;
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"plugins": [["transform-runtime", { "moduleName": "foo" }], "transform-regenerator"]
|
||||
}
|
||||
@ -1,11 +1,9 @@
|
||||
var _getIterator = require("@babel/runtime/core-js/get-iterator");
|
||||
|
||||
var _iteratorNormalCompletion = true;
|
||||
var _didIteratorError = false;
|
||||
var _iteratorError = undefined;
|
||||
|
||||
try {
|
||||
for (var _iterator = _getIterator(arr), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
||||
for (var _iterator = arr[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
||||
var i = _step.value;
|
||||
}
|
||||
} catch (err) {
|
||||
|
||||
@ -1,12 +1,11 @@
|
||||
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
||||
import _Symbol from "@babel/runtime/core-js/symbol";
|
||||
|
||||
var _marked =
|
||||
/*#__PURE__*/
|
||||
_regeneratorRuntime.mark(giveWord);
|
||||
|
||||
import foo, * as bar from "someModule";
|
||||
export const myWord = _Symbol("abc");
|
||||
export const myWord = Symbol("abc");
|
||||
export function giveWord() {
|
||||
return _regeneratorRuntime.wrap(function giveWord$(_context) {
|
||||
while (1) switch (_context.prev = _context.next) {
|
||||
|
||||
@ -2,10 +2,6 @@
|
||||
|
||||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
||||
|
||||
var _Object$defineProperty = require("@babel/runtime/core-js/object/define-property");
|
||||
|
||||
var _Object$keys = require("@babel/runtime/core-js/object/keys");
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
@ -14,15 +10,13 @@ var _bar = _interopRequireDefault(require("bar"));
|
||||
|
||||
var _mod = require("mod");
|
||||
|
||||
_Object$keys(_mod).forEach(function (key) {
|
||||
Object.keys(_mod).forEach(function (key) {
|
||||
if (key === "default" || key === "__esModule") return;
|
||||
|
||||
_Object$defineProperty(exports, key, {
|
||||
Object.defineProperty(exports, key, {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _mod[key];
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
_bar.default;
|
||||
|
||||
@ -1,3 +1 @@
|
||||
var _isIterable = require("@babel/runtime/core-js/is-iterable");
|
||||
|
||||
_isIterable(Object(arr));
|
||||
Symbol.iterator in Object(arr);
|
||||
|
||||
@ -1,3 +1 @@
|
||||
var _Symbol$iterator = require("@babel/runtime/core-js/symbol/iterator");
|
||||
|
||||
_Symbol$iterator;
|
||||
Symbol.iterator;
|
||||
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": [["transform-runtime", { "corejs": 2, "useESModules": true }], "transform-classes"]
|
||||
}
|
||||
23
packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs-useES6Modules/output.js
vendored
Normal file
23
packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs-useES6Modules/output.js
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
var _classCallCheck = require("@babel/runtime-corejs2/helpers/esm/classCallCheck");
|
||||
|
||||
var _possibleConstructorReturn = require("@babel/runtime-corejs2/helpers/esm/possibleConstructorReturn");
|
||||
|
||||
var _getPrototypeOf = require("@babel/runtime-corejs2/helpers/esm/getPrototypeOf");
|
||||
|
||||
var _inherits = require("@babel/runtime-corejs2/helpers/esm/inherits");
|
||||
|
||||
let Foo =
|
||||
/*#__PURE__*/
|
||||
function (_Bar) {
|
||||
"use strict";
|
||||
|
||||
_inherits(Foo, _Bar);
|
||||
|
||||
function Foo() {
|
||||
_classCallCheck(this, Foo);
|
||||
|
||||
return _possibleConstructorReturn(this, _getPrototypeOf(Foo).apply(this, arguments));
|
||||
}
|
||||
|
||||
return Foo;
|
||||
}(Bar);
|
||||
3
packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs/options.json
vendored
Normal file
3
packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": [["transform-runtime", { "corejs": 2 }], "transform-classes"]
|
||||
}
|
||||
23
packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs/output.js
vendored
Normal file
23
packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs/output.js
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
var _classCallCheck = require("@babel/runtime-corejs2/helpers/classCallCheck");
|
||||
|
||||
var _possibleConstructorReturn = require("@babel/runtime-corejs2/helpers/possibleConstructorReturn");
|
||||
|
||||
var _getPrototypeOf = require("@babel/runtime-corejs2/helpers/getPrototypeOf");
|
||||
|
||||
var _inherits = require("@babel/runtime-corejs2/helpers/inherits");
|
||||
|
||||
let Foo =
|
||||
/*#__PURE__*/
|
||||
function (_Bar) {
|
||||
"use strict";
|
||||
|
||||
_inherits(Foo, _Bar);
|
||||
|
||||
function Foo() {
|
||||
_classCallCheck(this, Foo);
|
||||
|
||||
return _possibleConstructorReturn(this, _getPrototypeOf(Foo).apply(this, arguments));
|
||||
}
|
||||
|
||||
return Foo;
|
||||
}(Bar);
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"plugins": [["transform-runtime", { "useBuiltIns": true, "useESModules": true }], "transform-classes"]
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
var _classCallCheck = require("@babel/runtime/helpers/builtin/es6/classCallCheck");
|
||||
|
||||
var _possibleConstructorReturn = require("@babel/runtime/helpers/builtin/es6/possibleConstructorReturn");
|
||||
|
||||
var _getPrototypeOf = require("@babel/runtime/helpers/builtin/es6/getPrototypeOf");
|
||||
|
||||
var _inherits = require("@babel/runtime/helpers/builtin/es6/inherits");
|
||||
|
||||
let Foo =
|
||||
/*#__PURE__*/
|
||||
function (_Bar) {
|
||||
"use strict";
|
||||
|
||||
_inherits(Foo, _Bar);
|
||||
|
||||
function Foo() {
|
||||
_classCallCheck(this, Foo);
|
||||
|
||||
return _possibleConstructorReturn(this, _getPrototypeOf(Foo).apply(this, arguments));
|
||||
}
|
||||
|
||||
return Foo;
|
||||
}(Bar);
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"plugins": [["transform-runtime", { "useBuiltIns": true }], "transform-classes"]
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
var _classCallCheck = require("@babel/runtime/helpers/builtin/classCallCheck");
|
||||
|
||||
var _possibleConstructorReturn = require("@babel/runtime/helpers/builtin/possibleConstructorReturn");
|
||||
|
||||
var _getPrototypeOf = require("@babel/runtime/helpers/builtin/getPrototypeOf");
|
||||
|
||||
var _inherits = require("@babel/runtime/helpers/builtin/inherits");
|
||||
|
||||
let Foo =
|
||||
/*#__PURE__*/
|
||||
function (_Bar) {
|
||||
"use strict";
|
||||
|
||||
_inherits(Foo, _Bar);
|
||||
|
||||
function Foo() {
|
||||
_classCallCheck(this, Foo);
|
||||
|
||||
return _possibleConstructorReturn(this, _getPrototypeOf(Foo).apply(this, arguments));
|
||||
}
|
||||
|
||||
return Foo;
|
||||
}(Bar);
|
||||
@ -1,10 +1,10 @@
|
||||
var _classCallCheck = require("@babel/runtime/helpers/es6/classCallCheck");
|
||||
var _classCallCheck = require("@babel/runtime/helpers/esm/classCallCheck");
|
||||
|
||||
var _possibleConstructorReturn = require("@babel/runtime/helpers/es6/possibleConstructorReturn");
|
||||
var _possibleConstructorReturn = require("@babel/runtime/helpers/esm/possibleConstructorReturn");
|
||||
|
||||
var _getPrototypeOf = require("@babel/runtime/helpers/es6/getPrototypeOf");
|
||||
var _getPrototypeOf = require("@babel/runtime/helpers/esm/getPrototypeOf");
|
||||
|
||||
var _inherits = require("@babel/runtime/helpers/es6/inherits");
|
||||
var _inherits = require("@babel/runtime/helpers/esm/inherits");
|
||||
|
||||
let Foo =
|
||||
/*#__PURE__*/
|
||||
|
||||
3
packages/babel-runtime-corejs2/.npmignore
Normal file
3
packages/babel-runtime-corejs2/.npmignore
Normal file
@ -0,0 +1,3 @@
|
||||
src
|
||||
test
|
||||
*.log
|
||||
19
packages/babel-runtime-corejs2/README.md
Normal file
19
packages/babel-runtime-corejs2/README.md
Normal file
@ -0,0 +1,19 @@
|
||||
# @babel/runtime-corejs2
|
||||
|
||||
> babel's modular runtime helpers with core-js@2 polyfilling
|
||||
|
||||
See our website [@babel/runtime-corejs2](https://babeljs.io/docs/en/next/babel-runtime-corejs2.html) for more information.
|
||||
|
||||
## Install
|
||||
|
||||
Using npm:
|
||||
|
||||
```sh
|
||||
npm install --save-dev @babel/runtime-corejs2
|
||||
```
|
||||
|
||||
or using yarn:
|
||||
|
||||
```sh
|
||||
yarn add @babel/runtime-corejs2 --dev
|
||||
```
|
||||
@ -0,0 +1,9 @@
|
||||
var _Array$from = require("../core-js/array/from");
|
||||
|
||||
var _isIterable = require("../core-js/is-iterable");
|
||||
|
||||
function _iterableToArray(iter) {
|
||||
if (_isIterable(Object(iter)) || Object.prototype.toString.call(iter) === "[object Arguments]") return _Array$from(iter);
|
||||
}
|
||||
|
||||
module.exports = _iterableToArray;
|
||||
12
packages/babel-runtime-corejs2/package.json
Normal file
12
packages/babel-runtime-corejs2/package.json
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "@babel/runtime-corejs2",
|
||||
"version": "7.0.0-beta.51",
|
||||
"description": "babel's modular runtime helpers with core-js@2 polyfilling",
|
||||
"license": "MIT",
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-runtime-corejs2",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"dependencies": {
|
||||
"core-js": "^2.5.7",
|
||||
"regenerator-runtime": "^0.12.0"
|
||||
}
|
||||
}
|
||||
1
packages/babel-runtime-corejs2/regenerator/index.js
Normal file
1
packages/babel-runtime-corejs2/regenerator/index.js
Normal file
@ -0,0 +1 @@
|
||||
module.exports = require("regenerator-runtime");
|
||||
@ -1,4 +1,3 @@
|
||||
scripts
|
||||
src
|
||||
test
|
||||
*.log
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
# @babel/runtime
|
||||
|
||||
> babel selfContained runtime
|
||||
> babel's modular runtime helpers
|
||||
|
||||
See our website [@babel/runtime](https://babeljs.io/docs/en/next/babel-runtime.html) for more information.
|
||||
|
||||
|
||||
@ -1 +0,0 @@
|
||||
module.exports = require("core-js/library");
|
||||
@ -1,5 +0,0 @@
|
||||
function _iterableToArray(iter) {
|
||||
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
|
||||
}
|
||||
|
||||
module.exports = _iterableToArray;
|
||||
@ -1,9 +1,5 @@
|
||||
var _Array$from = require("../core-js/array/from");
|
||||
|
||||
var _isIterable = require("../core-js/is-iterable");
|
||||
|
||||
function _iterableToArray(iter) {
|
||||
if (_isIterable(Object(iter)) || Object.prototype.toString.call(iter) === "[object Arguments]") return _Array$from(iter);
|
||||
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
|
||||
}
|
||||
|
||||
module.exports = _iterableToArray;
|
||||
@ -1,19 +1,11 @@
|
||||
{
|
||||
"name": "@babel/runtime",
|
||||
"version": "7.0.0-beta.55",
|
||||
"description": "babel selfContained runtime",
|
||||
"description": "babel's modular runtime helpers",
|
||||
"license": "MIT",
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-runtime",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"dependencies": {
|
||||
"core-js": "^2.5.7",
|
||||
"regenerator-runtime": "^0.12.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "7.0.0-beta.55",
|
||||
"@babel/helpers": "7.0.0-beta.55",
|
||||
"@babel/plugin-transform-runtime": "7.0.0-beta.55",
|
||||
"@babel/preset-env": "7.0.0-beta.55",
|
||||
"@babel/types": "7.0.0-beta.55"
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,165 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
const outputFile = require("output-file-sync");
|
||||
const coreDefinitions = require("@babel/plugin-transform-runtime").definitions;
|
||||
const helpers = require("@babel/helpers");
|
||||
const babel = require("@babel/core");
|
||||
const t = require("@babel/types");
|
||||
|
||||
const paths = ["is-iterable", "get-iterator"];
|
||||
|
||||
Object.keys(coreDefinitions.builtins).forEach((key) => {
|
||||
const path = coreDefinitions.builtins[key];
|
||||
paths.push(path);
|
||||
});
|
||||
|
||||
Object.keys(coreDefinitions.methods).forEach((key) => {
|
||||
const props = coreDefinitions.methods[key];
|
||||
Object.keys(props).forEach((key2) => {
|
||||
const path = props[key2];
|
||||
paths.push(path);
|
||||
});
|
||||
});
|
||||
|
||||
paths.forEach(function(path) {
|
||||
writeFile(
|
||||
"core-js/" + path + ".js",
|
||||
defaultify(`require("core-js/library/fn/${path}")`)
|
||||
);
|
||||
});
|
||||
|
||||
function relative(filename) {
|
||||
return `${__dirname}/../${filename}`;
|
||||
}
|
||||
|
||||
function defaultify(name) {
|
||||
return `module.exports = ${name};`;
|
||||
}
|
||||
|
||||
function writeRootFile(filename, content) {
|
||||
filename = relative(filename);
|
||||
outputFile(filename, content);
|
||||
}
|
||||
|
||||
function writeFile(filename, content) {
|
||||
return writeRootFile(filename, content);
|
||||
}
|
||||
|
||||
function makeTransformOpts(modules, useBuiltIns) {
|
||||
const opts = {
|
||||
presets: [[require("@babel/preset-env"), { modules: false }]],
|
||||
|
||||
plugins: [
|
||||
[
|
||||
require("@babel/plugin-transform-runtime"),
|
||||
{ useBuiltIns, useESModules: modules === false },
|
||||
],
|
||||
],
|
||||
};
|
||||
return opts;
|
||||
}
|
||||
|
||||
function adjustImportPath(node, relativePath) {
|
||||
if (helpers.list.indexOf(node.value) >= 0) {
|
||||
node.value = `./${node.value}`;
|
||||
} else {
|
||||
node.value = node.value.replace(/^@babel\/runtime/, relativePath);
|
||||
}
|
||||
}
|
||||
|
||||
function buildRuntimeRewritePlugin(relativePath, helperName, dependencies) {
|
||||
return {
|
||||
pre(file) {
|
||||
const original = file.get("helperGenerator");
|
||||
file.set("helperGenerator", (name) => {
|
||||
// make sure that helpers won't insert circular references to themselves
|
||||
if (name === helperName) return false;
|
||||
|
||||
return original(name);
|
||||
});
|
||||
},
|
||||
visitor: {
|
||||
ImportDeclaration(path) {
|
||||
adjustImportPath(path.get("source").node, relativePath);
|
||||
},
|
||||
CallExpression(path) {
|
||||
if (
|
||||
!path.get("callee").isIdentifier({ name: "require" }) ||
|
||||
path.get("arguments").length !== 1 ||
|
||||
!path.get("arguments")[0].isStringLiteral()
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
// replace any reference to @babel/runtime and other helpers
|
||||
// with a relative path
|
||||
adjustImportPath(path.get("arguments")[0].node, relativePath);
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function buildRequireCall(id, dep) {
|
||||
return t.variableDeclaration("var", [
|
||||
t.variableDeclarator(
|
||||
id,
|
||||
t.callExpression(t.identifier("require"), [t.stringLiteral(dep)])
|
||||
)
|
||||
]);
|
||||
}
|
||||
|
||||
function buildHelper(helperName, modules, useBuiltIns) {
|
||||
const id =
|
||||
modules === "commonjs"
|
||||
? t.memberExpression(t.identifier("module"), t.identifier("exports"))
|
||||
: null;
|
||||
const sourceType = modules === "commonjs" ? "script" : "module";
|
||||
|
||||
const tree = t.program([], [], sourceType);
|
||||
const dependencies = {};
|
||||
let bindings = null;
|
||||
|
||||
if (modules === "commonjs") {
|
||||
bindings = [];
|
||||
for (const dep of helpers.getDependencies(helperName)) {
|
||||
const id = dependencies[dep] = t.identifier(t.toIdentifier(dep));
|
||||
tree.body.push(buildRequireCall(id, dep));
|
||||
bindings.push(id.name);
|
||||
}
|
||||
}
|
||||
|
||||
const helper = helpers.get(
|
||||
helperName,
|
||||
dep => dependencies[dep],
|
||||
id,
|
||||
bindings
|
||||
);
|
||||
tree.body.push.apply(tree.body, helper.nodes);
|
||||
|
||||
const transformOpts = makeTransformOpts(modules, useBuiltIns);
|
||||
|
||||
const relative = useBuiltIns ? "../.." : "..";
|
||||
|
||||
return babel.transformFromAst(tree, null, {
|
||||
presets: transformOpts.presets,
|
||||
plugins: transformOpts.plugins.concat([
|
||||
buildRuntimeRewritePlugin(
|
||||
modules === false ? `../${relative}` : relative,
|
||||
helperName
|
||||
),
|
||||
]),
|
||||
}).code;
|
||||
}
|
||||
|
||||
for (const modules of ["commonjs", false]) {
|
||||
for (const builtin of [false, true]) {
|
||||
const dirname = `helpers/${builtin ? "builtin/" : ""}${!modules ? "es6/" : ""}`;
|
||||
|
||||
for (const helperName of helpers.list) {
|
||||
writeFile(
|
||||
`${dirname}${helperName}.js`,
|
||||
buildHelper(helperName, modules, builtin)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user