@babel/helper-module-transforms
Advanced tools
Comparing version 7.22.20 to 7.23.0
@@ -15,3 +15,3 @@ "use strict"; | ||
function buildDynamicImport(node, deferToThen, wrapWithPromise, builder) { | ||
const [specifier] = node.arguments; | ||
const specifier = _core.types.isCallExpression(node) ? node.arguments[0] : node.source; | ||
if (_core.types.isStringLiteral(specifier) || _core.types.isTemplateLiteral(specifier) && specifier.quasis.length === 0) { | ||
@@ -18,0 +18,0 @@ if (deferToThen) { |
@@ -52,2 +52,3 @@ "use strict"; | ||
var _normalizeAndLoadMetadata = require("./normalize-and-load-metadata.js"); | ||
var Lazy = require("./lazy-modules.js"); | ||
var _dynamicImport = require("./dynamic-import.js"); | ||
@@ -81,2 +82,4 @@ var _getModuleName = require("./get-module-name.js"); | ||
lazy, | ||
getWrapperPayload = Lazy.toGetWrapperPayload(lazy != null ? lazy : false), | ||
wrapReference = Lazy.wrapReference, | ||
esNamespaceOnly, | ||
@@ -94,3 +97,3 @@ filename, | ||
initializeReexports: constantReexports, | ||
lazy, | ||
getWrapperPayload, | ||
esNamespaceOnly, | ||
@@ -102,3 +105,3 @@ filename | ||
} | ||
(0, _rewriteLiveReferences.default)(path, meta); | ||
(0, _rewriteLiveReferences.default)(path, meta, wrapReference); | ||
if (strictMode !== false) { | ||
@@ -121,3 +124,3 @@ const hasStrict = path.node.directives.some(directive => { | ||
} | ||
headers.push(...buildExportInitializationStatements(path, meta, constantReexports, noIncompleteNsImportDetection)); | ||
headers.push(...buildExportInitializationStatements(path, meta, wrapReference, constantReexports, noIncompleteNsImportDetection)); | ||
return { | ||
@@ -152,3 +155,4 @@ meta, | ||
} | ||
function buildNamespaceInitStatements(metadata, sourceMetadata, constantReexports = false) { | ||
function buildNamespaceInitStatements(metadata, sourceMetadata, constantReexports = false, wrapReference = Lazy.wrapReference) { | ||
var _wrapReference; | ||
const statements = []; | ||
@@ -163,8 +167,8 @@ const srcNamespaceId = identifier(sourceMetadata.name); | ||
} | ||
const srcNamespace = sourceMetadata.lazy ? callExpression(srcNamespaceId, []) : srcNamespaceId; | ||
const srcNamespace = (_wrapReference = wrapReference(srcNamespaceId, sourceMetadata.wrap)) != null ? _wrapReference : srcNamespaceId; | ||
if (constantReexports) { | ||
statements.push(...buildReexportsFromMeta(metadata, sourceMetadata, true)); | ||
statements.push(...buildReexportsFromMeta(metadata, sourceMetadata, true, wrapReference)); | ||
} | ||
for (const exportName of sourceMetadata.reexportNamespace) { | ||
statements.push((sourceMetadata.lazy ? _core.template.statement` | ||
statements.push((!_core.types.isIdentifier(srcNamespace) ? _core.template.statement` | ||
Object.defineProperty(EXPORTS, "NAME", { | ||
@@ -201,4 +205,6 @@ enumerable: true, | ||
}; | ||
function buildReexportsFromMeta(meta, metadata, constantReexports) { | ||
const namespace = metadata.lazy ? callExpression(identifier(metadata.name), []) : identifier(metadata.name); | ||
function buildReexportsFromMeta(meta, metadata, constantReexports, wrapReference) { | ||
var _wrapReference2; | ||
let namespace = identifier(metadata.name); | ||
namespace = (_wrapReference2 = wrapReference(namespace, metadata.wrap)) != null ? _wrapReference2 : namespace; | ||
const { | ||
@@ -298,3 +304,3 @@ stringSpecifiers | ||
} | ||
function buildExportInitializationStatements(programPath, metadata, constantReexports = false, noIncompleteNsImportDetection = false) { | ||
function buildExportInitializationStatements(programPath, metadata, wrapReference, constantReexports = false, noIncompleteNsImportDetection = false) { | ||
const initStatements = []; | ||
@@ -312,3 +318,3 @@ for (const [localName, data] of metadata.local) { | ||
if (!constantReexports) { | ||
const reexportsStatements = buildReexportsFromMeta(metadata, data, false); | ||
const reexportsStatements = buildReexportsFromMeta(metadata, data, false, wrapReference); | ||
const reexports = [...data.reexports.keys()]; | ||
@@ -315,0 +321,0 @@ for (let i = 0; i < reexportsStatements.length; i++) { |
@@ -34,3 +34,3 @@ "use strict"; | ||
initializeReexports = false, | ||
lazy = false, | ||
getWrapperPayload, | ||
esNamespaceOnly = false, | ||
@@ -50,3 +50,3 @@ filename | ||
initializeReexports, | ||
lazy | ||
getWrapperPayload | ||
}, stringSpecifiers); | ||
@@ -106,8 +106,9 @@ removeImportExportDeclarations(programPath); | ||
function getModuleMetadata(programPath, { | ||
lazy, | ||
getWrapperPayload, | ||
initializeReexports | ||
}, stringSpecifiers) { | ||
const localData = getLocalExportMetadata(programPath, initializeReexports, stringSpecifiers); | ||
const importNodes = new Map(); | ||
const sourceData = new Map(); | ||
const getData = sourceNode => { | ||
const getData = (sourceNode, node) => { | ||
const source = sourceNode.value; | ||
@@ -125,6 +126,12 @@ let data = sourceData.get(source); | ||
reexportAll: null, | ||
lazy: false, | ||
wrap: null, | ||
get lazy() { | ||
return this.wrap === "lazy"; | ||
}, | ||
referenced: false | ||
}; | ||
sourceData.set(source, data); | ||
importNodes.set(source, [node]); | ||
} else { | ||
importNodes.get(source).push(node); | ||
} | ||
@@ -136,3 +143,3 @@ return data; | ||
if (child.isImportDeclaration()) { | ||
const data = getData(child.node.source); | ||
const data = getData(child.node.source, child.node); | ||
if (!data.loc) data.loc = child.node.loc; | ||
@@ -178,3 +185,3 @@ child.get("specifiers").forEach(spec => { | ||
hasExports = true; | ||
const data = getData(child.node.source); | ||
const data = getData(child.node.source, child.node); | ||
if (!data.loc) data.loc = child.node.loc; | ||
@@ -187,3 +194,3 @@ data.reexportAll = { | ||
hasExports = true; | ||
const data = getData(child.node.source); | ||
const data = getData(child.node.source, child.node); | ||
if (!data.loc) data.loc = child.node.loc; | ||
@@ -226,13 +233,5 @@ child.get("specifiers").forEach(spec => { | ||
} | ||
for (const [source, metadata] of sourceData) { | ||
if (lazy !== false && !(isSideEffectImport(metadata) || metadata.reexportAll)) { | ||
if (lazy === true) { | ||
metadata.lazy = !/\./.test(source); | ||
} else if (Array.isArray(lazy)) { | ||
metadata.lazy = lazy.indexOf(source) !== -1; | ||
} else if (typeof lazy === "function") { | ||
metadata.lazy = lazy(source); | ||
} else { | ||
throw new Error(`.lazy must be a boolean, string array, or function`); | ||
} | ||
if (getWrapperPayload) { | ||
for (const [source, metadata] of sourceData) { | ||
metadata.wrap = getWrapperPayload(source, metadata, importNodes.get(source)); | ||
} | ||
@@ -239,0 +238,0 @@ } |
@@ -12,3 +12,2 @@ "use strict"; | ||
assignmentExpression, | ||
callExpression, | ||
cloneNode, | ||
@@ -18,2 +17,6 @@ expressionStatement, | ||
identifier, | ||
isArrowFunctionExpression, | ||
isClassExpression, | ||
isFunctionExpression, | ||
isIdentifier, | ||
isMemberExpression, | ||
@@ -48,3 +51,3 @@ isVariableDeclaration, | ||
} | ||
function rewriteLiveReferences(programPath, metadata) { | ||
function rewriteLiveReferences(programPath, metadata, wrapReference) { | ||
const imported = new Map(); | ||
@@ -89,8 +92,9 @@ const exported = new Map(); | ||
exported, | ||
buildImportReference: ([source, importName, localName], identNode) => { | ||
buildImportReference([source, importName, localName], identNode) { | ||
const meta = metadata.source.get(source); | ||
meta.referenced = true; | ||
if (localName) { | ||
if (meta.lazy) { | ||
identNode = callExpression(identNode, []); | ||
if (meta.wrap) { | ||
var _wrapReference; | ||
identNode = (_wrapReference = wrapReference(identNode, meta.wrap)) != null ? _wrapReference : identNode; | ||
} | ||
@@ -100,3 +104,6 @@ return identNode; | ||
let namespace = identifier(meta.name); | ||
if (meta.lazy) namespace = callExpression(namespace, []); | ||
if (meta.wrap) { | ||
var _wrapReference2; | ||
namespace = (_wrapReference2 = wrapReference(namespace, meta.wrap)) != null ? _wrapReference2 : namespace; | ||
} | ||
if (importName === "default" && meta.interop === "node-default") { | ||
@@ -139,10 +146,30 @@ return namespace; | ||
} = this; | ||
Object.keys(path.getOuterBindingIdentifiers()).forEach(localName => { | ||
const exportNames = exported.get(localName) || []; | ||
if (exportNames.length > 0) { | ||
const statement = expressionStatement(buildBindingExportAssignmentExpression(metadata, exportNames, identifier(localName), path.scope)); | ||
statement._blockHoist = path.node._blockHoist; | ||
requeueInParent(path.insertAfter(statement)[0]); | ||
const isVar = path.node.kind === "var"; | ||
for (const decl of path.get("declarations")) { | ||
const { | ||
id | ||
} = decl.node; | ||
let { | ||
init | ||
} = decl.node; | ||
if (isIdentifier(id) && exported.has(id.name) && !isArrowFunctionExpression(init) && (!isFunctionExpression(init) || init.id) && (!isClassExpression(init) || init.id)) { | ||
if (!init) { | ||
if (isVar) { | ||
continue; | ||
} else { | ||
init = path.scope.buildUndefinedNode(); | ||
} | ||
} | ||
decl.node.init = buildBindingExportAssignmentExpression(metadata, exported.get(id.name), init, path.scope); | ||
requeueInParent(decl.get("init")); | ||
} else { | ||
for (const localName of Object.keys(decl.getOuterBindingIdentifiers())) { | ||
if (exported.has(localName)) { | ||
const statement = expressionStatement(buildBindingExportAssignmentExpression(metadata, exported.get(localName), identifier(localName), path.scope)); | ||
statement._blockHoist = path.node._blockHoist; | ||
requeueInParent(path.insertAfter(statement)[0]); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
@@ -149,0 +176,0 @@ }; |
{ | ||
"name": "@babel/helper-module-transforms", | ||
"version": "7.22.20", | ||
"version": "7.23.0", | ||
"description": "Babel helper functions for implementing ES6 module transformations", | ||
@@ -25,4 +25,4 @@ "author": "The Babel Team (https://babel.dev/team)", | ||
"devDependencies": { | ||
"@babel/core": "^7.22.20", | ||
"@babel/traverse": "^7.22.20" | ||
"@babel/core": "^7.23.0", | ||
"@babel/traverse": "^7.23.0" | ||
}, | ||
@@ -29,0 +29,0 @@ "peerDependencies": { |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
157496
17
1246