UNPKG

7.51 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _helperPluginUtils = require("@babel/helper-plugin-utils");
9
10var _helperModuleImports = require("@babel/helper-module-imports");
11
12var _core = require("@babel/core");
13
14var _helpers = require("./helpers");
15
16var _getRuntimePath = require("./get-runtime-path");
17
18var _babelPluginPolyfillCorejs = require("babel-plugin-polyfill-corejs2");
19
20var _babelPluginPolyfillCorejs2 = require("babel-plugin-polyfill-corejs3");
21
22var _babelPluginPolyfillRegenerator = require("babel-plugin-polyfill-regenerator");
23
24const pluginCorejs2 = _babelPluginPolyfillCorejs.default || _babelPluginPolyfillCorejs;
25const pluginCorejs3 = _babelPluginPolyfillCorejs2.default || _babelPluginPolyfillCorejs2;
26const pluginRegenerator = _babelPluginPolyfillRegenerator.default || _babelPluginPolyfillRegenerator;
27const pluginsCompat = "#__secret_key__@babel/runtime__compatibility";
28
29function supportsStaticESM(caller) {
30 return !!(caller != null && caller.supportsStaticESM);
31}
32
33var _default = (0, _helperPluginUtils.declare)((api, options, dirname) => {
34 api.assertVersion(7);
35 const {
36 corejs,
37 helpers: useRuntimeHelpers = true,
38 regenerator: useRuntimeRegenerator = true,
39 useESModules = false,
40 version: runtimeVersion = "7.0.0-beta.0",
41 absoluteRuntime = false
42 } = options;
43 let proposals = false;
44 let rawVersion;
45
46 if (typeof corejs === "object" && corejs !== null) {
47 rawVersion = corejs.version;
48 proposals = Boolean(corejs.proposals);
49 } else {
50 rawVersion = corejs;
51 }
52
53 const corejsVersion = rawVersion ? Number(rawVersion) : false;
54
55 if (![false, 2, 3].includes(corejsVersion)) {
56 throw new Error(`The \`core-js\` version must be false, 2 or 3, but got ${JSON.stringify(rawVersion)}.`);
57 }
58
59 if (proposals && (!corejsVersion || corejsVersion < 3)) {
60 throw new Error("The 'proposals' option is only supported when using 'corejs: 3'");
61 }
62
63 if (typeof useRuntimeRegenerator !== "boolean") {
64 throw new Error("The 'regenerator' option must be undefined, or a boolean.");
65 }
66
67 if (typeof useRuntimeHelpers !== "boolean") {
68 throw new Error("The 'helpers' option must be undefined, or a boolean.");
69 }
70
71 if (typeof useESModules !== "boolean" && useESModules !== "auto") {
72 throw new Error("The 'useESModules' option must be undefined, or a boolean, or 'auto'.");
73 }
74
75 if (typeof absoluteRuntime !== "boolean" && typeof absoluteRuntime !== "string") {
76 throw new Error("The 'absoluteRuntime' option must be undefined, a boolean, or a string.");
77 }
78
79 if (typeof runtimeVersion !== "string") {
80 throw new Error(`The 'version' option must be a version string.`);
81 }
82
83 {
84 const DUAL_MODE_RUNTIME = "7.13.0";
85 var supportsCJSDefault = (0, _helpers.hasMinVersion)(DUAL_MODE_RUNTIME, runtimeVersion);
86 }
87
88 function has(obj, key) {
89 return Object.prototype.hasOwnProperty.call(obj, key);
90 }
91
92 if (has(options, "useBuiltIns")) {
93 if (options.useBuiltIns) {
94 throw new Error("The 'useBuiltIns' option has been removed. The @babel/runtime " + "module now uses builtins by default.");
95 } else {
96 throw new Error("The 'useBuiltIns' option has been removed. Use the 'corejs'" + "option to polyfill with `core-js` via @babel/runtime.");
97 }
98 }
99
100 if (has(options, "polyfill")) {
101 if (options.polyfill === false) {
102 throw new Error("The 'polyfill' option has been removed. The @babel/runtime " + "module now skips polyfilling by default.");
103 } else {
104 throw new Error("The 'polyfill' option has been removed. Use the 'corejs'" + "option to polyfill with `core-js` via @babel/runtime.");
105 }
106 }
107
108 if (has(options, "moduleName")) {
109 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.");
110 }
111
112 const esModules = useESModules === "auto" ? api.caller(supportsStaticESM) : useESModules;
113 const injectCoreJS2 = corejsVersion === 2;
114 const injectCoreJS3 = corejsVersion === 3;
115 const moduleName = injectCoreJS3 ? "@babel/runtime-corejs3" : injectCoreJS2 ? "@babel/runtime-corejs2" : "@babel/runtime";
116 const HEADER_HELPERS = ["interopRequireWildcard", "interopRequireDefault"];
117 const modulePath = (0, _getRuntimePath.default)(moduleName, dirname, absoluteRuntime);
118
119 function createCorejsPlgin(plugin, options, regeneratorPlugin) {
120 return (api, _, filename) => {
121 return Object.assign({}, plugin(api, options, filename), {
122 inherits: regeneratorPlugin
123 });
124 };
125 }
126
127 function createRegeneratorPlugin(options) {
128 if (!useRuntimeRegenerator) return undefined;
129 return (api, _, filename) => {
130 return pluginRegenerator(api, options, filename);
131 };
132 }
133
134 return {
135 name: "transform-runtime",
136 inherits: injectCoreJS2 ? createCorejsPlgin(pluginCorejs2, {
137 method: "usage-pure",
138 absoluteImports: absoluteRuntime ? modulePath : false,
139 [pluginsCompat]: {
140 runtimeVersion,
141 useBabelRuntime: modulePath,
142 ext: ""
143 }
144 }, createRegeneratorPlugin({
145 method: "usage-pure",
146 absoluteImports: absoluteRuntime ? modulePath : false,
147 [pluginsCompat]: {
148 useBabelRuntime: modulePath
149 }
150 })) : injectCoreJS3 ? createCorejsPlgin(pluginCorejs3, {
151 method: "usage-pure",
152 version: 3,
153 proposals,
154 absoluteImports: absoluteRuntime ? modulePath : false,
155 [pluginsCompat]: {
156 useBabelRuntime: modulePath,
157 ext: ""
158 }
159 }, createRegeneratorPlugin({
160 method: "usage-pure",
161 absoluteImports: absoluteRuntime ? modulePath : false,
162 [pluginsCompat]: {
163 useBabelRuntime: modulePath
164 }
165 })) : createRegeneratorPlugin({
166 method: "usage-pure",
167 absoluteImports: absoluteRuntime ? modulePath : false,
168 [pluginsCompat]: {
169 useBabelRuntime: modulePath
170 }
171 }),
172
173 pre(file) {
174 if (!useRuntimeHelpers) return;
175 file.set("helperGenerator", name => {
176 if (file.availableHelper && !file.availableHelper(name, runtimeVersion)) {
177 return;
178 }
179
180 const isInteropHelper = HEADER_HELPERS.indexOf(name) !== -1;
181 const blockHoist = isInteropHelper && !(0, _helperModuleImports.isModule)(file.path) ? 4 : undefined;
182 const helpersDir = esModules && file.path.node.sourceType === "module" ? "helpers/esm" : "helpers";
183 let helperPath = `${modulePath}/${helpersDir}/${name}`;
184 if (absoluteRuntime) helperPath = (0, _getRuntimePath.resolveFSPath)(helperPath);
185 return addDefaultImport(helperPath, name, blockHoist, true);
186 });
187 const cache = new Map();
188
189 function addDefaultImport(source, nameHint, blockHoist, isHelper = false) {
190 const cacheKey = (0, _helperModuleImports.isModule)(file.path);
191 const key = `${source}:${nameHint}:${cacheKey || ""}`;
192 let cached = cache.get(key);
193
194 if (cached) {
195 cached = _core.types.cloneNode(cached);
196 } else {
197 cached = (0, _helperModuleImports.addDefault)(file.path, source, {
198 importedInterop: isHelper && supportsCJSDefault ? "compiled" : "uncompiled",
199 nameHint,
200 blockHoist
201 });
202 cache.set(key, cached);
203 }
204
205 return cached;
206 }
207 }
208
209 };
210});
211
212exports.default = _default;
\No newline at end of file