-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Fix missing support for named re-exports #53
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,9 +37,18 @@ export function cjsInterop(options: CjsInteropOptions): Plugin { | |
const { client = false, apply = "both" } = options; | ||
|
||
let sourcemaps = false; | ||
|
||
const matchedDependencies: Record<string, boolean> = {}; | ||
|
||
const matchesDependencies = (value: string) => { | ||
return dependencies.some((dependency) => minimatch(value, dependency)); | ||
if (!(value in matchedDependencies)) { | ||
matchedDependencies[value] = dependencies.some((dependency) => | ||
minimatch(value, dependency), | ||
); | ||
} | ||
return matchedDependencies[value]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an unrelated optimization; Some memoizing here speeds the plugin to a good amount. This makes our builds 1s faster with no apparent downside. |
||
}; | ||
|
||
return { | ||
name: "cjs-interop", | ||
enforce: "post", | ||
|
@@ -61,53 +70,119 @@ export function cjsInterop(options: CjsInteropOptions): Plugin { | |
}); | ||
|
||
const toBeFixed: any[] = []; | ||
const dynamicImportsToBeFixed: any[] = []; | ||
const preambles: string[] = []; | ||
let hasDynamicImportsToFix = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For simplicity, all fixes are included in toBeFixed. The dynamicImportsToBeFixed is turned to a hasDynamicImportsToFix boolean to replace the one case where we used it as such ( |
||
|
||
const { walk } = await walker; | ||
|
||
walk(ast as any, { | ||
enter(node) { | ||
if (node.type === "ImportDeclaration") { | ||
if (matchesDependencies(node.source.value as string)) { | ||
toBeFixed.push(node); | ||
} | ||
} else if (node.type === "ImportExpression") { | ||
if ( | ||
node.source.type === "Literal" && | ||
matchesDependencies(node.source.value as string) | ||
) { | ||
dynamicImportsToBeFixed.push(node); | ||
} | ||
switch (node.type) { | ||
case "ImportDeclaration": | ||
case "ExportNamedDeclaration": | ||
case "ImportExpression": | ||
if ( | ||
node.source && | ||
node.source.type === "Literal" && | ||
matchesDependencies(node.source.value as string) | ||
) { | ||
toBeFixed.push(node); | ||
if (node.type === "ImportExpression") { | ||
hasDynamicImportsToFix = true; | ||
} | ||
} | ||
break; | ||
default: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This adds support for |
||
} | ||
}, | ||
}); | ||
|
||
if ( | ||
toBeFixed.length === 0 && | ||
dynamicImportsToBeFixed.length === 0 | ||
) { | ||
if (toBeFixed.length === 0) { | ||
return; | ||
} | ||
const bottomUpToBeFixed = toBeFixed.reverse(); | ||
|
||
const ms = sourcemaps ? new MagicString(code) : null; | ||
let counter = 1; | ||
let specifierCounter = 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a unique counter for |
||
let isNamespaceImport = false; | ||
|
||
for (const node of dynamicImportsToBeFixed.reverse()) { | ||
const insertion = ".then(__cjs_dyn_import__)"; | ||
if (sourcemaps) { | ||
ms!.appendRight(node.end, insertion); | ||
} else { | ||
code = | ||
code.slice(0, node.end) + | ||
insertion + | ||
code.slice(node.end); | ||
for (const node of bottomUpToBeFixed) { | ||
if (node.type === "ImportExpression") { | ||
const insertion = ".then(__cjs_dyn_import__)"; | ||
if (sourcemaps) { | ||
ms!.appendRight(node.end, insertion); | ||
} else { | ||
code = | ||
code.slice(0, node.end) + | ||
insertion + | ||
code.slice(node.end); | ||
} | ||
continue; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code was just moved. This is a slight behavior change; We no longer process dynamicImportsToBeFixed before bottomUpToBeFixed. Instead, we process all fixes in the (reversed) order that they are found in the module... Which seemed actually safer to me. But please review carefully. |
||
|
||
if (node.type === "ExportNamedDeclaration") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the code inside that if block is the new feature being added to handle re-exports. |
||
const importDestructurings = []; | ||
const exportDestructurings = []; | ||
const name = `__cjsInterop${counter++}__`; | ||
let changed = false; | ||
let defaultExportSpecifier = null; | ||
for (const specifier of node.specifiers) { | ||
if (specifier.type === "ExportSpecifier") { | ||
if (specifier.local.name === "default") { | ||
defaultExportSpecifier = specifier; | ||
continue; | ||
} | ||
changed = true; | ||
const specifierName = `__cjsInteropSpecifier${specifierCounter++}__`; | ||
importDestructurings.push( | ||
`${specifier.local.name}: ${specifierName}`, | ||
); | ||
exportDestructurings.push( | ||
`${specifierName} as ${specifier.exported.name}`, | ||
); | ||
} else { | ||
throw new Error( | ||
`Unknown ExportNamedDeclaration type specifier: ${specifier.type}`, | ||
); | ||
} | ||
} | ||
if (!changed) { | ||
continue; | ||
} | ||
if (defaultExportSpecifier) { | ||
exportDestructurings.push( | ||
`${name} as ${defaultExportSpecifier.exported.name}}`, | ||
); | ||
} | ||
preambles.push( | ||
`const { ${importDestructurings.join( | ||
", ", | ||
)} } = ${name}?.default?.__esModule ? ${name}.default : ${name};`, | ||
); | ||
const replacementNamedImports = `import ${name} from ${JSON.stringify( | ||
node.source.value, | ||
)};`; | ||
const replacementNamedExports = `export { ${exportDestructurings.join(", ")} };`; | ||
|
||
const replacement = [ | ||
replacementNamedImports, | ||
replacementNamedExports, | ||
] | ||
.filter(Boolean) | ||
.join("\n"); | ||
|
||
if (ms) { | ||
ms.overwrite(node.start, node.end, replacement); | ||
} else { | ||
code = | ||
code.slice(0, node.start) + | ||
replacement + | ||
code.slice(node.end); | ||
} | ||
continue; | ||
} | ||
} | ||
|
||
for (const node of bottomUpToBeFixed) { | ||
const destructurings: string[] = []; | ||
const name = `__cjsInterop${counter++}__`; | ||
let changed = false; | ||
|
@@ -162,7 +237,7 @@ export function cjsInterop(options: CjsInteropOptions): Plugin { | |
} | ||
} | ||
|
||
if (dynamicImportsToBeFixed.length) { | ||
if (hasDynamicImportsToFix) { | ||
const importCompat = `import { __cjs_dyn_import__ } from "${virtualModuleId}";\n`; | ||
if (sourcemaps) { | ||
ms!.prepend(importCompat); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a test case for the missing CJS interop wrapping, now supported.