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