Skip to content

Commit

Permalink
fix(import-anaysis): markPos out-of-range for overwrite (#1671)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 23, 2021
1 parent 00bc446 commit 226e984
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions packages/vite/src/node/plugins/importAnaysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
for (let index = 0; index < imports.length; index++) {
const { s: start, e: end, d: dynamicIndex } = imports[index]
if (dynamicIndex > -1) {
// if dynmamic import polyfill is used, rewrite the import to
// if dynamic import polyfill is used, rewrite the import to
// use the polyfilled function.
if (isPolyfillEnabled) {
s.overwrite(dynamicIndex, dynamicIndex + 6, `__import__`)
Expand Down Expand Up @@ -218,15 +218,17 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
}

const markPos = code.indexOf(preloadMarker, end)
s.overwrite(
markPos - 1,
markPos + preloadMarker.length + 1,
// the dep list includes the main chunk, so only need to
// preload when there are actual other deps.
deps.size > 1
? `[${[...deps].map((d) => JSON.stringify(d)).join(',')}]`
: `void 0`
)
if (markPos > 0) {
s.overwrite(
markPos - 1,
markPos + preloadMarker.length + 1,
// the dep list includes the main chunk, so only need to
// preload when there are actual other deps.
deps.size > 1
? `[${[...deps].map((d) => JSON.stringify(d)).join(',')}]`
: `void 0`
)
}
}
}
chunk.code = s.toString()
Expand Down

0 comments on commit 226e984

Please sign in to comment.