1 | "use strict";
|
---|
2 |
|
---|
3 | exports.__esModule = true;
|
---|
4 | exports.createUtilsGetter = createUtilsGetter;
|
---|
5 | exports.getImportSource = getImportSource;
|
---|
6 | exports.getRequireSource = getRequireSource;
|
---|
7 | exports.has = has;
|
---|
8 | exports.intersection = intersection;
|
---|
9 | exports.resolveKey = resolveKey;
|
---|
10 | exports.resolveSource = resolveSource;
|
---|
11 | var _babel = _interopRequireWildcard(require("@babel/core"));
|
---|
12 | function _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); }
|
---|
13 | function _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; }
|
---|
14 | const {
|
---|
15 | types: t,
|
---|
16 | template: template
|
---|
17 | } = _babel.default || _babel;
|
---|
18 | function intersection(a, b) {
|
---|
19 | const result = new Set();
|
---|
20 | a.forEach(v => b.has(v) && result.add(v));
|
---|
21 | return result;
|
---|
22 | }
|
---|
23 | function has(object, key) {
|
---|
24 | return Object.prototype.hasOwnProperty.call(object, key);
|
---|
25 | }
|
---|
26 | function getType(target) {
|
---|
27 | return Object.prototype.toString.call(target).slice(8, -1);
|
---|
28 | }
|
---|
29 | function resolveId(path) {
|
---|
30 | if (path.isIdentifier() && !path.scope.hasBinding(path.node.name, /* noGlobals */true)) {
|
---|
31 | return path.node.name;
|
---|
32 | }
|
---|
33 | if (path.isPure()) {
|
---|
34 | const {
|
---|
35 | deopt
|
---|
36 | } = path.evaluate();
|
---|
37 | if (deopt && deopt.isIdentifier()) {
|
---|
38 | return deopt.node.name;
|
---|
39 | }
|
---|
40 | }
|
---|
41 | }
|
---|
42 | function resolveKey(path, computed = false) {
|
---|
43 | const {
|
---|
44 | scope
|
---|
45 | } = path;
|
---|
46 | if (path.isStringLiteral()) return path.node.value;
|
---|
47 | const isIdentifier = path.isIdentifier();
|
---|
48 | if (isIdentifier && !(computed || path.parent.computed)) {
|
---|
49 | return path.node.name;
|
---|
50 | }
|
---|
51 | if (computed && path.isMemberExpression() && path.get("object").isIdentifier({
|
---|
52 | name: "Symbol"
|
---|
53 | }) && !scope.hasBinding("Symbol", /* noGlobals */true)) {
|
---|
54 | const sym = resolveKey(path.get("property"), path.node.computed);
|
---|
55 | if (sym) return "Symbol." + sym;
|
---|
56 | }
|
---|
57 | if (isIdentifier ? scope.hasBinding(path.node.name, /* noGlobals */true) : path.isPure()) {
|
---|
58 | const {
|
---|
59 | value
|
---|
60 | } = path.evaluate();
|
---|
61 | if (typeof value === "string") return value;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | function resolveSource(obj) {
|
---|
65 | if (obj.isMemberExpression() && obj.get("property").isIdentifier({
|
---|
66 | name: "prototype"
|
---|
67 | })) {
|
---|
68 | const id = resolveId(obj.get("object"));
|
---|
69 | if (id) {
|
---|
70 | return {
|
---|
71 | id,
|
---|
72 | placement: "prototype"
|
---|
73 | };
|
---|
74 | }
|
---|
75 | return {
|
---|
76 | id: null,
|
---|
77 | placement: null
|
---|
78 | };
|
---|
79 | }
|
---|
80 | const id = resolveId(obj);
|
---|
81 | if (id) {
|
---|
82 | return {
|
---|
83 | id,
|
---|
84 | placement: "static"
|
---|
85 | };
|
---|
86 | }
|
---|
87 | if (obj.isRegExpLiteral()) {
|
---|
88 | return {
|
---|
89 | id: "RegExp",
|
---|
90 | placement: "prototype"
|
---|
91 | };
|
---|
92 | } else if (obj.isFunction()) {
|
---|
93 | return {
|
---|
94 | id: "Function",
|
---|
95 | placement: "prototype"
|
---|
96 | };
|
---|
97 | } else if (obj.isPure()) {
|
---|
98 | const {
|
---|
99 | value
|
---|
100 | } = obj.evaluate();
|
---|
101 | if (value !== undefined) {
|
---|
102 | return {
|
---|
103 | id: getType(value),
|
---|
104 | placement: "prototype"
|
---|
105 | };
|
---|
106 | }
|
---|
107 | }
|
---|
108 | return {
|
---|
109 | id: null,
|
---|
110 | placement: null
|
---|
111 | };
|
---|
112 | }
|
---|
113 | function getImportSource({
|
---|
114 | node
|
---|
115 | }) {
|
---|
116 | if (node.specifiers.length === 0) return node.source.value;
|
---|
117 | }
|
---|
118 | function getRequireSource({
|
---|
119 | node
|
---|
120 | }) {
|
---|
121 | if (!t.isExpressionStatement(node)) return;
|
---|
122 | const {
|
---|
123 | expression
|
---|
124 | } = node;
|
---|
125 | if (t.isCallExpression(expression) && t.isIdentifier(expression.callee) && expression.callee.name === "require" && expression.arguments.length === 1 && t.isStringLiteral(expression.arguments[0])) {
|
---|
126 | return expression.arguments[0].value;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | function hoist(node) {
|
---|
130 | // @ts-expect-error
|
---|
131 | node._blockHoist = 3;
|
---|
132 | return node;
|
---|
133 | }
|
---|
134 | function createUtilsGetter(cache) {
|
---|
135 | return path => {
|
---|
136 | const prog = path.findParent(p => p.isProgram());
|
---|
137 | return {
|
---|
138 | injectGlobalImport(url, moduleName) {
|
---|
139 | cache.storeAnonymous(prog, url, moduleName, (isScript, source) => {
|
---|
140 | return isScript ? template.statement.ast`require(${source})` : t.importDeclaration([], source);
|
---|
141 | });
|
---|
142 | },
|
---|
143 | injectNamedImport(url, name, hint = name, moduleName) {
|
---|
144 | return cache.storeNamed(prog, url, name, moduleName, (isScript, source, name) => {
|
---|
145 | const id = prog.scope.generateUidIdentifier(hint);
|
---|
146 | return {
|
---|
147 | node: isScript ? hoist(template.statement.ast`
|
---|
148 | var ${id} = require(${source}).${name}
|
---|
149 | `) : t.importDeclaration([t.importSpecifier(id, name)], source),
|
---|
150 | name: id.name
|
---|
151 | };
|
---|
152 | });
|
---|
153 | },
|
---|
154 | injectDefaultImport(url, hint = url, moduleName) {
|
---|
155 | return cache.storeNamed(prog, url, "default", moduleName, (isScript, source) => {
|
---|
156 | const id = prog.scope.generateUidIdentifier(hint);
|
---|
157 | return {
|
---|
158 | node: isScript ? hoist(template.statement.ast`var ${id} = require(${source})`) : t.importDeclaration([t.importDefaultSpecifier(id)], source),
|
---|
159 | name: id.name
|
---|
160 | };
|
---|
161 | });
|
---|
162 | }
|
---|
163 | };
|
---|
164 | };
|
---|
165 | } |
---|