Skip to content

Commit

Permalink
fix(optimizer): compiled esmdoule interop
Browse files Browse the repository at this point in the history
close #1659
  • Loading branch information
yyx990803 committed Jan 23, 2021
1 parent 523198e commit 6826624
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,12 +545,16 @@ function transformCjsImport(
const cjsModuleName = makeLegalIdentifier(
`__vite__cjsImport${importIndex}_${rawUrl}`
)
const lines: string[] = [`import ${cjsModuleName} from "${url}";`]
const lines: string[] = [`import ${cjsModuleName} from "${url}"`]
importNames.forEach(({ importedName, localName }) => {
if (importedName === '*' || importedName === 'default') {
lines.push(`const ${localName} = ${cjsModuleName};`)
if (importedName === '*') {
lines.push(`const ${localName} = ${cjsModuleName}`)
} else if (importedName === 'default') {
lines.push(
`const ${localName} = ${cjsModuleName}.__esModule ? ${cjsModuleName}.default : ${cjsModuleName}`
)
} else {
lines.push(`const ${localName} = ${cjsModuleName}["${importedName}"];`)
lines.push(`const ${localName} = ${cjsModuleName}["${importedName}"]`)
}
})
return lines.join('\n')
Expand Down

0 comments on commit 6826624

Please sign in to comment.