1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.default = void 0;
|
5 |
|
6 | var babel = _interopRequireWildcard(require("@babel/core"));
|
7 |
|
8 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
9 |
|
10 | function _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 |
|
12 | const {
|
13 | types: t
|
14 | } = babel.default || babel;
|
15 |
|
16 | class 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,
|
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 &&
|
59 |
|
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 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
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;
|
107 |
|
108 |
|
109 |
|
110 | return `${name && sourceType}::${url}::${name}`;
|
111 | }
|
112 |
|
113 | }
|
114 |
|
115 | exports.default = ImportsCache; |
\ | No newline at end of file |