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