UNPKG

9.25 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8function _gensync() {
9 const data = _interopRequireDefault(require("gensync"));
10
11 _gensync = function () {
12 return data;
13 };
14
15 return data;
16}
17
18var _async = require("../gensync-utils/async");
19
20var _util = require("./util");
21
22var context = _interopRequireWildcard(require("../index"));
23
24var _plugin = _interopRequireDefault(require("./plugin"));
25
26var _item = require("./item");
27
28var _configChain = require("./config-chain");
29
30function _traverse() {
31 const data = _interopRequireDefault(require("@babel/traverse"));
32
33 _traverse = function () {
34 return data;
35 };
36
37 return data;
38}
39
40var _caching = require("./caching");
41
42var _options = require("./validation/options");
43
44var _plugins = require("./validation/plugins");
45
46var _configApi = _interopRequireDefault(require("./helpers/config-api"));
47
48var _partial = _interopRequireDefault(require("./partial"));
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; } 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; }
53
54function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
55
56var _default = (0, _gensync().default)(function* loadFullConfig(inputOpts) {
57 const result = yield* (0, _partial.default)(inputOpts);
58
59 if (!result) {
60 return null;
61 }
62
63 const {
64 options,
65 context
66 } = result;
67 const optionDefaults = {};
68 const passes = [[]];
69
70 try {
71 const {
72 plugins,
73 presets
74 } = options;
75
76 if (!plugins || !presets) {
77 throw new Error("Assertion failure - plugins and presets exist");
78 }
79
80 const ignored = yield* function* recurseDescriptors(config, pass) {
81 const plugins = [];
82
83 for (let i = 0; i < config.plugins.length; i++) {
84 const descriptor = config.plugins[i];
85
86 if (descriptor.options !== false) {
87 try {
88 plugins.push(yield* loadPluginDescriptor(descriptor, context));
89 } catch (e) {
90 if (i > 0 && e.code === "BABEL_UNKNOWN_PLUGIN_PROPERTY") {
91 (0, _options.checkNoUnwrappedItemOptionPairs)(config.plugins[i - 1], descriptor, "plugin", i, e);
92 }
93
94 throw e;
95 }
96 }
97 }
98
99 const presets = [];
100
101 for (let i = 0; i < config.presets.length; i++) {
102 const descriptor = config.presets[i];
103
104 if (descriptor.options !== false) {
105 try {
106 presets.push({
107 preset: yield* loadPresetDescriptor(descriptor, context),
108 pass: descriptor.ownPass ? [] : pass
109 });
110 } catch (e) {
111 if (i > 0 && e.code === "BABEL_UNKNOWN_OPTION") {
112 (0, _options.checkNoUnwrappedItemOptionPairs)(config.presets[i - 1], descriptor, "preset", i, e);
113 }
114
115 throw e;
116 }
117 }
118 }
119
120 if (presets.length > 0) {
121 passes.splice(1, 0, ...presets.map(o => o.pass).filter(p => p !== pass));
122
123 for (const {
124 preset,
125 pass
126 } of presets) {
127 if (!preset) return true;
128 const ignored = yield* recurseDescriptors({
129 plugins: preset.plugins,
130 presets: preset.presets
131 }, pass);
132 if (ignored) return true;
133 preset.options.forEach(opts => {
134 (0, _util.mergeOptions)(optionDefaults, opts);
135 });
136 }
137 }
138
139 if (plugins.length > 0) {
140 pass.unshift(...plugins);
141 }
142 }({
143 plugins: plugins.map(item => {
144 const desc = (0, _item.getItemDescriptor)(item);
145
146 if (!desc) {
147 throw new Error("Assertion failure - must be config item");
148 }
149
150 return desc;
151 }),
152 presets: presets.map(item => {
153 const desc = (0, _item.getItemDescriptor)(item);
154
155 if (!desc) {
156 throw new Error("Assertion failure - must be config item");
157 }
158
159 return desc;
160 })
161 }, passes[0]);
162 if (ignored) return null;
163 } catch (e) {
164 if (!/^\[BABEL\]/.test(e.message)) {
165 e.message = `[BABEL] ${context.filename || "unknown"}: ${e.message}`;
166 }
167
168 throw e;
169 }
170
171 const opts = optionDefaults;
172 (0, _util.mergeOptions)(opts, options);
173 opts.plugins = passes[0];
174 opts.presets = passes.slice(1).filter(plugins => plugins.length > 0).map(plugins => ({
175 plugins
176 }));
177 opts.passPerPreset = opts.presets.length > 0;
178 return {
179 options: opts,
180 passes: passes
181 };
182});
183
184exports.default = _default;
185const loadDescriptor = (0, _caching.makeWeakCache)(function* ({
186 value,
187 options,
188 dirname,
189 alias
190}, cache) {
191 if (options === false) throw new Error("Assertion failure");
192 options = options || {};
193 let item = value;
194
195 if (typeof value === "function") {
196 const api = Object.assign({}, context, (0, _configApi.default)(cache));
197
198 try {
199 item = value(api, options, dirname);
200 } catch (e) {
201 if (alias) {
202 e.message += ` (While processing: ${JSON.stringify(alias)})`;
203 }
204
205 throw e;
206 }
207 }
208
209 if (!item || typeof item !== "object") {
210 throw new Error("Plugin/Preset did not return an object.");
211 }
212
213 if (typeof item.then === "function") {
214 yield* [];
215 throw new Error(`You appear to be using an async plugin, ` + `which your current version of Babel does not support. ` + `If you're using a published plugin, ` + `you may need to upgrade your @babel/core version.`);
216 }
217
218 return {
219 value: item,
220 options,
221 dirname,
222 alias
223 };
224});
225
226function* loadPluginDescriptor(descriptor, context) {
227 if (descriptor.value instanceof _plugin.default) {
228 if (descriptor.options) {
229 throw new Error("Passed options to an existing Plugin instance will not work.");
230 }
231
232 return descriptor.value;
233 }
234
235 return yield* instantiatePlugin(yield* loadDescriptor(descriptor, context), context);
236}
237
238const instantiatePlugin = (0, _caching.makeWeakCache)(function* ({
239 value,
240 options,
241 dirname,
242 alias
243}, cache) {
244 const pluginObj = (0, _plugins.validatePluginObject)(value);
245 const plugin = Object.assign({}, pluginObj);
246
247 if (plugin.visitor) {
248 plugin.visitor = _traverse().default.explode(Object.assign({}, plugin.visitor));
249 }
250
251 if (plugin.inherits) {
252 const inheritsDescriptor = {
253 name: undefined,
254 alias: `${alias}$inherits`,
255 value: plugin.inherits,
256 options,
257 dirname
258 };
259 const inherits = yield* (0, _async.forwardAsync)(loadPluginDescriptor, run => {
260 return cache.invalidate(data => run(inheritsDescriptor, data));
261 });
262 plugin.pre = chain(inherits.pre, plugin.pre);
263 plugin.post = chain(inherits.post, plugin.post);
264 plugin.manipulateOptions = chain(inherits.manipulateOptions, plugin.manipulateOptions);
265 plugin.visitor = _traverse().default.visitors.merge([inherits.visitor || {}, plugin.visitor || {}]);
266 }
267
268 return new _plugin.default(plugin, options, alias);
269});
270
271const validateIfOptionNeedsFilename = (options, descriptor) => {
272 if (options.test || options.include || options.exclude) {
273 const formattedPresetName = descriptor.name ? `"${descriptor.name}"` : "/* your preset */";
274 throw new Error([`Preset ${formattedPresetName} requires a filename to be set when babel is called directly,`, `\`\`\``, `babel.transform(code, { filename: 'file.ts', presets: [${formattedPresetName}] });`, `\`\`\``, `See https://babeljs.io/docs/en/options#filename for more information.`].join("\n"));
275 }
276};
277
278const validatePreset = (preset, context, descriptor) => {
279 if (!context.filename) {
280 const {
281 options
282 } = preset;
283 validateIfOptionNeedsFilename(options, descriptor);
284
285 if (options.overrides) {
286 options.overrides.forEach(overrideOptions => validateIfOptionNeedsFilename(overrideOptions, descriptor));
287 }
288 }
289};
290
291function* loadPresetDescriptor(descriptor, context) {
292 const preset = instantiatePreset(yield* loadDescriptor(descriptor, context));
293 validatePreset(preset, context, descriptor);
294 return yield* (0, _configChain.buildPresetChain)(preset, context);
295}
296
297const instantiatePreset = (0, _caching.makeWeakCacheSync)(({
298 value,
299 dirname,
300 alias
301}) => {
302 return {
303 options: (0, _options.validate)("preset", value),
304 alias,
305 dirname
306 };
307});
308
309function chain(a, b) {
310 const fns = [a, b].filter(Boolean);
311 if (fns.length <= 1) return fns[0];
312 return function (...args) {
313 for (const fn of fns) {
314 fn.apply(this, args);
315 }
316 };
317}
\No newline at end of file