source: frontend/node_modules/@babel/helper-define-polyfill-provider/lib/imports-injector.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.1 KB
Line 
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5var _babel = _interopRequireWildcard(require("@babel/core"));
6function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
7const {
8 types: t
9} = _babel.default || _babel;
10class ImportsCachedInjector {
11 constructor(resolver, getPreferredIndex) {
12 this._imports = new WeakMap();
13 this._anonymousImports = new WeakMap();
14 this._lastImports = new WeakMap();
15 this._resolver = resolver;
16 this._getPreferredIndex = getPreferredIndex;
17 }
18 storeAnonymous(programPath, url, moduleName, getVal) {
19 const key = this._normalizeKey(programPath, url);
20 const imports = this._ensure(this._anonymousImports, programPath, Set);
21 if (imports.has(key)) return;
22 const node = getVal(programPath.node.sourceType === "script", t.stringLiteral(this._resolver(url)));
23 imports.add(key);
24 this._injectImport(programPath, node, moduleName);
25 }
26 storeNamed(programPath, url, name, moduleName, getVal) {
27 const key = this._normalizeKey(programPath, url, name);
28 const imports = this._ensure(this._imports, programPath, Map);
29 if (!imports.has(key)) {
30 const {
31 node,
32 name: id
33 } = getVal(programPath.node.sourceType === "script", t.stringLiteral(this._resolver(url)), t.identifier(name));
34 imports.set(key, id);
35 this._injectImport(programPath, node, moduleName);
36 }
37 return t.identifier(imports.get(key));
38 }
39 _injectImport(programPath, node, moduleName) {
40 var _this$_lastImports$ge;
41 const newIndex = this._getPreferredIndex(moduleName);
42 const lastImports = (_this$_lastImports$ge = this._lastImports.get(programPath)) != null ? _this$_lastImports$ge : [];
43 const isPathStillValid = path => path.node &&
44 // Sometimes the AST is modified and the "last import"
45 // we have has been replaced
46 path.parent === programPath.node && path.container === programPath.node.body;
47 let last;
48 if (newIndex === Infinity) {
49 // Fast path: we can always just insert at the end if newIndex is `Infinity`
50 if (lastImports.length > 0) {
51 last = lastImports[lastImports.length - 1].path;
52 if (!isPathStillValid(last)) last = undefined;
53 }
54 } else {
55 for (const [i, data] of lastImports.entries()) {
56 const {
57 path,
58 index
59 } = data;
60 if (isPathStillValid(path)) {
61 if (newIndex < index) {
62 const [newPath] = path.insertBefore(node);
63 lastImports.splice(i, 0, {
64 path: newPath,
65 index: newIndex
66 });
67 return;
68 }
69 last = path;
70 }
71 }
72 }
73 if (last) {
74 const [newPath] = last.insertAfter(node);
75 lastImports.push({
76 path: newPath,
77 index: newIndex
78 });
79 } else {
80 const [newPath] = programPath.unshiftContainer("body", [node]);
81 this._lastImports.set(programPath, [{
82 path: newPath,
83 index: newIndex
84 }]);
85 }
86 }
87 _ensure(map, programPath, Collection) {
88 let collection = map.get(programPath);
89 if (!collection) {
90 collection = new Collection();
91 map.set(programPath, collection);
92 }
93 return collection;
94 }
95 _normalizeKey(programPath, url, name = "") {
96 const {
97 sourceType
98 } = programPath.node;
99
100 // If we rely on the imported binding (the "name" parameter), we also need to cache
101 // based on the sourceType. This is because the module transforms change the names
102 // of the import variables.
103 return `${name && sourceType}::${url}::${name}`;
104 }
105}
106exports.default = ImportsCachedInjector;
Note: See TracBrowser for help on using the repository browser.