1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 | exports.isPluginRequired = isPluginRequired;
|
---|
8 | exports.transformIncludesAndExcludes = void 0;
|
---|
9 | var _semver = require("semver");
|
---|
10 | var _debug = require("./debug.js");
|
---|
11 | var _filterItems = require("./filter-items.js");
|
---|
12 | var _moduleTransformations = require("./module-transformations.js");
|
---|
13 | var _normalizeOptions = require("./normalize-options.js");
|
---|
14 | var _shippedProposals = require("./shipped-proposals.js");
|
---|
15 | var _pluginsCompatData = require("./plugins-compat-data.js");
|
---|
16 | var _babelPluginPolyfillCorejs = require("babel-plugin-polyfill-corejs3");
|
---|
17 | var _babel7Plugins = require("./polyfills/babel-7-plugins.cjs");
|
---|
18 | var _helperCompilationTargets = require("@babel/helper-compilation-targets");
|
---|
19 | var _availablePlugins = require("./available-plugins.js");
|
---|
20 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
---|
21 | const pluginCoreJS3 = _babelPluginPolyfillCorejs.default || _babelPluginPolyfillCorejs;
|
---|
22 | function isPluginRequired(targets, support) {
|
---|
23 | return (0, _helperCompilationTargets.isRequired)("fake-name", targets, {
|
---|
24 | compatData: {
|
---|
25 | "fake-name": support
|
---|
26 | }
|
---|
27 | });
|
---|
28 | }
|
---|
29 | function filterStageFromList(list, stageList) {
|
---|
30 | return Object.keys(list).reduce((result, item) => {
|
---|
31 | if (!stageList.has(item)) {
|
---|
32 | result[item] = list[item];
|
---|
33 | }
|
---|
34 | return result;
|
---|
35 | }, {});
|
---|
36 | }
|
---|
37 | const pluginLists = {
|
---|
38 | withProposals: {
|
---|
39 | withoutBugfixes: _pluginsCompatData.plugins,
|
---|
40 | withBugfixes: Object.assign({}, _pluginsCompatData.plugins, _pluginsCompatData.pluginsBugfixes)
|
---|
41 | },
|
---|
42 | withoutProposals: {
|
---|
43 | withoutBugfixes: filterStageFromList(_pluginsCompatData.plugins, _shippedProposals.proposalPlugins),
|
---|
44 | withBugfixes: filterStageFromList(Object.assign({}, _pluginsCompatData.plugins, _pluginsCompatData.pluginsBugfixes), _shippedProposals.proposalPlugins)
|
---|
45 | }
|
---|
46 | };
|
---|
47 | function getPluginList(proposals, bugfixes) {
|
---|
48 | if (proposals) {
|
---|
49 | if (bugfixes) return pluginLists.withProposals.withBugfixes;else return pluginLists.withProposals.withoutBugfixes;
|
---|
50 | } else {
|
---|
51 | if (bugfixes) return pluginLists.withoutProposals.withBugfixes;else return pluginLists.withoutProposals.withoutBugfixes;
|
---|
52 | }
|
---|
53 | }
|
---|
54 | const getPlugin = pluginName => {
|
---|
55 | const plugin = _availablePlugins.default[pluginName]();
|
---|
56 | if (!plugin) {
|
---|
57 | throw new Error(`Could not find plugin "${pluginName}". Ensure there is an entry in ./available-plugins.js for it.`);
|
---|
58 | }
|
---|
59 | return plugin;
|
---|
60 | };
|
---|
61 | const transformIncludesAndExcludes = opts => {
|
---|
62 | return opts.reduce((result, opt) => {
|
---|
63 | const target = /^(?:es|es6|es7|esnext|web)\./.test(opt) ? "builtIns" : "plugins";
|
---|
64 | result[target].add(opt);
|
---|
65 | return result;
|
---|
66 | }, {
|
---|
67 | all: opts,
|
---|
68 | plugins: new Set(),
|
---|
69 | builtIns: new Set()
|
---|
70 | });
|
---|
71 | };
|
---|
72 | exports.transformIncludesAndExcludes = transformIncludesAndExcludes;
|
---|
73 | function getSpecialModulesPluginNames(modules, shouldTransformDynamicImport, babelVersion) {
|
---|
74 | const modulesPluginNames = [];
|
---|
75 | if (modules) {
|
---|
76 | modulesPluginNames.push(_moduleTransformations.default[modules]);
|
---|
77 | }
|
---|
78 | if (shouldTransformDynamicImport) {
|
---|
79 | if (modules && modules !== "umd") {
|
---|
80 | modulesPluginNames.push("transform-dynamic-import");
|
---|
81 | } else {
|
---|
82 | console.warn("Dynamic import can only be transformed when transforming ES" + " modules to AMD, CommonJS or SystemJS.");
|
---|
83 | }
|
---|
84 | }
|
---|
85 | if (babelVersion[0] !== "8") {
|
---|
86 | if (!shouldTransformDynamicImport) {
|
---|
87 | modulesPluginNames.push("syntax-dynamic-import");
|
---|
88 | }
|
---|
89 | modulesPluginNames.push("syntax-top-level-await");
|
---|
90 | modulesPluginNames.push("syntax-import-meta");
|
---|
91 | }
|
---|
92 | return modulesPluginNames;
|
---|
93 | }
|
---|
94 | const getCoreJSOptions = ({
|
---|
95 | useBuiltIns,
|
---|
96 | corejs,
|
---|
97 | polyfillTargets,
|
---|
98 | include,
|
---|
99 | exclude,
|
---|
100 | proposals,
|
---|
101 | shippedProposals,
|
---|
102 | debug
|
---|
103 | }) => ({
|
---|
104 | method: `${useBuiltIns}-global`,
|
---|
105 | version: corejs ? corejs.toString() : undefined,
|
---|
106 | targets: polyfillTargets,
|
---|
107 | include,
|
---|
108 | exclude,
|
---|
109 | proposals,
|
---|
110 | shippedProposals,
|
---|
111 | debug,
|
---|
112 | "#__secret_key__@babel/preset-env__compatibility": {
|
---|
113 | noRuntimeName: true
|
---|
114 | }
|
---|
115 | });
|
---|
116 | {
|
---|
117 | var getPolyfillPlugins = ({
|
---|
118 | useBuiltIns,
|
---|
119 | corejs,
|
---|
120 | polyfillTargets,
|
---|
121 | include,
|
---|
122 | exclude,
|
---|
123 | proposals,
|
---|
124 | shippedProposals,
|
---|
125 | regenerator,
|
---|
126 | debug
|
---|
127 | }) => {
|
---|
128 | const polyfillPlugins = [];
|
---|
129 | if (useBuiltIns === "usage" || useBuiltIns === "entry") {
|
---|
130 | const pluginOptions = getCoreJSOptions({
|
---|
131 | useBuiltIns,
|
---|
132 | corejs,
|
---|
133 | polyfillTargets,
|
---|
134 | include,
|
---|
135 | exclude,
|
---|
136 | proposals,
|
---|
137 | shippedProposals,
|
---|
138 | debug
|
---|
139 | });
|
---|
140 | if (corejs) {
|
---|
141 | {
|
---|
142 | if (useBuiltIns === "usage") {
|
---|
143 | if (corejs.major === 2) {
|
---|
144 | polyfillPlugins.push([_babel7Plugins.pluginCoreJS2, pluginOptions], [_babel7Plugins.legacyBabelPolyfillPlugin, {
|
---|
145 | usage: true
|
---|
146 | }]);
|
---|
147 | } else {
|
---|
148 | polyfillPlugins.push([pluginCoreJS3, pluginOptions], [_babel7Plugins.legacyBabelPolyfillPlugin, {
|
---|
149 | usage: true,
|
---|
150 | deprecated: true
|
---|
151 | }]);
|
---|
152 | }
|
---|
153 | if (regenerator) {
|
---|
154 | polyfillPlugins.push([_babel7Plugins.pluginRegenerator, {
|
---|
155 | method: "usage-global",
|
---|
156 | debug
|
---|
157 | }]);
|
---|
158 | }
|
---|
159 | } else {
|
---|
160 | if (corejs.major === 2) {
|
---|
161 | polyfillPlugins.push([_babel7Plugins.legacyBabelPolyfillPlugin, {
|
---|
162 | regenerator
|
---|
163 | }], [_babel7Plugins.pluginCoreJS2, pluginOptions]);
|
---|
164 | } else {
|
---|
165 | polyfillPlugins.push([pluginCoreJS3, pluginOptions], [_babel7Plugins.legacyBabelPolyfillPlugin, {
|
---|
166 | deprecated: true
|
---|
167 | }]);
|
---|
168 | if (!regenerator) {
|
---|
169 | polyfillPlugins.push([_babel7Plugins.removeRegeneratorEntryPlugin, pluginOptions]);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|
176 | return polyfillPlugins;
|
---|
177 | };
|
---|
178 | {
|
---|
179 | exports.getPolyfillPlugins = getPolyfillPlugins;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | function getLocalTargets(optionsTargets, ignoreBrowserslistConfig, configPath, browserslistEnv, api) {
|
---|
183 | if (optionsTargets != null && optionsTargets.esmodules && optionsTargets.browsers) {
|
---|
184 | console.warn(`
|
---|
185 | @babel/preset-env: esmodules and browsers targets have been specified together.
|
---|
186 | \`browsers\` target, \`${optionsTargets.browsers.toString()}\` will be ignored.
|
---|
187 | `);
|
---|
188 | }
|
---|
189 | return (0, _helperCompilationTargets.default)(optionsTargets, {
|
---|
190 | ignoreBrowserslistConfig,
|
---|
191 | configPath,
|
---|
192 | browserslistEnv,
|
---|
193 | onBrowserslistConfigFound(config) {
|
---|
194 | api.addExternalDependency(config);
|
---|
195 | }
|
---|
196 | });
|
---|
197 | }
|
---|
198 | function supportsStaticESM(caller) {
|
---|
199 | return !!(caller != null && caller.supportsStaticESM);
|
---|
200 | }
|
---|
201 | function supportsDynamicImport(caller) {
|
---|
202 | return !!(caller != null && caller.supportsDynamicImport);
|
---|
203 | }
|
---|
204 | function supportsExportNamespaceFrom(caller) {
|
---|
205 | return !!(caller != null && caller.supportsExportNamespaceFrom);
|
---|
206 | }
|
---|
207 | var _default = exports.default = (0, _helperPluginUtils.declarePreset)((api, opts) => {
|
---|
208 | api.assertVersion(7);
|
---|
209 | const babelTargets = api.targets();
|
---|
210 | ;
|
---|
211 | const {
|
---|
212 | bugfixes,
|
---|
213 | configPath,
|
---|
214 | debug,
|
---|
215 | exclude: optionsExclude,
|
---|
216 | forceAllTransforms,
|
---|
217 | ignoreBrowserslistConfig,
|
---|
218 | include: optionsInclude,
|
---|
219 | modules: optionsModules,
|
---|
220 | shippedProposals,
|
---|
221 | targets: optionsTargets,
|
---|
222 | useBuiltIns,
|
---|
223 | corejs: {
|
---|
224 | version: corejs,
|
---|
225 | proposals
|
---|
226 | },
|
---|
227 | browserslistEnv
|
---|
228 | } = (0, _normalizeOptions.default)(opts);
|
---|
229 | {
|
---|
230 | var {
|
---|
231 | loose,
|
---|
232 | spec = false
|
---|
233 | } = opts;
|
---|
234 | }
|
---|
235 | let targets = babelTargets;
|
---|
236 | if (_semver.lt(api.version, "7.13.0") || opts.targets || opts.configPath || opts.browserslistEnv || opts.ignoreBrowserslistConfig) {
|
---|
237 | {
|
---|
238 | var hasUglifyTarget = false;
|
---|
239 | if (optionsTargets != null && optionsTargets.uglify) {
|
---|
240 | hasUglifyTarget = true;
|
---|
241 | delete optionsTargets.uglify;
|
---|
242 | console.warn(`
|
---|
243 | The uglify target has been deprecated. Set the top level
|
---|
244 | option \`forceAllTransforms: true\` instead.
|
---|
245 | `);
|
---|
246 | }
|
---|
247 | }
|
---|
248 | targets = getLocalTargets(optionsTargets, ignoreBrowserslistConfig, configPath, browserslistEnv, api);
|
---|
249 | }
|
---|
250 | const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;
|
---|
251 | const include = transformIncludesAndExcludes(optionsInclude);
|
---|
252 | const exclude = transformIncludesAndExcludes(optionsExclude);
|
---|
253 | const compatData = getPluginList(shippedProposals, bugfixes);
|
---|
254 | const modules = optionsModules === "auto" ? api.caller(supportsStaticESM) ? false : "commonjs" : optionsModules;
|
---|
255 | const shouldTransformDynamicImport = optionsModules === "auto" ? !api.caller(supportsDynamicImport) : !!modules;
|
---|
256 | if (!exclude.plugins.has("transform-export-namespace-from") && (optionsModules === "auto" ? !api.caller(supportsExportNamespaceFrom) : !!modules)) {
|
---|
257 | include.plugins.add("transform-export-namespace-from");
|
---|
258 | }
|
---|
259 | const pluginNames = (0, _helperCompilationTargets.filterItems)(compatData, include.plugins, exclude.plugins, transformTargets, getSpecialModulesPluginNames(modules, shouldTransformDynamicImport, api.version), !loose ? undefined : ["transform-typeof-symbol"], _shippedProposals.pluginSyntaxMap);
|
---|
260 | if (shippedProposals) {
|
---|
261 | (0, _filterItems.addProposalSyntaxPlugins)(pluginNames, _shippedProposals.proposalSyntaxPlugins);
|
---|
262 | }
|
---|
263 | (0, _filterItems.removeUnsupportedItems)(pluginNames, api.version);
|
---|
264 | (0, _filterItems.removeUnnecessaryItems)(pluginNames, _pluginsCompatData.overlappingPlugins);
|
---|
265 | const polyfillPlugins = getPolyfillPlugins({
|
---|
266 | useBuiltIns,
|
---|
267 | corejs,
|
---|
268 | polyfillTargets: targets,
|
---|
269 | include: include.builtIns,
|
---|
270 | exclude: exclude.builtIns,
|
---|
271 | proposals,
|
---|
272 | shippedProposals,
|
---|
273 | regenerator: pluginNames.has("transform-regenerator"),
|
---|
274 | debug
|
---|
275 | });
|
---|
276 | const pluginUseBuiltIns = useBuiltIns !== false;
|
---|
277 | const plugins = Array.from(pluginNames).map(pluginName => {
|
---|
278 | if (pluginName === "transform-class-properties" || pluginName === "transform-private-methods" || pluginName === "transform-private-property-in-object") {
|
---|
279 | return [getPlugin(pluginName), {
|
---|
280 | loose: loose ? "#__internal__@babel/preset-env__prefer-true-but-false-is-ok-if-it-prevents-an-error" : "#__internal__@babel/preset-env__prefer-false-but-true-is-ok-if-it-prevents-an-error"
|
---|
281 | }];
|
---|
282 | }
|
---|
283 | if (pluginName === "syntax-import-attributes") {
|
---|
284 | return [getPlugin(pluginName), {
|
---|
285 | deprecatedAssertSyntax: true
|
---|
286 | }];
|
---|
287 | }
|
---|
288 | return [getPlugin(pluginName), {
|
---|
289 | spec,
|
---|
290 | loose,
|
---|
291 | useBuiltIns: pluginUseBuiltIns
|
---|
292 | }];
|
---|
293 | }).concat(polyfillPlugins);
|
---|
294 | if (debug) {
|
---|
295 | console.log("@babel/preset-env: `DEBUG` option");
|
---|
296 | console.log("\nUsing targets:");
|
---|
297 | console.log(JSON.stringify((0, _helperCompilationTargets.prettifyTargets)(targets), null, 2));
|
---|
298 | console.log(`\nUsing modules transform: ${optionsModules.toString()}`);
|
---|
299 | console.log("\nUsing plugins:");
|
---|
300 | pluginNames.forEach(pluginName => {
|
---|
301 | (0, _debug.logPlugin)(pluginName, targets, compatData);
|
---|
302 | });
|
---|
303 | if (!useBuiltIns) {
|
---|
304 | console.log("\nUsing polyfills: No polyfills were added, since the `useBuiltIns` option was not set.");
|
---|
305 | }
|
---|
306 | }
|
---|
307 | return {
|
---|
308 | plugins
|
---|
309 | };
|
---|
310 | });
|
---|
311 | {
|
---|
312 | exports.getModulesPluginNames = ({
|
---|
313 | modules,
|
---|
314 | transformations,
|
---|
315 | shouldTransformESM,
|
---|
316 | shouldTransformDynamicImport,
|
---|
317 | shouldTransformExportNamespaceFrom
|
---|
318 | }) => {
|
---|
319 | const modulesPluginNames = [];
|
---|
320 | if (modules !== false && transformations[modules]) {
|
---|
321 | if (shouldTransformESM) {
|
---|
322 | modulesPluginNames.push(transformations[modules]);
|
---|
323 | }
|
---|
324 | if (shouldTransformDynamicImport) {
|
---|
325 | if (shouldTransformESM && modules !== "umd") {
|
---|
326 | modulesPluginNames.push("transform-dynamic-import");
|
---|
327 | } else {
|
---|
328 | console.warn("Dynamic import can only be transformed when transforming ES" + " modules to AMD, CommonJS or SystemJS.");
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|
332 | if (shouldTransformExportNamespaceFrom) {
|
---|
333 | modulesPluginNames.push("transform-export-namespace-from");
|
---|
334 | }
|
---|
335 | if (!shouldTransformDynamicImport) {
|
---|
336 | modulesPluginNames.push("syntax-dynamic-import");
|
---|
337 | }
|
---|
338 | if (!shouldTransformExportNamespaceFrom) {
|
---|
339 | modulesPluginNames.push("syntax-export-namespace-from");
|
---|
340 | }
|
---|
341 | modulesPluginNames.push("syntax-top-level-await");
|
---|
342 | modulesPluginNames.push("syntax-import-meta");
|
---|
343 | return modulesPluginNames;
|
---|
344 | };
|
---|
345 | }
|
---|
346 |
|
---|
347 | //# sourceMappingURL=index.js.map
|
---|