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: adjust esm syntax judgment logic #16436

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Changes from all commits
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
14 changes: 7 additions & 7 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const jsExtensionRE = /\.js$/i
const jsMapExtensionRE = /\.js\.map$/i

export type ExportsData = {
hasImports: boolean
hasModuleSyntax: boolean
// exported names (for `export { a as b }`, `b` is exported name)
exports: readonly string[]
// hint if the dep requires loading as jsx
Expand Down Expand Up @@ -1079,9 +1079,9 @@ export async function extractExportsData(
write: false,
format: 'esm',
})
const [imports, exports] = parse(result.outputFiles[0].text)
const [, exports, , hasModuleSyntax] = parse(result.outputFiles[0].text)
return {
hasImports: imports.length > 0,
hasModuleSyntax,
exports: exports.map((e) => e.n),
}
}
Expand All @@ -1104,9 +1104,9 @@ export async function extractExportsData(
usedJsxLoader = true
}

const [imports, exports] = parseResult
const [, exports, , hasModuleSyntax] = parseResult
const exportsData: ExportsData = {
hasImports: imports.length > 0,
hasModuleSyntax,
exports: exports.map((e) => e.n),
jsxLoader: usedJsxLoader,
}
Expand All @@ -1123,9 +1123,9 @@ function needsInterop(
if (getDepOptimizationConfig(config, ssr)?.needsInterop?.includes(id)) {
return true
}
const { hasImports, exports } = exportsData
const { hasModuleSyntax, exports } = exportsData
// entry has no ESM syntax - likely CJS or UMD
if (!exports.length && !hasImports) {
if (!hasModuleSyntax) {
return true
}

Expand Down