function buildPreset(context) { var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var validatedOptions = (0, _normalizeOptions2.default)(opts); var debug = validatedOptions.debug, loose = validatedOptions.loose, moduleType = validatedOptions.moduleType, spec = validatedOptions.spec, useBuiltIns = validatedOptions.useBuiltIns; var targets = getTargets(validatedOptions.targets); var include = transformIncludesAndExcludes(validatedOptions.include); var exclude = transformIncludesAndExcludes(validatedOptions.exclude); var filterPlugins = filterItem.bind(null, targets, exclude.plugins, _plugins2.default); var transformations = Object.keys(_plugins2.default).filter(filterPlugins).concat(include.plugins); var polyfills = void 0; var polyfillTargets = void 0; if (useBuiltIns) { polyfillTargets = getBuiltInTargets(targets); var filterBuiltIns = filterItem.bind(null, polyfillTargets, exclude.builtIns, _builtIns2.default); polyfills = Object.keys(_builtIns2.default).concat(getPlatformSpecificDefaultFor(polyfillTargets)).filter(filterBuiltIns).concat (include.builtIns); } if (debug && !hasBeenLogged) { hasBeenLogged = true; console.log("babel-preset-env: `DEBUG` option"); console.log("\nUsing targets:"); console.log(JSON.stringify(targets, null, 2)); console.log("\nModules transform: " + moduleType); console.log("\nUsing plugins:"); transformations.forEach(function (transform) { logPlugin(transform, targets, _plugins2.default); }); if (useBuiltIns && polyfills.length) { console.log("\nUsing polyfills:"); polyfills.forEach(function (polyfill) { logPlugin(polyfill, polyfillTargets, _builtIns2.default); }); } } var regenerator = transformations.indexOf("transform-regenerator") >= 0; var modulePlugin = moduleType !== false && _moduleTransformations2.default[moduleType]; var plugins = []; // NOTE: not giving spec here yet to avoid compatibility issues when // babel-plugin-transform-es2015-modules-commonjs gets its spec mode modulePlugin && plugins.push([require("babel-plugin-" + modulePlugin), { loose: loose }]); plugins.push.apply(plugins, transformations.map(function (pluginName) { return [require("babel-plugin-" + pluginName), { spec: spec, loose: loose }]; })); useBuiltIns && plugins.push([_transformPolyfillRequirePlugin2.default, { polyfills: polyfills, regenerator: regenerator }]); return { plugins: plugins }; }
n/a
function getCurrentNodeVersion() { return parseFloat(process.versions.node); }
n/a
function getTargets() { var targets = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var targetOpts = _extends({}, targets); if (targetOpts.node === true || targetOpts.node === "current") { targetOpts.node = getCurrentNodeVersion(); } if (typeof targetOpts.electron === "string") { targetOpts.electron = parseFloat(targetOpts.electron); } if (targetOpts.hasOwnProperty("uglify") && !targetOpts.uglify) { delete targetOpts.uglify; } var browserOpts = targetOpts.browsers; if (isBrowsersQueryValid(browserOpts)) { var queryBrowsers = getLowestVersions((0, _browserslist2.default)(browserOpts)); return mergeBrowsers(queryBrowsers, targetOpts); } return targetOpts; }
n/a
function isPluginRequired(supportedEnvironments, plugin) { if (supportedEnvironments.browsers) { supportedEnvironments = getTargets(supportedEnvironments); } var targetEnvironments = Object.keys(supportedEnvironments); if (targetEnvironments.length === 0) { return true; } var isRequiredForEnvironments = targetEnvironments.filter(function (environment) { // Feature is not implemented in that environment if (!plugin[environment]) { return true; } var lowestImplementedVersion = plugin[environment]; var lowestTargetedVersion = supportedEnvironments[environment]; if (typeof lowestTargetedVersion === "string") { throw new Error("Target version must be a number,\n '" + lowestTargetedVersion + "' was given for '" + environment + "'"); } return lowestTargetedVersion < lowestImplementedVersion; }); return isRequiredForEnvironments.length > 0 ? true : false; }
n/a
function transformIncludesAndExcludes(opts) { return { all: opts, plugins: opts.filter(function (opt) { return !opt.match(/^(es\d+|web)\./); }), builtIns: opts.filter(function (opt) { return opt.match(/^(es\d+|web)\./); }) }; }
n/a
function checkDuplicateIncludeExcludes() { var include = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var exclude = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; var duplicates = include.filter(function (opt) { return exclude.indexOf(opt) >= 0; }); (0, _invariant2.default)(duplicates.length === 0, "Invalid Option: The plugins/built-ins '" + duplicates + "' were found in both the\"include\" and\n \"exclude\" options."); }
n/a
function normalizeOptions(opts) { // TODO: remove whitelist in favor of include in next major if (opts.whitelist && !hasBeenWarned) { console.warn("Deprecation Warning: The \"whitelist\" option has been deprecated in favor of \"include\" to\n match the newly added\"exclude\" option (instead of \"blacklist\")."); hasBeenWarned = true; } (0, _invariant2.default)(!(opts.whitelist && opts.include), "Invalid Option: The \"whitelist\" and the \"include\" option are the same and one can be used at\n a time"); if (opts.exclude) { opts.exclude = normalizePluginNames(opts.exclude); } if (opts.whitelist || opts.include) { opts.include = normalizePluginNames(opts.whitelist || opts.include); } checkDuplicateIncludeExcludes(opts.include, opts.exclude); return { debug: opts.debug, exclude: validateIncludesAndExcludes(opts.exclude, "exclude"), include: validateIncludesAndExcludes(opts.include, "include"), loose: validateLooseOption(opts.loose), moduleType: validateModulesOption(opts.modules), spec: validateSpecOption(opts.spec), targets: opts.targets, useBuiltIns: opts.useBuiltIns }; }
n/a
function normalizePluginName(plugin) { return plugin.replace(/^babel-plugin-/, ""); }
n/a
function normalizePluginNames(plugins) { return plugins.map(normalizePluginName); }
n/a
function validateBoolOption(name, value, defaultValue) { if (typeof value === "undefined") { value = defaultValue; } if (typeof value !== "boolean") { throw new Error("Preset env: '" + name + "' option must be a boolean."); } return value; }
n/a
function validateIncludesAndExcludes() { var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var type = arguments[1]; (0, _invariant2.default)(Array.isArray(opts), "Invalid Option: The '" + type + "' option must be an Array<String> of plugins/built -ins"); var unknownOpts = []; opts.forEach(function (opt) { if (validIncludesAndExcludes.indexOf(opt) === -1) { unknownOpts.push(opt); } }); (0, _invariant2.default)(unknownOpts.length === 0, "Invalid Option: The plugins/built-ins '" + unknownOpts + "' passed to the '" + type + "' option are not\n valid. Please check data/[plugin-features|built-in-features].js in babel-preset-env"); return opts; }
n/a
function validateLooseOption(looseOpt) { return validateBoolOption("loose", looseOpt, false); }
n/a
function validateModulesOption() { var modulesOpt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "commonjs"; (0, _invariant2.default)(modulesOpt === false || Object.keys(_moduleTransformations2.default).indexOf(modulesOpt) > -1, "Invalid Option: The 'modules' option must be either 'false' to indicate no modules, or a\n module type which can be be one of: 'commonjs' (default), 'amd', 'umd', 'systemjs'."); return modulesOpt; }
n/a
function validateSpecOption(specOpt) { return validateBoolOption("spec", specOpt, false); }
n/a
default = function (_ref) { var t = _ref.types; function createImportDeclaration(polyfill) { var declar = t.importDeclaration([], t.stringLiteral(polyfill)); declar._blockHoist = 3; return declar; } function createRequireStatement(polyfill) { return t.expressionStatement(t.callExpression(t.identifier("require"), [t.stringLiteral(polyfill)])); } function isRequire(path) { return t.isExpressionStatement(path.node) && t.isCallExpression(path.node.expression) && t.isIdentifier(path.node.expression .callee) && path.node.expression.callee.name === "require" && path.node.expression.arguments.length === 1 && t.isStringLiteral(path .node.expression.arguments[0]) && isPolyfillSource(path.node.expression.arguments[0].value); } function createImport(polyfill, requireType, core) { if (core) { polyfill = "core-js/modules/" + polyfill; } if (requireType === "import") { return createImportDeclaration(polyfill); } else { return createRequireStatement(polyfill); } } function createImports(polyfills, requireType, regenerator) { var imports = polyfills.filter(function (el, i, arr) { return arr.indexOf(el) === i; }).map(function (polyfill) { return createImport(polyfill, requireType, true); }); return [].concat(imports, [regenerator && createImport("regenerator-runtime/runtime", requireType)]).filter(Boolean); } var isPolyfillImport = { ImportDeclaration: function ImportDeclaration(path, state) { if (path.node.specifiers.length === 0 && isPolyfillSource(path.node.source.value)) { this.numPolyfillImports++; if (this.numPolyfillImports > 1) { path.remove(); return; } path.replaceWithMultiple(createImports(state.opts.polyfills, "import", state.opts.regenerator)); } }, Program: function Program(path, state) { var _this = this; if (!state.opts.polyfills) { throw path.buildCodeFrameError("\nThere was an issue in \"babel-preset-env\" such that\nthe \"polyfills\" option was not correctly passed\nto the \"transform-polyfill-require\" plugin\n"); } path.get("body").forEach(function (bodyPath) { if (isRequire(bodyPath)) { _this.numPolyfillImports++; if (_this.numPolyfillImports > 1) { path.remove(); return; } bodyPath.replaceWithMultiple(createImports(state.opts.polyfills, "require", state.opts.regenerator)); } }); } }; return { name: "transform-polyfill-require", visitor: isPolyfillImport, pre: function pre() { this.numPolyfillImports = 0; } }; }
n/a