From 226e9849253677770f11f8a26e6ec205067dc902 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sun, 24 Jan 2021 05:00:26 +0800 Subject: [PATCH] fix(import-anaysis): markPos out-of-range for overwrite (#1671) --- .../src/node/plugins/importAnaysisBuild.ts | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/vite/src/node/plugins/importAnaysisBuild.ts b/packages/vite/src/node/plugins/importAnaysisBuild.ts index 52fb5841ca2a35..06dc29a3b8b6e4 100644 --- a/packages/vite/src/node/plugins/importAnaysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnaysisBuild.ts @@ -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__`) @@ -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()