UNPKG

5.56 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.validateModulesOption = exports.validateSpecOption = exports.validateLooseOption = exports.validateBoolOption = exports.checkDuplicateIncludeExcludes = exports.normalizePluginNames = exports.normalizePluginName = exports.validateIncludesAndExcludes = undefined;
7exports.default = normalizeOptions;
8
9var _invariant = require("invariant");
10
11var _invariant2 = _interopRequireDefault(_invariant);
12
13var _builtIns = require("../data/built-ins.json");
14
15var _builtIns2 = _interopRequireDefault(_builtIns);
16
17var _defaultIncludes = require("./default-includes");
18
19var _moduleTransformations = require("./module-transformations");
20
21var _moduleTransformations2 = _interopRequireDefault(_moduleTransformations);
22
23var _pluginFeatures = require("../data/plugin-features");
24
25var _pluginFeatures2 = _interopRequireDefault(_pluginFeatures);
26
27function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
28
29function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
30
31var validIncludesAndExcludes = [].concat(_toConsumableArray(Object.keys(_pluginFeatures2.default)), _toConsumableArray(Object.keys(_moduleTransformations2.default).map(function (m) {
32 return _moduleTransformations2.default[m];
33})), _toConsumableArray(Object.keys(_builtIns2.default)), _toConsumableArray(_defaultIncludes.defaultWebIncludes));
34
35var hasBeenWarned = false;
36
37var validateIncludesAndExcludes = exports.validateIncludesAndExcludes = function validateIncludesAndExcludes() {
38 var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
39 var type = arguments[1];
40
41 (0, _invariant2.default)(Array.isArray(opts), "Invalid Option: The '" + type + "' option must be an Array<String> of plugins/built-ins");
42
43 var unknownOpts = [];
44 opts.forEach(function (opt) {
45 if (validIncludesAndExcludes.indexOf(opt) === -1) {
46 unknownOpts.push(opt);
47 }
48 });
49
50 (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");
51
52 return opts;
53};
54
55var normalizePluginName = exports.normalizePluginName = function normalizePluginName(plugin) {
56 return plugin.replace(/^babel-plugin-/, "");
57};
58
59var normalizePluginNames = exports.normalizePluginNames = function normalizePluginNames(plugins) {
60 return plugins.map(normalizePluginName);
61};
62
63var checkDuplicateIncludeExcludes = exports.checkDuplicateIncludeExcludes = function checkDuplicateIncludeExcludes() {
64 var include = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
65 var exclude = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
66
67 var duplicates = include.filter(function (opt) {
68 return exclude.indexOf(opt) >= 0;
69 });
70
71 (0, _invariant2.default)(duplicates.length === 0, "Invalid Option: The plugins/built-ins '" + duplicates + "' were found in both the \"include\" and\n \"exclude\" options.");
72};
73
74var validateBoolOption = exports.validateBoolOption = function validateBoolOption(name, value, defaultValue) {
75 if (typeof value === "undefined") {
76 value = defaultValue;
77 }
78
79 if (typeof value !== "boolean") {
80 throw new Error("Preset env: '" + name + "' option must be a boolean.");
81 }
82
83 return value;
84};
85
86var validateLooseOption = exports.validateLooseOption = function validateLooseOption(looseOpt) {
87 return validateBoolOption("loose", looseOpt, false);
88};
89var validateSpecOption = exports.validateSpecOption = function validateSpecOption(specOpt) {
90 return validateBoolOption("spec", specOpt, false);
91};
92
93var validateModulesOption = exports.validateModulesOption = function validateModulesOption() {
94 var modulesOpt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "commonjs";
95
96 (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'.");
97
98 return modulesOpt;
99};
100
101function normalizeOptions(opts) {
102 // TODO: remove whitelist in favor of include in next major
103 if (opts.whitelist && !hasBeenWarned) {
104 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\").");
105 hasBeenWarned = true;
106 }
107
108 (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");
109
110 if (opts.exclude) {
111 opts.exclude = normalizePluginNames(opts.exclude);
112 }
113
114 if (opts.whitelist || opts.include) {
115 opts.include = normalizePluginNames(opts.whitelist || opts.include);
116 }
117
118 checkDuplicateIncludeExcludes(opts.include, opts.exclude);
119
120 return {
121 debug: opts.debug,
122 exclude: validateIncludesAndExcludes(opts.exclude, "exclude"),
123 include: validateIncludesAndExcludes(opts.include, "include"),
124 loose: validateLooseOption(opts.loose),
125 moduleType: validateModulesOption(opts.modules),
126 spec: validateSpecOption(opts.spec),
127 targets: opts.targets,
128 useBuiltIns: opts.useBuiltIns
129 };
130}
\No newline at end of file