Skip to content

Commit

Permalink
fix: use safe dynamic import rewrite
Browse files Browse the repository at this point in the history
fix #1563
  • Loading branch information
yyx990803 committed Jan 19, 2021
1 parent c6d5ed8 commit 5cb02ce
Showing 1 changed file with 1 addition and 14 deletions.
15 changes: 1 addition & 14 deletions packages/vite/src/node/plugins/importAnaysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
needPreloadHelper = true
const dynamicEnd = source.indexOf(`)`, end) + 1
const original = source.slice(dynamicIndex, dynamicEnd)
let replacement =
`(${isModernFlag} ` +
`? ${preloadMethod}(() => ${original},"${preloadMarker}")` +
` : ${original})`
// backtrace to see if the import call is after a newline
for (let i = dynamicIndex - 1; i > 0; i--) {
const char = source.charAt(i)
if (char === '\n') {
replacement = `;` + replacement
break
} else if (!/\s/.test(char)) {
break
}
}
const replacement = `${preloadMethod}(() => ${original},${isModernFlag}?"${preloadMarker}":void 0)`
str().overwrite(dynamicIndex, dynamicEnd, replacement)
}
}
Expand Down

0 comments on commit 5cb02ce

Please sign in to comment.