Skip to content

Commit

Permalink
fix: __VITE_PRELOAD__ replacement error(vitejs#3051)
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Jul 6, 2021
1 parent c115e19 commit 115e738
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Expand Up @@ -26,7 +26,7 @@ const preloadMarkerRE = new RegExp(`"${preloadMarker}"`, 'g')
*/
function preload(baseModule: () => Promise<{}>, deps?: string[]) {
// @ts-ignore
if (!__VITE_IS_MODERN__ || !deps) {
if (!__VITE_IS_MODERN__ || !deps || deps.length === 0) {
return baseModule()
}

Expand Down Expand Up @@ -262,7 +262,12 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
addDeps(normalizedFile)
}

const markPos = code.indexOf(preloadMarker, end)
let markPos = code.indexOf(preloadMarker, end)
// fix issue #3051
if (markPos == -1 && imports.length == 1) {
markPos = code.indexOf(preloadMarker)
}

if (markPos > 0) {
s.overwrite(
markPos - 1,
Expand All @@ -271,7 +276,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
// preload when there are actual other deps.
deps.size > 1
? `[${[...deps].map((d) => JSON.stringify(d)).join(',')}]`
: `void 0`
: `[]`
)
}
}
Expand Down

0 comments on commit 115e738

Please sign in to comment.