1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 |
|
---|
8 | var _assert = require("assert");
|
---|
9 |
|
---|
10 | var _t = require("@babel/types");
|
---|
11 |
|
---|
12 | var _importBuilder = require("./import-builder");
|
---|
13 |
|
---|
14 | var _isModule = require("./is-module");
|
---|
15 |
|
---|
16 | const {
|
---|
17 | numericLiteral,
|
---|
18 | sequenceExpression
|
---|
19 | } = _t;
|
---|
20 |
|
---|
21 | class ImportInjector {
|
---|
22 | constructor(path, importedSource, opts) {
|
---|
23 | this._defaultOpts = {
|
---|
24 | importedSource: null,
|
---|
25 | importedType: "commonjs",
|
---|
26 | importedInterop: "babel",
|
---|
27 | importingInterop: "babel",
|
---|
28 | ensureLiveReference: false,
|
---|
29 | ensureNoContext: false,
|
---|
30 | importPosition: "before"
|
---|
31 | };
|
---|
32 | const programPath = path.find(p => p.isProgram());
|
---|
33 | this._programPath = programPath;
|
---|
34 | this._programScope = programPath.scope;
|
---|
35 | this._hub = programPath.hub;
|
---|
36 | this._defaultOpts = this._applyDefaults(importedSource, opts, true);
|
---|
37 | }
|
---|
38 |
|
---|
39 | addDefault(importedSourceIn, opts) {
|
---|
40 | return this.addNamed("default", importedSourceIn, opts);
|
---|
41 | }
|
---|
42 |
|
---|
43 | addNamed(importName, importedSourceIn, opts) {
|
---|
44 | _assert(typeof importName === "string");
|
---|
45 |
|
---|
46 | return this._generateImport(this._applyDefaults(importedSourceIn, opts), importName);
|
---|
47 | }
|
---|
48 |
|
---|
49 | addNamespace(importedSourceIn, opts) {
|
---|
50 | return this._generateImport(this._applyDefaults(importedSourceIn, opts), null);
|
---|
51 | }
|
---|
52 |
|
---|
53 | addSideEffect(importedSourceIn, opts) {
|
---|
54 | return this._generateImport(this._applyDefaults(importedSourceIn, opts), false);
|
---|
55 | }
|
---|
56 |
|
---|
57 | _applyDefaults(importedSource, opts, isInit = false) {
|
---|
58 | const optsList = [];
|
---|
59 |
|
---|
60 | if (typeof importedSource === "string") {
|
---|
61 | optsList.push({
|
---|
62 | importedSource
|
---|
63 | });
|
---|
64 | optsList.push(opts);
|
---|
65 | } else {
|
---|
66 | _assert(!opts, "Unexpected secondary arguments.");
|
---|
67 |
|
---|
68 | optsList.push(importedSource);
|
---|
69 | }
|
---|
70 |
|
---|
71 | const newOpts = Object.assign({}, this._defaultOpts);
|
---|
72 |
|
---|
73 | for (const opts of optsList) {
|
---|
74 | if (!opts) continue;
|
---|
75 | Object.keys(newOpts).forEach(key => {
|
---|
76 | if (opts[key] !== undefined) newOpts[key] = opts[key];
|
---|
77 | });
|
---|
78 |
|
---|
79 | if (!isInit) {
|
---|
80 | if (opts.nameHint !== undefined) newOpts.nameHint = opts.nameHint;
|
---|
81 | if (opts.blockHoist !== undefined) newOpts.blockHoist = opts.blockHoist;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | return newOpts;
|
---|
86 | }
|
---|
87 |
|
---|
88 | _generateImport(opts, importName) {
|
---|
89 | const isDefault = importName === "default";
|
---|
90 | const isNamed = !!importName && !isDefault;
|
---|
91 | const isNamespace = importName === null;
|
---|
92 | const {
|
---|
93 | importedSource,
|
---|
94 | importedType,
|
---|
95 | importedInterop,
|
---|
96 | importingInterop,
|
---|
97 | ensureLiveReference,
|
---|
98 | ensureNoContext,
|
---|
99 | nameHint,
|
---|
100 | importPosition,
|
---|
101 | blockHoist
|
---|
102 | } = opts;
|
---|
103 | let name = nameHint || importName;
|
---|
104 | const isMod = (0, _isModule.default)(this._programPath);
|
---|
105 | const isModuleForNode = isMod && importingInterop === "node";
|
---|
106 | const isModuleForBabel = isMod && importingInterop === "babel";
|
---|
107 |
|
---|
108 | if (importPosition === "after" && !isMod) {
|
---|
109 | throw new Error(`"importPosition": "after" is only supported in modules`);
|
---|
110 | }
|
---|
111 |
|
---|
112 | const builder = new _importBuilder.default(importedSource, this._programScope, this._hub);
|
---|
113 |
|
---|
114 | if (importedType === "es6") {
|
---|
115 | if (!isModuleForNode && !isModuleForBabel) {
|
---|
116 | throw new Error("Cannot import an ES6 module from CommonJS");
|
---|
117 | }
|
---|
118 |
|
---|
119 | builder.import();
|
---|
120 |
|
---|
121 | if (isNamespace) {
|
---|
122 | builder.namespace(nameHint || importedSource);
|
---|
123 | } else if (isDefault || isNamed) {
|
---|
124 | builder.named(name, importName);
|
---|
125 | }
|
---|
126 | } else if (importedType !== "commonjs") {
|
---|
127 | throw new Error(`Unexpected interopType "${importedType}"`);
|
---|
128 | } else if (importedInterop === "babel") {
|
---|
129 | if (isModuleForNode) {
|
---|
130 | name = name !== "default" ? name : importedSource;
|
---|
131 | const es6Default = `${importedSource}$es6Default`;
|
---|
132 | builder.import();
|
---|
133 |
|
---|
134 | if (isNamespace) {
|
---|
135 | builder.default(es6Default).var(name || importedSource).wildcardInterop();
|
---|
136 | } else if (isDefault) {
|
---|
137 | if (ensureLiveReference) {
|
---|
138 | builder.default(es6Default).var(name || importedSource).defaultInterop().read("default");
|
---|
139 | } else {
|
---|
140 | builder.default(es6Default).var(name).defaultInterop().prop(importName);
|
---|
141 | }
|
---|
142 | } else if (isNamed) {
|
---|
143 | builder.default(es6Default).read(importName);
|
---|
144 | }
|
---|
145 | } else if (isModuleForBabel) {
|
---|
146 | builder.import();
|
---|
147 |
|
---|
148 | if (isNamespace) {
|
---|
149 | builder.namespace(name || importedSource);
|
---|
150 | } else if (isDefault || isNamed) {
|
---|
151 | builder.named(name, importName);
|
---|
152 | }
|
---|
153 | } else {
|
---|
154 | builder.require();
|
---|
155 |
|
---|
156 | if (isNamespace) {
|
---|
157 | builder.var(name || importedSource).wildcardInterop();
|
---|
158 | } else if ((isDefault || isNamed) && ensureLiveReference) {
|
---|
159 | if (isDefault) {
|
---|
160 | name = name !== "default" ? name : importedSource;
|
---|
161 | builder.var(name).read(importName);
|
---|
162 | builder.defaultInterop();
|
---|
163 | } else {
|
---|
164 | builder.var(importedSource).read(importName);
|
---|
165 | }
|
---|
166 | } else if (isDefault) {
|
---|
167 | builder.var(name).defaultInterop().prop(importName);
|
---|
168 | } else if (isNamed) {
|
---|
169 | builder.var(name).prop(importName);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | } else if (importedInterop === "compiled") {
|
---|
173 | if (isModuleForNode) {
|
---|
174 | builder.import();
|
---|
175 |
|
---|
176 | if (isNamespace) {
|
---|
177 | builder.default(name || importedSource);
|
---|
178 | } else if (isDefault || isNamed) {
|
---|
179 | builder.default(importedSource).read(name);
|
---|
180 | }
|
---|
181 | } else if (isModuleForBabel) {
|
---|
182 | builder.import();
|
---|
183 |
|
---|
184 | if (isNamespace) {
|
---|
185 | builder.namespace(name || importedSource);
|
---|
186 | } else if (isDefault || isNamed) {
|
---|
187 | builder.named(name, importName);
|
---|
188 | }
|
---|
189 | } else {
|
---|
190 | builder.require();
|
---|
191 |
|
---|
192 | if (isNamespace) {
|
---|
193 | builder.var(name || importedSource);
|
---|
194 | } else if (isDefault || isNamed) {
|
---|
195 | if (ensureLiveReference) {
|
---|
196 | builder.var(importedSource).read(name);
|
---|
197 | } else {
|
---|
198 | builder.prop(importName).var(name);
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 | } else if (importedInterop === "uncompiled") {
|
---|
203 | if (isDefault && ensureLiveReference) {
|
---|
204 | throw new Error("No live reference for commonjs default");
|
---|
205 | }
|
---|
206 |
|
---|
207 | if (isModuleForNode) {
|
---|
208 | builder.import();
|
---|
209 |
|
---|
210 | if (isNamespace) {
|
---|
211 | builder.default(name || importedSource);
|
---|
212 | } else if (isDefault) {
|
---|
213 | builder.default(name);
|
---|
214 | } else if (isNamed) {
|
---|
215 | builder.default(importedSource).read(name);
|
---|
216 | }
|
---|
217 | } else if (isModuleForBabel) {
|
---|
218 | builder.import();
|
---|
219 |
|
---|
220 | if (isNamespace) {
|
---|
221 | builder.default(name || importedSource);
|
---|
222 | } else if (isDefault) {
|
---|
223 | builder.default(name);
|
---|
224 | } else if (isNamed) {
|
---|
225 | builder.named(name, importName);
|
---|
226 | }
|
---|
227 | } else {
|
---|
228 | builder.require();
|
---|
229 |
|
---|
230 | if (isNamespace) {
|
---|
231 | builder.var(name || importedSource);
|
---|
232 | } else if (isDefault) {
|
---|
233 | builder.var(name);
|
---|
234 | } else if (isNamed) {
|
---|
235 | if (ensureLiveReference) {
|
---|
236 | builder.var(importedSource).read(name);
|
---|
237 | } else {
|
---|
238 | builder.var(name).prop(importName);
|
---|
239 | }
|
---|
240 | }
|
---|
241 | }
|
---|
242 | } else {
|
---|
243 | throw new Error(`Unknown importedInterop "${importedInterop}".`);
|
---|
244 | }
|
---|
245 |
|
---|
246 | const {
|
---|
247 | statements,
|
---|
248 | resultName
|
---|
249 | } = builder.done();
|
---|
250 |
|
---|
251 | this._insertStatements(statements, importPosition, blockHoist);
|
---|
252 |
|
---|
253 | if ((isDefault || isNamed) && ensureNoContext && resultName.type !== "Identifier") {
|
---|
254 | return sequenceExpression([numericLiteral(0), resultName]);
|
---|
255 | }
|
---|
256 |
|
---|
257 | return resultName;
|
---|
258 | }
|
---|
259 |
|
---|
260 | _insertStatements(statements, importPosition = "before", blockHoist = 3) {
|
---|
261 | const body = this._programPath.get("body");
|
---|
262 |
|
---|
263 | if (importPosition === "after") {
|
---|
264 | for (let i = body.length - 1; i >= 0; i--) {
|
---|
265 | if (body[i].isImportDeclaration()) {
|
---|
266 | body[i].insertAfter(statements);
|
---|
267 | return;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | } else {
|
---|
271 | statements.forEach(node => {
|
---|
272 | node._blockHoist = blockHoist;
|
---|
273 | });
|
---|
274 | const targetPath = body.find(p => {
|
---|
275 | const val = p.node._blockHoist;
|
---|
276 | return Number.isFinite(val) && val < 4;
|
---|
277 | });
|
---|
278 |
|
---|
279 | if (targetPath) {
|
---|
280 | targetPath.insertBefore(statements);
|
---|
281 | return;
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | this._programPath.unshiftContainer("body", statements);
|
---|
286 | }
|
---|
287 |
|
---|
288 | }
|
---|
289 |
|
---|
290 | exports.default = ImportInjector; |
---|