UNPKG

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