source: trip-planner-front/node_modules/@babel/helper-define-polyfill-provider/lib/index.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 10.9 KB
Line 
1"use strict";
2
3exports.__esModule = true;
4exports.default = definePolyfillProvider;
5
6var _helperPluginUtils = require("@babel/helper-plugin-utils");
7
8var _helperCompilationTargets = _interopRequireWildcard(require("@babel/helper-compilation-targets"));
9
10var _utils = require("./utils");
11
12var _importsCache = _interopRequireDefault(require("./imports-cache"));
13
14var _debugUtils = require("./debug-utils");
15
16var _normalizeOptions = require("./normalize-options");
17
18var v = _interopRequireWildcard(require("./visitors"));
19
20var deps = _interopRequireWildcard(require("./node/dependencies"));
21
22var _metaResolver = _interopRequireDefault(require("./meta-resolver"));
23
24function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
26function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
27
28function _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; }
29
30function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
31
32const getTargets = _helperCompilationTargets.default.default || _helperCompilationTargets.default;
33
34function resolveOptions(options, babelApi) {
35 const {
36 method,
37 targets: targetsOption,
38 ignoreBrowserslistConfig,
39 configPath,
40 debug,
41 shouldInjectPolyfill,
42 absoluteImports
43 } = options,
44 providerOptions = _objectWithoutPropertiesLoose(options, ["method", "targets", "ignoreBrowserslistConfig", "configPath", "debug", "shouldInjectPolyfill", "absoluteImports"]);
45
46 let methodName;
47 if (method === "usage-global") methodName = "usageGlobal";else if (method === "entry-global") methodName = "entryGlobal";else if (method === "usage-pure") methodName = "usagePure";else if (typeof method !== "string") {
48 throw new Error(".method must be a string");
49 } else {
50 throw new Error(`.method must be one of "entry-global", "usage-global"` + ` or "usage-pure" (received ${JSON.stringify(method)})`);
51 }
52
53 if (typeof shouldInjectPolyfill === "function") {
54 if (options.include || options.exclude) {
55 throw new Error(`.include and .exclude are not supported when using the` + ` .shouldInjectPolyfill function.`);
56 }
57 } else if (shouldInjectPolyfill != null) {
58 throw new Error(`.shouldInjectPolyfill must be a function, or undefined` + ` (received ${JSON.stringify(shouldInjectPolyfill)})`);
59 }
60
61 if (absoluteImports != null && typeof absoluteImports !== "boolean" && typeof absoluteImports !== "string") {
62 throw new Error(`.absoluteImports must be a boolean, a string, or undefined` + ` (received ${JSON.stringify(absoluteImports)})`);
63 }
64
65 let targets;
66
67 if ( // If any browserslist-related option is specified, fallback to the old
68 // behavior of not using the targets specified in the top-level options.
69 targetsOption || configPath || ignoreBrowserslistConfig) {
70 const targetsObj = typeof targetsOption === "string" || Array.isArray(targetsOption) ? {
71 browsers: targetsOption
72 } : targetsOption;
73 targets = getTargets(targetsObj, {
74 ignoreBrowserslistConfig,
75 configPath
76 });
77 } else {
78 targets = babelApi.targets();
79 }
80
81 return {
82 method,
83 methodName,
84 targets,
85 absoluteImports: absoluteImports != null ? absoluteImports : false,
86 shouldInjectPolyfill,
87 debug: !!debug,
88 providerOptions: providerOptions
89 };
90}
91
92function instantiateProvider(factory, options, missingDependencies, dirname, debugLog, babelApi) {
93 const {
94 method,
95 methodName,
96 targets,
97 debug,
98 shouldInjectPolyfill,
99 providerOptions,
100 absoluteImports
101 } = resolveOptions(options, babelApi);
102 const getUtils = (0, _utils.createUtilsGetter)(new _importsCache.default(moduleName => deps.resolve(dirname, moduleName, absoluteImports))); // eslint-disable-next-line prefer-const
103
104 let include, exclude;
105 let polyfillsSupport;
106 let polyfillsNames;
107 let filterPolyfills;
108 const depsCache = new Map();
109 const api = {
110 babel: babelApi,
111 getUtils,
112 method: options.method,
113 targets,
114 createMetaResolver: _metaResolver.default,
115
116 shouldInjectPolyfill(name) {
117 if (polyfillsNames === undefined) {
118 throw new Error(`Internal error in the ${factory.name} provider: ` + `shouldInjectPolyfill() can't be called during initialization.`);
119 }
120
121 if (!polyfillsNames.has(name)) {
122 console.warn(`Internal error in the ${provider.name} provider: ` + `unknown polyfill "${name}".`);
123 }
124
125 if (filterPolyfills && !filterPolyfills(name)) return false;
126 let shouldInject = (0, _helperCompilationTargets.isRequired)(name, targets, {
127 compatData: polyfillsSupport,
128 includes: include,
129 excludes: exclude
130 });
131
132 if (shouldInjectPolyfill) {
133 shouldInject = shouldInjectPolyfill(name, shouldInject);
134
135 if (typeof shouldInject !== "boolean") {
136 throw new Error(`.shouldInjectPolyfill must return a boolean.`);
137 }
138 }
139
140 return shouldInject;
141 },
142
143 debug(name) {
144 debugLog().found = true;
145 if (!debug || !name) return;
146 if (debugLog().polyfills.has(provider.name)) return;
147 debugLog().polyfills.set(name, polyfillsSupport && name && polyfillsSupport[name]);
148 },
149
150 assertDependency(name, version = "*") {
151 if (missingDependencies === false) return;
152
153 if (absoluteImports) {
154 // If absoluteImports is not false, we will try resolving
155 // the dependency and throw if it's not possible. We can
156 // skip the check here.
157 return;
158 }
159
160 const dep = version === "*" ? name : `${name}@^${version}`;
161 const found = missingDependencies.all ? false : mapGetOr(depsCache, `${name} :: ${dirname}`, () => deps.has(dirname, name));
162
163 if (!found) {
164 debugLog().missingDeps.add(dep);
165 }
166 }
167
168 };
169 const provider = factory(api, providerOptions, dirname);
170
171 if (typeof provider[methodName] !== "function") {
172 throw new Error(`The "${provider.name || factory.name}" provider doesn't ` + `support the "${method}" polyfilling method.`);
173 }
174
175 if (Array.isArray(provider.polyfills)) {
176 polyfillsNames = new Set(provider.polyfills);
177 filterPolyfills = provider.filterPolyfills;
178 } else if (provider.polyfills) {
179 polyfillsNames = new Set(Object.keys(provider.polyfills));
180 polyfillsSupport = provider.polyfills;
181 filterPolyfills = provider.filterPolyfills;
182 } else {
183 polyfillsNames = new Set();
184 }
185
186 ({
187 include,
188 exclude
189 } = (0, _normalizeOptions.validateIncludeExclude)(provider.name || factory.name, polyfillsNames, providerOptions.include || [], providerOptions.exclude || []));
190 return {
191 debug,
192 method,
193 targets,
194 provider,
195
196 callProvider(payload, path) {
197 const utils = getUtils(path); // $FlowIgnore
198
199 provider[methodName](payload, utils, path);
200 }
201
202 };
203}
204
205function definePolyfillProvider(factory) {
206 return (0, _helperPluginUtils.declare)((babelApi, options, dirname) => {
207 babelApi.assertVersion(7);
208 const {
209 traverse
210 } = babelApi;
211 let debugLog;
212 const missingDependencies = (0, _normalizeOptions.applyMissingDependenciesDefaults)(options, babelApi);
213 const {
214 debug,
215 method,
216 targets,
217 provider,
218 callProvider
219 } = instantiateProvider(factory, options, missingDependencies, dirname, () => debugLog, babelApi);
220 const createVisitor = method === "entry-global" ? v.entry : v.usage;
221 const visitor = provider.visitor ? traverse.visitors.merge([createVisitor(callProvider), provider.visitor]) : createVisitor(callProvider);
222
223 if (debug && debug !== _debugUtils.presetEnvSilentDebugHeader) {
224 console.log(`${provider.name}: \`DEBUG\` option`);
225 console.log(`\nUsing targets: ${(0, _debugUtils.stringifyTargetsMultiline)(targets)}`);
226 console.log(`\nUsing polyfills with \`${method}\` method:`);
227 }
228
229 return {
230 name: "inject-polyfills",
231 visitor,
232
233 pre() {
234 var _provider$pre;
235
236 debugLog = {
237 polyfills: new Map(),
238 found: false,
239 providers: new Set(),
240 missingDeps: new Set()
241 }; // $FlowIgnore - Flow doesn't support optional calls
242
243 (_provider$pre = provider.pre) == null ? void 0 : _provider$pre.apply(this, arguments);
244 },
245
246 post() {
247 var _provider$post;
248
249 // $FlowIgnore - Flow doesn't support optional calls
250 (_provider$post = provider.post) == null ? void 0 : _provider$post.apply(this, arguments);
251
252 if (missingDependencies !== false) {
253 if (missingDependencies.log === "per-file") {
254 deps.logMissing(debugLog.missingDeps);
255 } else {
256 deps.laterLogMissing(debugLog.missingDeps);
257 }
258 }
259
260 if (!debug) return;
261 if (this.filename) console.log(`\n[${this.filename}]`);
262
263 if (debugLog.polyfills.size === 0) {
264 console.log(method === "entry-global" ? debugLog.found ? `Based on your targets, the ${provider.name} polyfill did not add any polyfill.` : `The entry point for the ${provider.name} polyfill has not been found.` : `Based on your code and targets, the ${provider.name} polyfill did not add any polyfill.`);
265 return;
266 }
267
268 if (method === "entry-global") {
269 console.log(`The ${provider.name} polyfill entry has been replaced with ` + `the following polyfills:`);
270 } else {
271 console.log(`The ${provider.name} polyfill added the following polyfills:`);
272 }
273
274 for (const [name, support] of debugLog.polyfills) {
275 if (support) {
276 const filteredTargets = (0, _helperCompilationTargets.getInclusionReasons)(name, targets, support);
277 const formattedTargets = JSON.stringify(filteredTargets).replace(/,/g, ", ").replace(/^\{"/, '{ "').replace(/"\}$/, '" }');
278 console.log(` ${name} ${formattedTargets}`);
279 } else {
280 console.log(` ${name}`);
281 }
282 }
283 }
284
285 };
286 });
287}
288
289function mapGetOr(map, key, getDefault) {
290 let val = map.get(key);
291
292 if (val === undefined) {
293 val = getDefault();
294 map.set(key, val);
295 }
296
297 return val;
298}
Note: See TracBrowser for help on using the repository browser.