@babel/plugin-transform-runtime
Advanced tools
Comparing version
@@ -7,6 +7,11 @@ "use strict"; | ||
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."); | ||
} | ||
} | ||
//# sourceMappingURL=browser.js.map |
@@ -7,18 +7,21 @@ "use strict"; | ||
exports.default = _default; | ||
var _path = _interopRequireDefault(require("path")); | ||
var _resolve = _interopRequireDefault(require("resolve")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.resolveFSPath = resolveFSPath; | ||
var _path = require("path"); | ||
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, "/"); | ||
@@ -33,2 +36,7 @@ } catch (err) { | ||
} | ||
} | ||
} | ||
function resolveFSPath(path) { | ||
return require.resolve(path).replace(/\\/g, "/"); | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -7,27 +7,9 @@ "use strict"; | ||
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); | ||
if (_semver.valid(runtimeVersion)) runtimeVersion = `^${runtimeVersion}`; | ||
return !_semver.intersects(`<${minVersion}`, runtimeVersion) && !_semver.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"; | ||
} | ||
} | ||
//# sourceMappingURL=helpers.js.map |
272
lib/index.js
@@ -7,29 +7,15 @@ "use strict"; | ||
exports.default = void 0; | ||
var _helperPluginUtils = require("@babel/helper-plugin-utils"); | ||
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")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _getRuntimePath = require("./get-runtime-path"); | ||
var _polyfills = require("./polyfills"); | ||
function supportsStaticESM(caller) { | ||
return !!(caller == null ? void 0 : caller.supportsStaticESM); | ||
return !!(caller != null && caller.supportsStaticESM); | ||
} | ||
var _default = (0, _helperPluginUtils.declare)((api, options, dirname) => { | ||
api.assertVersion(7); | ||
const { | ||
corejs, | ||
helpers: useRuntimeHelpers = true, | ||
regenerator: useRuntimeRegenerator = true, | ||
useESModules = false, | ||
@@ -39,81 +25,23 @@ version: runtimeVersion = "7.0.0-beta.0", | ||
} = options; | ||
let proposals = false; | ||
let rawVersion; | ||
if (typeof corejs === "object" && corejs !== null) { | ||
rawVersion = corejs.version; | ||
proposals = Boolean(corejs.proposals); | ||
} else { | ||
rawVersion = corejs; | ||
} | ||
const corejsVersion = rawVersion ? Number(rawVersion) : false; | ||
if (![false, 2, 3].includes(corejsVersion)) { | ||
throw new Error(`The \`core-js\` version must be false, 2 or 3, but got ${JSON.stringify(rawVersion)}.`); | ||
} | ||
if (proposals && (!corejsVersion || corejsVersion < 3)) { | ||
throw new Error("The 'proposals' option is only supported when using 'corejs: 3'"); | ||
} | ||
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" && useESModules !== "auto") { | ||
throw new Error("The 'useESModules' option must be undefined, or a boolean, or 'auto'."); | ||
} | ||
if (typeof absoluteRuntime !== "boolean" && typeof absoluteRuntime !== "string") { | ||
throw new Error("The 'absoluteRuntime' option must be undefined, a boolean, or a string."); | ||
} | ||
if (typeof runtimeVersion !== "string") { | ||
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."); | ||
@@ -124,5 +52,4 @@ } else { | ||
} | ||
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."); | ||
@@ -133,44 +60,36 @@ } else { | ||
} | ||
if (has(options, "moduleName")) { | ||
throw new Error("The 'moduleName' option has been removed. @babel/transform-runtime " + "no longer supports arbitrary runtimes. If you were using this to " + "set an absolute path for Babel's standard runtimes, please use the " + "'absoluteRuntime' option."); | ||
} | ||
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); | ||
return { | ||
name: "transform-runtime", | ||
inherits: (0, _polyfills.createBasePolyfillsPlugin)(options, runtimeVersion, absoluteRuntime), | ||
pre(file) { | ||
if (useRuntimeHelpers) { | ||
file.set("helperGenerator", name => { | ||
if (file.availableHelper && !file.availableHelper(name, runtimeVersion)) { | ||
if (!useRuntimeHelpers) return; | ||
let modulePath; | ||
file.set("helperGenerator", name => { | ||
var _modulePath, _file$get; | ||
(_modulePath = modulePath) != null ? _modulePath : modulePath = (0, _getRuntimePath.default)((_file$get = file.get("runtimeHelpersModuleName")) != null ? _file$get : "@babel/runtime", dirname, absoluteRuntime); | ||
{ | ||
if (!(file.availableHelper != null && file.availableHelper(name, runtimeVersion))) { | ||
if (name === "regeneratorRuntime") { | ||
return _core.types.arrowFunctionExpression([], _core.types.identifier("regeneratorRuntime")); | ||
} | ||
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"; | ||
return this.addDefaultImport(`${modulePath}/${helpersDir}/${name}`, name, blockHoist); | ||
}); | ||
} | ||
} | ||
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); | ||
if (cached) { | ||
@@ -180,3 +99,3 @@ cached = _core.types.cloneNode(cached); | ||
cached = (0, _helperModuleImports.addDefault)(file.path, source, { | ||
importedInterop: "uncompiled", | ||
importedInterop: isHelper && supportsCJSDefault ? "compiled" : "uncompiled", | ||
nameHint, | ||
@@ -187,131 +106,3 @@ blockHoist | ||
} | ||
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("=", _core.types.cloneNode(context1), object); | ||
} | ||
node.callee = _core.types.memberExpression(_core.types.callExpression(this.addDefaultImport(`${modulePath}/${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(`${modulePath}/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(`${modulePath}/${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)); | ||
} | ||
} | ||
@@ -321,3 +112,4 @@ } | ||
}); | ||
exports.default = _default; | ||
exports.default = _default; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@babel/plugin-transform-runtime", | ||
"version": "7.11.0", | ||
"version": "7.22.4", | ||
"description": "Externalise references to helpers and builtins, automatically polyfilling your code without polluting globals", | ||
@@ -14,3 +14,3 @@ "repository": { | ||
}, | ||
"main": "lib/index.js", | ||
"main": "./lib/index.js", | ||
"keywords": [ | ||
@@ -21,9 +21,11 @@ "babel-plugin" | ||
"./lib/get-runtime-path/index.js": "./lib/get-runtime-path/browser.js", | ||
"./src/get-runtime-path/index.js": "./src/get-runtime-path/browser.js" | ||
"./src/get-runtime-path/index.ts": "./src/get-runtime-path/browser.ts" | ||
}, | ||
"dependencies": { | ||
"@babel/helper-module-imports": "^7.10.4", | ||
"@babel/helper-plugin-utils": "^7.10.4", | ||
"resolve": "^1.8.1", | ||
"semver": "^5.5.1" | ||
"@babel/helper-module-imports": "^7.21.4", | ||
"@babel/helper-plugin-utils": "^7.21.5", | ||
"babel-plugin-polyfill-corejs2": "^0.4.3", | ||
"babel-plugin-polyfill-corejs3": "^0.8.1", | ||
"babel-plugin-polyfill-regenerator": "^0.5.0", | ||
"semver": "^6.3.0" | ||
}, | ||
@@ -34,12 +36,18 @@ "peerDependencies": { | ||
"devDependencies": { | ||
"@babel/core": "^7.11.0", | ||
"@babel/helper-plugin-test-runner": "^7.10.4", | ||
"@babel/helpers": "^7.10.4", | ||
"@babel/plugin-transform-typeof-symbol": "^7.10.4", | ||
"@babel/preset-env": "^7.11.0", | ||
"@babel/runtime": "^7.11.0", | ||
"@babel/runtime-corejs3": "^7.11.0", | ||
"@babel/template": "^7.10.4", | ||
"@babel/types": "^7.11.0" | ||
} | ||
} | ||
"@babel/core": "^7.22.1", | ||
"@babel/helper-plugin-test-runner": "^7.18.6", | ||
"@babel/helpers": "^7.22.3", | ||
"@babel/preset-env": "^7.22.4", | ||
"@babel/runtime": "^7.22.3", | ||
"@babel/runtime-corejs3": "^7.22.3", | ||
"@babel/template": "^7.21.9", | ||
"@babel/types": "^7.22.4", | ||
"make-dir": "^2.1.0" | ||
}, | ||
"homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-runtime", | ||
"engines": { | ||
"node": ">=6.9.0" | ||
}, | ||
"author": "The Babel Team (https://babel.dev/team)", | ||
"type": "commonjs" | ||
} |
@@ -5,3 +5,3 @@ # @babel/plugin-transform-runtime | ||
See our website [@babel/plugin-transform-runtime](https://babeljs.io/docs/en/next/babel-plugin-transform-runtime.html) for more information. | ||
See our website [@babel/plugin-transform-runtime](https://babeljs.io/docs/en/babel-plugin-transform-runtime) for more information. | ||
@@ -8,0 +8,0 @@ ## Install |
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
15
66.67%1
-50%0
-100%38582
-3.87%7
40%281
-80.17%3
200%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
Updated