update
This commit is contained in:
+5
@@ -4,8 +4,13 @@ Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = _default;
|
||||
exports.resolveFSPath = resolveFSPath;
|
||||
|
||||
function _default(moduleName, dirname, absoluteRuntime) {
|
||||
if (absoluteRuntime === false) return moduleName;
|
||||
resolveFSPath();
|
||||
}
|
||||
|
||||
function resolveFSPath() {
|
||||
throw new Error("The 'absoluteRuntime' option is not supported when using @babel/standalone.");
|
||||
}
|
||||
+19
-7
@@ -4,22 +4,30 @@ Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = _default;
|
||||
exports.resolveFSPath = resolveFSPath;
|
||||
|
||||
var _path = _interopRequireDefault(require("path"));
|
||||
var _path = require("path");
|
||||
|
||||
var _resolve = _interopRequireDefault(require("resolve"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
var _module = require("module");
|
||||
|
||||
function _default(moduleName, dirname, absoluteRuntime) {
|
||||
if (absoluteRuntime === false) return moduleName;
|
||||
return resolveAbsoluteRuntime(moduleName, _path.default.resolve(dirname, absoluteRuntime === true ? "." : absoluteRuntime));
|
||||
return resolveAbsoluteRuntime(moduleName, _path.resolve(dirname, absoluteRuntime === true ? "." : absoluteRuntime));
|
||||
}
|
||||
|
||||
function resolveAbsoluteRuntime(moduleName, dirname) {
|
||||
try {
|
||||
return _path.default.dirname(_resolve.default.sync(`${moduleName}/package.json`, {
|
||||
basedir: dirname
|
||||
return _path.dirname((((v, w) => (v = v.split("."), w = w.split("."), +v[0] > +w[0] || v[0] == w[0] && +v[1] >= +w[1]))(process.versions.node, "8.9") ? require.resolve : (r, {
|
||||
paths: [b]
|
||||
}, M = require("module")) => {
|
||||
let f = M._findPath(r, M._nodeModulePaths(b).concat(b));
|
||||
|
||||
if (f) return f;
|
||||
f = new Error(`Cannot resolve module '${r}'`);
|
||||
f.code = "MODULE_NOT_FOUND";
|
||||
throw f;
|
||||
})(`${moduleName}/package.json`, {
|
||||
paths: [dirname]
|
||||
})).replace(/\\/g, "/");
|
||||
} catch (err) {
|
||||
if (err.code !== "MODULE_NOT_FOUND") throw err;
|
||||
@@ -29,4 +37,8 @@ function resolveAbsoluteRuntime(moduleName, dirname) {
|
||||
dirname
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function resolveFSPath(path) {
|
||||
return require.resolve(path).replace(/\\/g, "/");
|
||||
}
|
||||
+3
-21
@@ -4,29 +4,11 @@ Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.hasMinVersion = hasMinVersion;
|
||||
exports.typeAnnotationToString = typeAnnotationToString;
|
||||
|
||||
var _semver = _interopRequireDefault(require("semver"));
|
||||
|
||||
var _core = require("@babel/core");
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
var _semver = require("semver");
|
||||
|
||||
function hasMinVersion(minVersion, runtimeVersion) {
|
||||
if (!runtimeVersion) return true;
|
||||
if (_semver.default.valid(runtimeVersion)) runtimeVersion = `^${runtimeVersion}`;
|
||||
return !_semver.default.intersects(`<${minVersion}`, runtimeVersion) && !_semver.default.intersects(`>=8.0.0`, runtimeVersion);
|
||||
}
|
||||
|
||||
function typeAnnotationToString(node) {
|
||||
switch (node.type) {
|
||||
case "GenericTypeAnnotation":
|
||||
if (_core.types.isIdentifier(node.id, {
|
||||
name: "Array"
|
||||
})) return "array";
|
||||
break;
|
||||
|
||||
case "StringTypeAnnotation":
|
||||
return "string";
|
||||
}
|
||||
if (_semver.valid(runtimeVersion)) runtimeVersion = `^${runtimeVersion}`;
|
||||
return !_semver.intersects(`<${minVersion}`, runtimeVersion) && !_semver.intersects(`>=8.0.0`, runtimeVersion);
|
||||
}
|
||||
+88
-188
@@ -11,18 +11,23 @@ var _helperModuleImports = require("@babel/helper-module-imports");
|
||||
|
||||
var _core = require("@babel/core");
|
||||
|
||||
var _runtimeCorejs2Definitions = _interopRequireDefault(require("./runtime-corejs2-definitions"));
|
||||
|
||||
var _runtimeCorejs3Definitions = _interopRequireDefault(require("./runtime-corejs3-definitions"));
|
||||
|
||||
var _helpers = require("./helpers");
|
||||
|
||||
var _getRuntimePath = _interopRequireDefault(require("./get-runtime-path"));
|
||||
var _getRuntimePath = require("./get-runtime-path");
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
var _babelPluginPolyfillCorejs = require("babel-plugin-polyfill-corejs2");
|
||||
|
||||
var _babelPluginPolyfillCorejs2 = require("babel-plugin-polyfill-corejs3");
|
||||
|
||||
var _babelPluginPolyfillRegenerator = require("babel-plugin-polyfill-regenerator");
|
||||
|
||||
const pluginCorejs2 = _babelPluginPolyfillCorejs.default || _babelPluginPolyfillCorejs;
|
||||
const pluginCorejs3 = _babelPluginPolyfillCorejs2.default || _babelPluginPolyfillCorejs2;
|
||||
const pluginRegenerator = _babelPluginPolyfillRegenerator.default || _babelPluginPolyfillRegenerator;
|
||||
const pluginsCompat = "#__secret_key__@babel/runtime__compatibility";
|
||||
|
||||
function supportsStaticESM(caller) {
|
||||
return !!(caller == null ? void 0 : caller.supportsStaticESM);
|
||||
return !!(caller != null && caller.supportsStaticESM);
|
||||
}
|
||||
|
||||
var _default = (0, _helperPluginUtils.declare)((api, options, dirname) => {
|
||||
@@ -75,45 +80,17 @@ var _default = (0, _helperPluginUtils.declare)((api, options, dirname) => {
|
||||
throw new Error(`The 'version' option must be a version string.`);
|
||||
}
|
||||
|
||||
{
|
||||
const DUAL_MODE_RUNTIME = "7.13.0";
|
||||
var supportsCJSDefault = (0, _helpers.hasMinVersion)(DUAL_MODE_RUNTIME, runtimeVersion);
|
||||
}
|
||||
|
||||
function has(obj, key) {
|
||||
return Object.prototype.hasOwnProperty.call(obj, key);
|
||||
}
|
||||
|
||||
function hasMapping(methods, name) {
|
||||
return has(methods, name) && (proposals || methods[name].stable);
|
||||
}
|
||||
|
||||
function hasStaticMapping(object, method) {
|
||||
return has(StaticProperties, object) && hasMapping(StaticProperties[object], method);
|
||||
}
|
||||
|
||||
function isNamespaced(path) {
|
||||
const binding = path.scope.getBinding(path.node.name);
|
||||
if (!binding) return false;
|
||||
return binding.path.isImportNamespaceSpecifier();
|
||||
}
|
||||
|
||||
function maybeNeedsPolyfill(path, methods, name) {
|
||||
if (isNamespaced(path.get("object"))) return false;
|
||||
if (!methods[name].types) return true;
|
||||
const typeAnnotation = path.get("object").getTypeAnnotation();
|
||||
const type = (0, _helpers.typeAnnotationToString)(typeAnnotation);
|
||||
if (!type) return true;
|
||||
return methods[name].types.some(name => name === type);
|
||||
}
|
||||
|
||||
function resolvePropertyName(path, computed) {
|
||||
const {
|
||||
node
|
||||
} = path;
|
||||
if (!computed) return node.name;
|
||||
if (path.isStringLiteral()) return node.value;
|
||||
const result = path.evaluate();
|
||||
return result.value;
|
||||
}
|
||||
|
||||
if (has(options, "useBuiltIns")) {
|
||||
if (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 to polyfill with `core-js` via @babel/runtime.");
|
||||
@@ -121,7 +98,7 @@ var _default = (0, _helperPluginUtils.declare)((api, options, dirname) => {
|
||||
}
|
||||
|
||||
if (has(options, "polyfill")) {
|
||||
if (options.polyfill === false) {
|
||||
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 to polyfill with `core-js` via @babel/runtime.");
|
||||
@@ -135,36 +112,85 @@ var _default = (0, _helperPluginUtils.declare)((api, options, dirname) => {
|
||||
const esModules = useESModules === "auto" ? api.caller(supportsStaticESM) : useESModules;
|
||||
const injectCoreJS2 = corejsVersion === 2;
|
||||
const injectCoreJS3 = corejsVersion === 3;
|
||||
const injectCoreJS = corejsVersion !== false;
|
||||
const moduleName = injectCoreJS3 ? "@babel/runtime-corejs3" : injectCoreJS2 ? "@babel/runtime-corejs2" : "@babel/runtime";
|
||||
const corejsRoot = injectCoreJS3 && !proposals ? "core-js-stable" : "core-js";
|
||||
const {
|
||||
BuiltIns,
|
||||
StaticProperties,
|
||||
InstanceProperties
|
||||
} = (injectCoreJS2 ? _runtimeCorejs2Definitions.default : _runtimeCorejs3Definitions.default)(runtimeVersion);
|
||||
const HEADER_HELPERS = ["interopRequireWildcard", "interopRequireDefault"];
|
||||
const modulePath = (0, _getRuntimePath.default)(moduleName, dirname, absoluteRuntime);
|
||||
|
||||
function createCorejsPlgin(plugin, options, regeneratorPlugin) {
|
||||
return (api, _, filename) => {
|
||||
return Object.assign({}, plugin(api, options, filename), {
|
||||
inherits: regeneratorPlugin
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function createRegeneratorPlugin(options) {
|
||||
if (!useRuntimeRegenerator) return undefined;
|
||||
return (api, _, filename) => {
|
||||
return pluginRegenerator(api, options, filename);
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
name: "transform-runtime",
|
||||
inherits: injectCoreJS2 ? createCorejsPlgin(pluginCorejs2, {
|
||||
method: "usage-pure",
|
||||
absoluteImports: absoluteRuntime ? modulePath : false,
|
||||
[pluginsCompat]: {
|
||||
runtimeVersion,
|
||||
useBabelRuntime: modulePath,
|
||||
ext: ""
|
||||
}
|
||||
}, createRegeneratorPlugin({
|
||||
method: "usage-pure",
|
||||
absoluteImports: absoluteRuntime ? modulePath : false,
|
||||
[pluginsCompat]: {
|
||||
useBabelRuntime: modulePath
|
||||
}
|
||||
})) : injectCoreJS3 ? createCorejsPlgin(pluginCorejs3, {
|
||||
method: "usage-pure",
|
||||
version: 3,
|
||||
proposals,
|
||||
absoluteImports: absoluteRuntime ? modulePath : false,
|
||||
[pluginsCompat]: {
|
||||
useBabelRuntime: modulePath,
|
||||
ext: ""
|
||||
}
|
||||
}, createRegeneratorPlugin({
|
||||
method: "usage-pure",
|
||||
absoluteImports: absoluteRuntime ? modulePath : false,
|
||||
[pluginsCompat]: {
|
||||
useBabelRuntime: modulePath
|
||||
}
|
||||
})) : createRegeneratorPlugin({
|
||||
method: "usage-pure",
|
||||
absoluteImports: absoluteRuntime ? modulePath : false,
|
||||
[pluginsCompat]: {
|
||||
useBabelRuntime: modulePath
|
||||
}
|
||||
}),
|
||||
|
||||
pre(file) {
|
||||
if (useRuntimeHelpers) {
|
||||
file.set("helperGenerator", name => {
|
||||
if (file.availableHelper && !file.availableHelper(name, runtimeVersion)) {
|
||||
return;
|
||||
if (!useRuntimeHelpers) return;
|
||||
file.set("helperGenerator", name => {
|
||||
if (!(file.availableHelper != null && file.availableHelper(name, runtimeVersion))) {
|
||||
if (name === "regeneratorRuntime") {
|
||||
return _core.types.arrowFunctionExpression([], _core.types.identifier("regeneratorRuntime"));
|
||||
}
|
||||
|
||||
const isInteropHelper = HEADER_HELPERS.indexOf(name) !== -1;
|
||||
const blockHoist = isInteropHelper && !(0, _helperModuleImports.isModule)(file.path) ? 4 : undefined;
|
||||
const helpersDir = esModules && file.path.node.sourceType === "module" ? "helpers/esm" : "helpers";
|
||||
return this.addDefaultImport(`${modulePath}/${helpersDir}/${name}`, name, blockHoist);
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const isInteropHelper = HEADER_HELPERS.indexOf(name) !== -1;
|
||||
const blockHoist = isInteropHelper && !(0, _helperModuleImports.isModule)(file.path) ? 4 : undefined;
|
||||
const helpersDir = esModules && file.path.node.sourceType === "module" ? "helpers/esm" : "helpers";
|
||||
let helperPath = `${modulePath}/${helpersDir}/${name}`;
|
||||
if (absoluteRuntime) helperPath = (0, _getRuntimePath.resolveFSPath)(helperPath);
|
||||
return addDefaultImport(helperPath, name, blockHoist, true);
|
||||
});
|
||||
const cache = new Map();
|
||||
|
||||
this.addDefaultImport = (source, nameHint, blockHoist) => {
|
||||
function addDefaultImport(source, nameHint, blockHoist, isHelper = false) {
|
||||
const cacheKey = (0, _helperModuleImports.isModule)(file.path);
|
||||
const key = `${source}:${nameHint}:${cacheKey || ""}`;
|
||||
let cached = cache.get(key);
|
||||
@@ -173,7 +199,7 @@ var _default = (0, _helperPluginUtils.declare)((api, options, dirname) => {
|
||||
cached = _core.types.cloneNode(cached);
|
||||
} else {
|
||||
cached = (0, _helperModuleImports.addDefault)(file.path, source, {
|
||||
importedInterop: "uncompiled",
|
||||
importedInterop: isHelper && supportsCJSDefault ? "compiled" : "uncompiled",
|
||||
nameHint,
|
||||
blockHoist
|
||||
});
|
||||
@@ -181,135 +207,9 @@ var _default = (0, _helperPluginUtils.declare)((api, options, dirname) => {
|
||||
}
|
||||
|
||||
return cached;
|
||||
};
|
||||
},
|
||||
|
||||
visitor: {
|
||||
ReferencedIdentifier(path) {
|
||||
const {
|
||||
node,
|
||||
parent,
|
||||
scope
|
||||
} = path;
|
||||
const {
|
||||
name
|
||||
} = node;
|
||||
|
||||
if (name === "regeneratorRuntime" && useRuntimeRegenerator) {
|
||||
path.replaceWith(this.addDefaultImport(`${modulePath}/regenerator`, "regeneratorRuntime"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!injectCoreJS) return;
|
||||
if (_core.types.isMemberExpression(parent)) return;
|
||||
if (!hasMapping(BuiltIns, name)) return;
|
||||
if (scope.getBindingIdentifier(name)) return;
|
||||
path.replaceWith(this.addDefaultImport(`${modulePath}/${corejsRoot}/${BuiltIns[name].path}`, name));
|
||||
},
|
||||
|
||||
CallExpression(path) {
|
||||
if (!injectCoreJS) return;
|
||||
const {
|
||||
node
|
||||
} = path;
|
||||
const {
|
||||
callee
|
||||
} = node;
|
||||
if (!_core.types.isMemberExpression(callee)) return;
|
||||
const {
|
||||
object
|
||||
} = callee;
|
||||
const propertyName = resolvePropertyName(path.get("callee.property"), callee.computed);
|
||||
|
||||
if (injectCoreJS3 && !hasStaticMapping(object.name, propertyName)) {
|
||||
if (hasMapping(InstanceProperties, propertyName) && maybeNeedsPolyfill(path.get("callee"), InstanceProperties, propertyName)) {
|
||||
let context1, context2;
|
||||
|
||||
if (_core.types.isIdentifier(object)) {
|
||||
context1 = object;
|
||||
context2 = _core.types.cloneNode(object);
|
||||
} else {
|
||||
context1 = path.scope.generateDeclaredUidIdentifier("context");
|
||||
context2 = _core.types.assignmentExpression("=", context1, object);
|
||||
}
|
||||
|
||||
node.callee = _core.types.memberExpression(_core.types.callExpression(this.addDefaultImport(`${moduleName}/${corejsRoot}/instance/${InstanceProperties[propertyName].path}`, `${propertyName}InstanceProperty`), [context2]), _core.types.identifier("call"));
|
||||
node.arguments.unshift(context1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (node.arguments.length) return;
|
||||
if (!callee.computed) return;
|
||||
|
||||
if (!path.get("callee.property").matchesPattern("Symbol.iterator")) {
|
||||
return;
|
||||
}
|
||||
|
||||
path.replaceWith(_core.types.callExpression(this.addDefaultImport(`${modulePath}/core-js/get-iterator`, "getIterator"), [object]));
|
||||
},
|
||||
|
||||
BinaryExpression(path) {
|
||||
if (!injectCoreJS) return;
|
||||
if (path.node.operator !== "in") return;
|
||||
if (!path.get("left").matchesPattern("Symbol.iterator")) return;
|
||||
path.replaceWith(_core.types.callExpression(this.addDefaultImport(`${modulePath}/core-js/is-iterable`, "isIterable"), [path.node.right]));
|
||||
},
|
||||
|
||||
MemberExpression: {
|
||||
enter(path) {
|
||||
if (!injectCoreJS) return;
|
||||
if (!path.isReferenced()) return;
|
||||
if (path.parentPath.isUnaryExpression({
|
||||
operator: "delete"
|
||||
})) return;
|
||||
const {
|
||||
node
|
||||
} = path;
|
||||
const {
|
||||
object
|
||||
} = node;
|
||||
if (!_core.types.isReferenced(object, node)) return;
|
||||
|
||||
if (!injectCoreJS2 && node.computed && path.get("property").matchesPattern("Symbol.iterator")) {
|
||||
path.replaceWith(_core.types.callExpression(this.addDefaultImport(`${moduleName}/core-js/get-iterator-method`, "getIteratorMethod"), [object]));
|
||||
return;
|
||||
}
|
||||
|
||||
const objectName = object.name;
|
||||
const propertyName = resolvePropertyName(path.get("property"), node.computed);
|
||||
|
||||
if (path.scope.getBindingIdentifier(objectName) || !hasStaticMapping(objectName, propertyName)) {
|
||||
if (injectCoreJS3 && hasMapping(InstanceProperties, propertyName) && maybeNeedsPolyfill(path, InstanceProperties, propertyName)) {
|
||||
path.replaceWith(_core.types.callExpression(this.addDefaultImport(`${moduleName}/${corejsRoot}/instance/${InstanceProperties[propertyName].path}`, `${propertyName}InstanceProperty`), [object]));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
path.replaceWith(this.addDefaultImport(`${modulePath}/${corejsRoot}/${StaticProperties[objectName][propertyName].path}`, `${objectName}$${propertyName}`));
|
||||
},
|
||||
|
||||
exit(path) {
|
||||
if (!injectCoreJS) return;
|
||||
if (!path.isReferenced()) return;
|
||||
if (path.node.computed) return;
|
||||
const {
|
||||
node
|
||||
} = path;
|
||||
const {
|
||||
object
|
||||
} = node;
|
||||
const {
|
||||
name
|
||||
} = object;
|
||||
if (!hasMapping(BuiltIns, name)) return;
|
||||
if (path.scope.getBindingIdentifier(name)) return;
|
||||
path.replaceWith(_core.types.memberExpression(this.addDefaultImport(`${modulePath}/${corejsRoot}/${BuiltIns[name].path}`, name), node.property));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user