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

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

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