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