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