UNPKG

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