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