Skip to content
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: handle namespace import and dynamic import interop consistently #15619

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,18 @@ export function createParseErrorInfo(
}
}

const interopHelper = (module: any) => {
if (Array.isArray(module) || typeof module === 'string') {
return {
default: module,
}
}
if (module && module.__esModule) {
return module
}
return { ...module, default: module }
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}

export function interopNamedImports(
str: MagicString,
importSpecifier: ImportSpecifier,
Expand All @@ -870,7 +882,7 @@ export function interopNamedImports(
str.overwrite(
expStart,
expEnd,
`import('${rewrittenUrl}').then(m => m.default && m.default.__esModule ? m.default : ({ ...m.default, default: m.default }))` +
`import('${rewrittenUrl}').then(m => (${interopHelper.toString()})(m.default))` +
bluwy marked this conversation as resolved.
Show resolved Hide resolved
getLineBreaks(exp),
{ contentOnly: true },
)
Expand Down Expand Up @@ -1006,7 +1018,9 @@ export function transformCjsImport(
const lines: string[] = [`import ${cjsModuleName} from "${url}"`]
importNames.forEach(({ importedName, localName }) => {
if (importedName === '*') {
lines.push(`const ${localName} = ${cjsModuleName}`)
lines.push(
`const ${localName} = (${interopHelper.toString()})(${cjsModuleName})`,
)
} else if (importedName === 'default') {
lines.push(
`const ${localName} = ${cjsModuleName}.__esModule ? ${cjsModuleName}.default : ${cjsModuleName}`,
Expand Down