UNPKG

9.65 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 _plugins = _interopRequireDefault(require("@babel/compat-data/plugins"));
22
23var _shippedProposals = require("../data/shipped-proposals");
24
25var _overlappingPlugins = _interopRequireDefault(require("@babel/compat-data/overlapping-plugins"));
26
27var _usagePlugin = _interopRequireDefault(require("./polyfills/corejs2/usage-plugin"));
28
29var _usagePlugin2 = _interopRequireDefault(require("./polyfills/corejs3/usage-plugin"));
30
31var _usagePlugin3 = _interopRequireDefault(require("./polyfills/regenerator/usage-plugin"));
32
33var _entryPlugin = _interopRequireDefault(require("./polyfills/corejs2/entry-plugin"));
34
35var _entryPlugin2 = _interopRequireDefault(require("./polyfills/corejs3/entry-plugin"));
36
37var _entryPlugin3 = _interopRequireDefault(require("./polyfills/regenerator/entry-plugin"));
38
39var _helperCompilationTargets = _interopRequireWildcard(require("@babel/helper-compilation-targets"));
40
41var _availablePlugins = _interopRequireDefault(require("./available-plugins"));
42
43var _utils = require("./utils");
44
45var _helperPluginUtils = require("@babel/helper-plugin-utils");
46
47function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
48
49function _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; }
50
51function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
52
53function isPluginRequired(targets, support) {
54 return !(0, _helperCompilationTargets.isRequired)("fake-name", targets, {
55 compatData: {
56 "fake-name": support
57 }
58 });
59}
60
61const pluginListWithoutProposals = (0, _utils.filterStageFromList)(_plugins.default, _shippedProposals.proposalPlugins);
62
63const getPlugin = pluginName => {
64 const plugin = _availablePlugins.default[pluginName];
65
66 if (!plugin) {
67 throw new Error(`Could not find plugin "${pluginName}". Ensure there is an entry in ./available-plugins.js for it.`);
68 }
69
70 return plugin;
71};
72
73const transformIncludesAndExcludes = opts => {
74 return opts.reduce((result, opt) => {
75 const target = opt.match(/^(es|es6|es7|esnext|web)\./) ? "builtIns" : "plugins";
76 result[target].add(opt);
77 return result;
78 }, {
79 all: opts,
80 plugins: new Set(),
81 builtIns: new Set()
82 });
83};
84
85exports.transformIncludesAndExcludes = transformIncludesAndExcludes;
86
87const getModulesPluginNames = ({
88 modules,
89 transformations,
90 shouldTransformESM,
91 shouldTransformDynamicImport,
92 shouldParseTopLevelAwait
93}) => {
94 const modulesPluginNames = [];
95
96 if (modules !== false && transformations[modules]) {
97 if (shouldTransformESM) {
98 modulesPluginNames.push(transformations[modules]);
99 }
100
101 if (shouldTransformDynamicImport && shouldTransformESM && modules !== "umd") {
102 modulesPluginNames.push("proposal-dynamic-import");
103 } else {
104 if (shouldTransformDynamicImport) {
105 console.warn("Dynamic import can only be supported when transforming ES modules" + " to AMD, CommonJS or SystemJS. Only the parser plugin will be enabled.");
106 }
107
108 modulesPluginNames.push("syntax-dynamic-import");
109 }
110 } else {
111 modulesPluginNames.push("syntax-dynamic-import");
112 }
113
114 if (shouldParseTopLevelAwait) {
115 modulesPluginNames.push("syntax-top-level-await");
116 }
117
118 return modulesPluginNames;
119};
120
121exports.getModulesPluginNames = getModulesPluginNames;
122
123const getPolyfillPlugins = ({
124 useBuiltIns,
125 corejs,
126 polyfillTargets,
127 include,
128 exclude,
129 proposals,
130 shippedProposals,
131 regenerator,
132 debug
133}) => {
134 const polyfillPlugins = [];
135
136 if (useBuiltIns === "usage" || useBuiltIns === "entry") {
137 const pluginOptions = {
138 corejs,
139 polyfillTargets,
140 include,
141 exclude,
142 proposals,
143 shippedProposals,
144 regenerator,
145 debug
146 };
147
148 if (corejs) {
149 if (useBuiltIns === "usage") {
150 if (corejs.major === 2) {
151 polyfillPlugins.push([_usagePlugin.default, pluginOptions]);
152 } else {
153 polyfillPlugins.push([_usagePlugin2.default, pluginOptions]);
154 }
155
156 if (regenerator) {
157 polyfillPlugins.push([_usagePlugin3.default, pluginOptions]);
158 }
159 } else {
160 if (corejs.major === 2) {
161 polyfillPlugins.push([_entryPlugin.default, pluginOptions]);
162 } else {
163 polyfillPlugins.push([_entryPlugin2.default, pluginOptions]);
164
165 if (!regenerator) {
166 polyfillPlugins.push([_entryPlugin3.default, pluginOptions]);
167 }
168 }
169 }
170 }
171 }
172
173 return polyfillPlugins;
174};
175
176exports.getPolyfillPlugins = getPolyfillPlugins;
177
178function supportsStaticESM(caller) {
179 return !!(caller && caller.supportsStaticESM);
180}
181
182function supportsDynamicImport(caller) {
183 return !!(caller && caller.supportsDynamicImport);
184}
185
186function supportsTopLevelAwait(caller) {
187 return !!(caller && caller.supportsTopLevelAwait);
188}
189
190var _default = (0, _helperPluginUtils.declare)((api, opts) => {
191 api.assertVersion(7);
192 const {
193 configPath,
194 debug,
195 exclude: optionsExclude,
196 forceAllTransforms,
197 ignoreBrowserslistConfig,
198 include: optionsInclude,
199 loose,
200 modules,
201 shippedProposals,
202 spec,
203 targets: optionsTargets,
204 useBuiltIns,
205 corejs: {
206 version: corejs,
207 proposals
208 }
209 } = (0, _normalizeOptions.default)(opts);
210 let hasUglifyTarget = false;
211
212 if (optionsTargets && optionsTargets.uglify) {
213 hasUglifyTarget = true;
214 delete optionsTargets.uglify;
215 console.log("");
216 console.log("The uglify target has been deprecated. Set the top level");
217 console.log("option `forceAllTransforms: true` instead.");
218 console.log("");
219 }
220
221 if (optionsTargets && optionsTargets.esmodules && optionsTargets.browsers) {
222 console.log("");
223 console.log("@babel/preset-env: esmodules and browsers targets have been specified together.");
224 console.log(`\`browsers\` target, \`${optionsTargets.browsers}\` will be ignored.`);
225 console.log("");
226 }
227
228 const targets = (0, _helperCompilationTargets.default)(optionsTargets, {
229 ignoreBrowserslistConfig,
230 configPath
231 });
232 const include = transformIncludesAndExcludes(optionsInclude);
233 const exclude = transformIncludesAndExcludes(optionsExclude);
234 const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;
235 const modulesPluginNames = getModulesPluginNames({
236 modules,
237 transformations: _moduleTransformations.default,
238 shouldTransformESM: modules !== "auto" || !api.caller || !api.caller(supportsStaticESM),
239 shouldTransformDynamicImport: modules !== "auto" || !api.caller || !api.caller(supportsDynamicImport),
240 shouldParseTopLevelAwait: !api.caller || api.caller(supportsTopLevelAwait)
241 });
242 const pluginNames = (0, _helperCompilationTargets.filterItems)(shippedProposals ? _plugins.default : pluginListWithoutProposals, include.plugins, exclude.plugins, transformTargets, modulesPluginNames, (0, _getOptionSpecificExcludes.default)({
243 loose
244 }), _shippedProposals.pluginSyntaxMap);
245 (0, _filterItems.removeUnnecessaryItems)(pluginNames, _overlappingPlugins.default);
246 const polyfillPlugins = getPolyfillPlugins({
247 useBuiltIns,
248 corejs,
249 polyfillTargets: targets,
250 include: include.builtIns,
251 exclude: exclude.builtIns,
252 proposals,
253 shippedProposals,
254 regenerator: pluginNames.has("transform-regenerator"),
255 debug
256 });
257 const pluginUseBuiltIns = useBuiltIns !== false;
258 const plugins = Array.from(pluginNames).map(pluginName => [getPlugin(pluginName), {
259 spec,
260 loose,
261 useBuiltIns: pluginUseBuiltIns
262 }]).concat(polyfillPlugins);
263
264 if (debug) {
265 console.log("@babel/preset-env: `DEBUG` option");
266 console.log("\nUsing targets:");
267 console.log(JSON.stringify((0, _helperCompilationTargets.prettifyTargets)(targets), null, 2));
268 console.log(`\nUsing modules transform: ${modules.toString()}`);
269 console.log("\nUsing plugins:");
270 pluginNames.forEach(pluginName => {
271 (0, _debug.logPluginOrPolyfill)(pluginName, targets, _plugins.default);
272 });
273
274 if (!useBuiltIns) {
275 console.log("\nUsing polyfills: No polyfills were added, since the `useBuiltIns` option was not set.");
276 } else {
277 console.log(`\nUsing polyfills with \`${useBuiltIns}\` option:`);
278 }
279 }
280
281 return {
282 plugins
283 };
284});
285
286exports.default = _default;
\No newline at end of file