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