UNPKG

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