Skip to content

Commit 8b249b8

Browse files
committed
fix: resolve relative URL generated by renderBuiltUrl passed to module preload
1 parent 5caea89 commit 8b249b8

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

packages/vite/src/node/plugins/importAnalysisBuild.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,17 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
160160
? `'modulepreload'`
161161
: `(${detectScriptRel.toString()})()`
162162

163-
// There are three different cases for the preload list format in __vitePreload
163+
// There are two different cases for the preload list format in __vitePreload
164164
//
165165
// __vitePreload(() => import(asyncChunk), [ ...deps... ])
166166
//
167167
// This is maintained to keep backwards compatibility as some users developed plugins
168168
// using regex over this list to workaround the fact that module preload wasn't
169169
// configurable.
170-
const assetsURL = renderBuiltUrl
171-
? // If `experimental.renderBuiltUrl` is used, the dependencies are already resolved.
172-
// To avoid the need for `new URL(dep, import.meta.url)`, a helper `__vitePreloadRelativeDep` is
173-
// used to resolve from relative paths which can be minimized.
174-
`function(dep, importerUrl) { return dep[0] === '.' ? new URL(dep, importerUrl).href : dep }`
175-
: optimizeModulePreloadRelativePaths
176-
? // If there isn't custom resolvers affecting the deps list, deps in the list are relative
177-
// to the current chunk and are resolved to absolute URL by the __vitePreload helper itself.
170+
const assetsURL =
171+
renderBuiltUrl || optimizeModulePreloadRelativePaths
172+
? // If `experimental.renderBuiltUrl` is used, the dependencies might be relative to the current chunk.
173+
// If relative base is used, the dependencies are relative to the current chunk.
178174
// The importerUrl is passed as third parameter to __vitePreload in this case
179175
`function(dep, importerUrl) { return new URL(dep, importerUrl).href }`
180176
: // If the base isn't relative, then the deps are relative to the projects `outDir` and the base

0 commit comments

Comments
 (0)