@babel/helper-module-imports
Advanced tools
Comparing version 7.22.5 to 8.0.0-alpha.0
@@ -1,9 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _assert = require("assert"); | ||
var _t = require("@babel/types"); | ||
import assert from "assert"; | ||
import * as _t from "@babel/types"; | ||
const { | ||
@@ -23,7 +17,7 @@ callExpression, | ||
} = _t; | ||
class ImportBuilder { | ||
export default class ImportBuilder { | ||
_statements = []; | ||
_resultName = null; | ||
_importedSource; | ||
constructor(importedSource, scope, hub) { | ||
this._statements = []; | ||
this._resultName = null; | ||
this._importedSource = void 0; | ||
this._scope = scope; | ||
@@ -50,4 +44,4 @@ this._hub = hub; | ||
const statement = this._statements[this._statements.length - 1]; | ||
_assert(statement.type === "ImportDeclaration"); | ||
_assert(statement.specifiers.length === 0); | ||
assert(statement.type === "ImportDeclaration"); | ||
assert(statement.specifiers.length === 0); | ||
statement.specifiers = [importNamespaceSpecifier(local)]; | ||
@@ -60,4 +54,4 @@ this._resultName = cloneNode(local); | ||
const statement = this._statements[this._statements.length - 1]; | ||
_assert(statement.type === "ImportDeclaration"); | ||
_assert(statement.specifiers.length === 0); | ||
assert(statement.type === "ImportDeclaration"); | ||
assert(statement.specifiers.length === 0); | ||
statement.specifiers = [importDefaultSpecifier(id)]; | ||
@@ -71,4 +65,4 @@ this._resultName = cloneNode(id); | ||
const statement = this._statements[this._statements.length - 1]; | ||
_assert(statement.type === "ImportDeclaration"); | ||
_assert(statement.specifiers.length === 0); | ||
assert(statement.type === "ImportDeclaration"); | ||
assert(statement.specifiers.length === 0); | ||
statement.specifiers = [importSpecifier(id, identifier(importName))]; | ||
@@ -82,3 +76,3 @@ this._resultName = cloneNode(id); | ||
if (statement.type !== "ExpressionStatement") { | ||
_assert(this._resultName); | ||
assert(this._resultName); | ||
statement = expressionStatement(this._resultName); | ||
@@ -102,6 +96,6 @@ this._statements.push(statement); | ||
} else if (statement.type === "VariableDeclaration") { | ||
_assert(statement.declarations.length === 1); | ||
assert(statement.declarations.length === 1); | ||
statement.declarations[0].init = callExpression(callee, [statement.declarations[0].init]); | ||
} else { | ||
_assert.fail("Unexpected type."); | ||
assert.fail("Unexpected type."); | ||
} | ||
@@ -115,6 +109,6 @@ return this; | ||
} else if (statement.type === "VariableDeclaration") { | ||
_assert(statement.declarations.length === 1); | ||
assert(statement.declarations.length === 1); | ||
statement.declarations[0].init = memberExpression(statement.declarations[0].init, identifier(name)); | ||
} else { | ||
_assert.fail("Unexpected type:" + statement.type); | ||
assert.fail("Unexpected type:" + statement.type); | ||
} | ||
@@ -127,4 +121,3 @@ return this; | ||
} | ||
exports.default = ImportBuilder; | ||
//# sourceMappingURL=import-builder.js.map |
@@ -1,11 +0,3 @@ | ||
"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"); | ||
var _isModule = require("./is-module"); | ||
import assert from "assert"; | ||
import * as _t from "@babel/types"; | ||
const { | ||
@@ -15,13 +7,15 @@ numericLiteral, | ||
} = _t; | ||
class ImportInjector { | ||
import ImportBuilder from "./import-builder.js"; | ||
import isModule from "./is-module.js"; | ||
export default class ImportInjector { | ||
_defaultOpts = { | ||
importedSource: null, | ||
importedType: "commonjs", | ||
importedInterop: "babel", | ||
importingInterop: "babel", | ||
ensureLiveReference: false, | ||
ensureNoContext: false, | ||
importPosition: "before" | ||
}; | ||
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()); | ||
@@ -37,3 +31,3 @@ this._programPath = programPath; | ||
addNamed(importName, importedSourceIn, opts) { | ||
_assert(typeof importName === "string"); | ||
assert(typeof importName === "string"); | ||
return this._generateImport(this._applyDefaults(importedSourceIn, opts), importName); | ||
@@ -54,3 +48,3 @@ } | ||
} else { | ||
_assert(!opts, "Unexpected secondary arguments."); | ||
assert(!opts, "Unexpected secondary arguments."); | ||
newOpts = Object.assign({}, this._defaultOpts, importedSource); | ||
@@ -80,3 +74,3 @@ } | ||
let name = nameHint || importName; | ||
const isMod = (0, _isModule.default)(this._programPath); | ||
const isMod = isModule(this._programPath); | ||
const isModuleForNode = isMod && importingInterop === "node"; | ||
@@ -87,3 +81,3 @@ const isModuleForBabel = isMod && importingInterop === "babel"; | ||
} | ||
const builder = new _importBuilder.default(importedSource, this._programScope, this._hub); | ||
const builder = new ImportBuilder(importedSource, this._programScope, this._hub); | ||
if (importedType === "es6") { | ||
@@ -243,4 +237,3 @@ if (!isModuleForNode && !isModuleForBabel) { | ||
} | ||
exports.default = ImportInjector; | ||
//# sourceMappingURL=import-injector.js.map |
373
lib/index.js
@@ -1,37 +0,362 @@ | ||
"use strict"; | ||
import assert from 'assert'; | ||
import * as _t from '@babel/types'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "ImportInjector", { | ||
enumerable: true, | ||
get: function () { | ||
return _importInjector.default; | ||
const { | ||
callExpression, | ||
cloneNode, | ||
expressionStatement, | ||
identifier, | ||
importDeclaration, | ||
importDefaultSpecifier, | ||
importNamespaceSpecifier, | ||
importSpecifier, | ||
memberExpression, | ||
stringLiteral, | ||
variableDeclaration, | ||
variableDeclarator | ||
} = _t; | ||
class ImportBuilder { | ||
_statements = []; | ||
_resultName = null; | ||
_importedSource; | ||
constructor(importedSource, scope, hub) { | ||
this._scope = scope; | ||
this._hub = hub; | ||
this._importedSource = importedSource; | ||
} | ||
}); | ||
exports.addDefault = addDefault; | ||
exports.addNamed = addNamed; | ||
exports.addNamespace = addNamespace; | ||
exports.addSideEffect = addSideEffect; | ||
Object.defineProperty(exports, "isModule", { | ||
enumerable: true, | ||
get: function () { | ||
return _isModule.default; | ||
done() { | ||
return { | ||
statements: this._statements, | ||
resultName: this._resultName | ||
}; | ||
} | ||
}); | ||
var _importInjector = require("./import-injector"); | ||
var _isModule = require("./is-module"); | ||
import() { | ||
this._statements.push(importDeclaration([], stringLiteral(this._importedSource))); | ||
return this; | ||
} | ||
require() { | ||
this._statements.push(expressionStatement(callExpression(identifier("require"), [stringLiteral(this._importedSource)]))); | ||
return this; | ||
} | ||
namespace(name = "namespace") { | ||
const local = this._scope.generateUidIdentifier(name); | ||
const statement = this._statements[this._statements.length - 1]; | ||
assert(statement.type === "ImportDeclaration"); | ||
assert(statement.specifiers.length === 0); | ||
statement.specifiers = [importNamespaceSpecifier(local)]; | ||
this._resultName = cloneNode(local); | ||
return this; | ||
} | ||
default(name) { | ||
const id = this._scope.generateUidIdentifier(name); | ||
const statement = this._statements[this._statements.length - 1]; | ||
assert(statement.type === "ImportDeclaration"); | ||
assert(statement.specifiers.length === 0); | ||
statement.specifiers = [importDefaultSpecifier(id)]; | ||
this._resultName = cloneNode(id); | ||
return this; | ||
} | ||
named(name, importName) { | ||
if (importName === "default") return this.default(name); | ||
const id = this._scope.generateUidIdentifier(name); | ||
const statement = this._statements[this._statements.length - 1]; | ||
assert(statement.type === "ImportDeclaration"); | ||
assert(statement.specifiers.length === 0); | ||
statement.specifiers = [importSpecifier(id, identifier(importName))]; | ||
this._resultName = cloneNode(id); | ||
return this; | ||
} | ||
var(name) { | ||
const id = this._scope.generateUidIdentifier(name); | ||
let statement = this._statements[this._statements.length - 1]; | ||
if (statement.type !== "ExpressionStatement") { | ||
assert(this._resultName); | ||
statement = expressionStatement(this._resultName); | ||
this._statements.push(statement); | ||
} | ||
this._statements[this._statements.length - 1] = variableDeclaration("var", [variableDeclarator(id, statement.expression)]); | ||
this._resultName = cloneNode(id); | ||
return this; | ||
} | ||
defaultInterop() { | ||
return this._interop(this._hub.addHelper("interopRequireDefault")); | ||
} | ||
wildcardInterop() { | ||
return this._interop(this._hub.addHelper("interopRequireWildcard")); | ||
} | ||
_interop(callee) { | ||
const statement = this._statements[this._statements.length - 1]; | ||
if (statement.type === "ExpressionStatement") { | ||
statement.expression = callExpression(callee, [statement.expression]); | ||
} else if (statement.type === "VariableDeclaration") { | ||
assert(statement.declarations.length === 1); | ||
statement.declarations[0].init = callExpression(callee, [statement.declarations[0].init]); | ||
} else { | ||
assert.fail("Unexpected type."); | ||
} | ||
return this; | ||
} | ||
prop(name) { | ||
const statement = this._statements[this._statements.length - 1]; | ||
if (statement.type === "ExpressionStatement") { | ||
statement.expression = memberExpression(statement.expression, identifier(name)); | ||
} else if (statement.type === "VariableDeclaration") { | ||
assert(statement.declarations.length === 1); | ||
statement.declarations[0].init = memberExpression(statement.declarations[0].init, identifier(name)); | ||
} else { | ||
assert.fail("Unexpected type:" + statement.type); | ||
} | ||
return this; | ||
} | ||
read(name) { | ||
this._resultName = memberExpression(this._resultName, identifier(name)); | ||
} | ||
} | ||
function isModule(path) { | ||
return path.node.sourceType === "module"; | ||
} | ||
const { | ||
numericLiteral, | ||
sequenceExpression | ||
} = _t; | ||
class ImportInjector { | ||
_defaultOpts = { | ||
importedSource: null, | ||
importedType: "commonjs", | ||
importedInterop: "babel", | ||
importingInterop: "babel", | ||
ensureLiveReference: false, | ||
ensureNoContext: false, | ||
importPosition: "before" | ||
}; | ||
constructor(path, importedSource, opts) { | ||
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 = isModule(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(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) { | ||
const body = this._programPath.get("body"); | ||
if (importPosition === "after") { | ||
for (let i = body.length - 1; i >= 0; i--) { | ||
if (body[i].isImportDeclaration()) { | ||
body[i].insertAfter(statements); | ||
return; | ||
} | ||
} | ||
} else { | ||
statements.forEach(node => { | ||
node._blockHoist = blockHoist; | ||
}); | ||
const targetPath = body.find(p => { | ||
const val = p.node._blockHoist; | ||
return Number.isFinite(val) && val < 4; | ||
}); | ||
if (targetPath) { | ||
targetPath.insertBefore(statements); | ||
return; | ||
} | ||
} | ||
this._programPath.unshiftContainer("body", statements); | ||
} | ||
} | ||
function addDefault(path, importedSource, opts) { | ||
return new _importInjector.default(path).addDefault(importedSource, opts); | ||
return new ImportInjector(path).addDefault(importedSource, opts); | ||
} | ||
function addNamed(path, name, importedSource, opts) { | ||
return new _importInjector.default(path).addNamed(name, importedSource, opts); | ||
return new ImportInjector(path).addNamed(name, importedSource, opts); | ||
} | ||
function addNamespace(path, importedSource, opts) { | ||
return new _importInjector.default(path).addNamespace(importedSource, opts); | ||
return new ImportInjector(path).addNamespace(importedSource, opts); | ||
} | ||
function addSideEffect(path, importedSource, opts) { | ||
return new _importInjector.default(path).addSideEffect(importedSource, opts); | ||
return new ImportInjector(path).addSideEffect(importedSource, opts); | ||
} | ||
export { ImportInjector, addDefault, addNamed, addNamespace, addSideEffect, isModule }; | ||
//# sourceMappingURL=index.js.map |
@@ -1,8 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = isModule; | ||
function isModule(path) { | ||
export default function isModule(path) { | ||
return path.node.sourceType === "module"; | ||
@@ -9,0 +3,0 @@ } |
{ | ||
"name": "@babel/helper-module-imports", | ||
"version": "7.22.5", | ||
"version": "8.0.0-alpha.0", | ||
"description": "Babel helper functions for inserting module loads", | ||
@@ -18,12 +18,16 @@ "author": "The Babel Team (https://babel.dev/team)", | ||
"dependencies": { | ||
"@babel/types": "^7.22.5" | ||
"@babel/types": "^8.0.0-alpha.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.22.5", | ||
"@babel/traverse": "^7.22.5" | ||
"@babel/core": "^8.0.0-alpha.0", | ||
"@babel/traverse": "^8.0.0-alpha.0" | ||
}, | ||
"engines": { | ||
"node": ">=6.9.0" | ||
"node": "^16.20.0 || ^18.16.0 || >=20.0.0" | ||
}, | ||
"type": "commonjs" | ||
"exports": { | ||
".": "./lib/index.js", | ||
"./package.json": "./package.json" | ||
}, | ||
"type": "module" | ||
} |
@@ -5,3 +5,3 @@ # @babel/helper-module-imports | ||
See our website [@babel/helper-module-imports](https://babeljs.io/docs/en/babel-helper-module-imports) for more information. | ||
See our website [@babel/helper-module-imports](https://babeljs.io/docs/babel-helper-module-imports) for more information. | ||
@@ -8,0 +8,0 @@ ## Install |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
101280
707
Yes
1
+ Added@babel/helper-string-parser@8.0.0-alpha.12(transitive)
+ Added@babel/helper-validator-identifier@8.0.0-alpha.12(transitive)
+ Added@babel/types@8.0.0-alpha.12(transitive)
+ Addedto-fast-properties@3.0.1(transitive)
- Removed@babel/helper-string-parser@7.24.8(transitive)
- Removed@babel/helper-validator-identifier@7.24.7(transitive)
- Removed@babel/types@7.25.2(transitive)
- Removedto-fast-properties@2.0.0(transitive)
Updated@babel/types@^8.0.0-alpha.0