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