UNPKG

@babel/plugin-transform-runtime

Version:

Externalise references to helpers and builtins, automatically polyfilling your code without polluting globals

82 lines (79 loc) 3.8 kB
import { declare } from '@babel/helper-plugin-utils'; import { isModule, addDefault } from '@babel/helper-module-imports'; import { types } from '@babel/core'; import getRuntimePath, { resolveFSPath } from '#get-runtime-path'; const index = declare((api, options, dirname) => { api.assertVersion("^7.0.0-0 || ^8.0.0"); const { version: runtimeVersion = "8.0.0-beta.0", absoluteRuntime = false, moduleName = null } = options; 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.`); } if (moduleName !== null && typeof moduleName !== "string") { throw new Error("The 'moduleName' option must be null or a string."); } if (Object.hasOwn(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."); } } if (Object.hasOwn(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 to polyfill with `core-js` via @babel/runtime."); } } if (Object.hasOwn(options, "regenerator")) { throw new Error("The 'regenerator' option has been removed. The generators transform " + "no longers relies on a 'regeneratorRuntime' global. " + "If you still need to replace imports to the 'regeneratorRuntime' " + "global, you can use babel-plugin-polyfill-regenerator."); } if (Object.hasOwn(options, "useESModules")) { throw new Error("The 'useESModules' option has been removed. @babel/runtime now uses " + "package.json#exports to support both CommonJS and ESM helpers."); } if (Object.hasOwn(options, "helpers")) { throw new Error("The 'helpers' option has been removed. " + "Remove the plugin from your config if " + "you want to disable helpers import injection."); } const HEADER_HELPERS = new Set(["interopRequireWildcard", "interopRequireDefault"]); return { name: "transform-runtime", inherits: undefined, pre(file) { let modulePath; file.set("helperGenerator", name => { modulePath ??= getRuntimePath(moduleName ?? file.get("runtimeHelpersModuleName") ?? "@babel/runtime", dirname, absoluteRuntime); if (!file.availableHelper(name, runtimeVersion)) return; const blockHoist = HEADER_HELPERS.has(name) && !isModule(file.path) ? 4 : undefined; let helperPath = `${modulePath}/helpers/${name}`; if (absoluteRuntime) helperPath = resolveFSPath(helperPath); return addDefaultImport(helperPath, name, blockHoist, true); }); const cache = new Map(); function addDefaultImport(source, nameHint, blockHoist, isHelper = false) { const cacheKey = isModule(file.path); const key = `${source}:${nameHint}:${cacheKey || ""}`; let cached = cache.get(key); if (cached) { cached = types.cloneNode(cached); } else { cached = addDefault(file.path, source, { importedInterop: isHelper ? "compiled" : "uncompiled", nameHint, blockHoist }); cache.set(key, cached); } return cached; } } }; }); export { index as default }; //# sourceMappingURL=index.js.map