source: trip-planner-front/node_modules/@babel/helper-define-polyfill-provider/lib/imports-cache.js@ ceaed42

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

initial commit

  • Property mode set to 100644
File size: 3.9 KB
Line 
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var babel = _interopRequireWildcard(require("@babel/core"));
7
8function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
9
10function _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; }
11
12const {
13 types: t
14} = babel.default || babel;
15
16class ImportsCache {
17 constructor(resolver) {
18 this._imports = new WeakMap();
19 this._anonymousImports = new WeakMap();
20 this._lastImports = new WeakMap();
21 this._resolver = resolver;
22 }
23
24 storeAnonymous(programPath, url, // eslint-disable-next-line no-undef
25 getVal) {
26 const key = this._normalizeKey(programPath, url);
27
28 const imports = this._ensure(this._anonymousImports, programPath, Set);
29
30 if (imports.has(key)) return;
31 const node = getVal(programPath.node.sourceType === "script", t.stringLiteral(this._resolver(url)));
32 imports.add(key);
33
34 this._injectImport(programPath, node);
35 }
36
37 storeNamed(programPath, url, name, getVal) {
38 const key = this._normalizeKey(programPath, url, name);
39
40 const imports = this._ensure(this._imports, programPath, Map);
41
42 if (!imports.has(key)) {
43 const {
44 node,
45 name: id
46 } = getVal(programPath.node.sourceType === "script", t.stringLiteral(this._resolver(url)), t.identifier(name));
47 imports.set(key, id);
48
49 this._injectImport(programPath, node);
50 }
51
52 return t.identifier(imports.get(key));
53 }
54
55 _injectImport(programPath, node) {
56 let lastImport = this._lastImports.get(programPath);
57
58 if (lastImport && lastImport.node && // Sometimes the AST is modified and the "last import"
59 // we have has been replaced
60 lastImport.parent === programPath.node && lastImport.container === programPath.node.body) {
61 lastImport = lastImport.insertAfter(node);
62 } else {
63 lastImport = programPath.unshiftContainer("body", node);
64 }
65
66 lastImport = lastImport[lastImport.length - 1];
67
68 this._lastImports.set(programPath, lastImport);
69 /*
70 let lastImport;
71 programPath.get("body").forEach(path => {
72 if (path.isImportDeclaration()) lastImport = path;
73 if (
74 path.isExpressionStatement() &&
75 isRequireCall(path.get("expression"))
76 ) {
77 lastImport = path;
78 }
79 if (
80 path.isVariableDeclaration() &&
81 path.get("declarations").length === 1 &&
82 (isRequireCall(path.get("declarations.0.init")) ||
83 (path.get("declarations.0.init").isMemberExpression() &&
84 isRequireCall(path.get("declarations.0.init.object"))))
85 ) {
86 lastImport = path;
87 }
88 });*/
89
90 }
91
92 _ensure(map, programPath, Collection) {
93 let collection = map.get(programPath);
94
95 if (!collection) {
96 collection = new Collection();
97 map.set(programPath, collection);
98 }
99
100 return collection;
101 }
102
103 _normalizeKey(programPath, url, name = "") {
104 const {
105 sourceType
106 } = programPath.node; // If we rely on the imported binding (the "name" parameter), we also need to cache
107 // based on the sourceType. This is because the module transforms change the names
108 // of the import variables.
109
110 return `${name && sourceType}::${url}::${name}`;
111 }
112
113}
114
115exports.default = ImportsCache;
Note: See TracBrowser for help on using the repository browser.