@@ -13,7 +13,7 @@ import type { Plugin } from '../plugin'
1313import type { ResolvedConfig } from '../config'
1414import { toOutputFilePathInJS } from '../build'
1515import { genSourceMapUrl } from '../server/sourcemap'
16- import type { Environment } from '../environment '
16+ import type { PartialEnvironment } from '../baseEnvironment '
1717import { removedPureCssFilesCache } from './css'
1818import { createParseErrorInfo } from './importAnalysis'
1919
@@ -175,11 +175,43 @@ function preload(
175175 } )
176176}
177177
178+ function getPreloadCode (
179+ environment : PartialEnvironment ,
180+ renderBuiltUrlBoolean : boolean ,
181+ isRelativeBase : boolean ,
182+ ) {
183+ const { modulePreload } = environment . config . build
184+
185+ const scriptRel =
186+ modulePreload && modulePreload . polyfill
187+ ? `'modulepreload'`
188+ : `/* @__PURE__ */ (${ detectScriptRel . toString ( ) } )()`
189+
190+ // There are two different cases for the preload list format in __vitePreload
191+ //
192+ // __vitePreload(() => import(asyncChunk), [ ...deps... ])
193+ //
194+ // This is maintained to keep backwards compatibility as some users developed plugins
195+ // using regex over this list to workaround the fact that module preload wasn't
196+ // configurable.
197+ const assetsURL =
198+ renderBuiltUrlBoolean || isRelativeBase
199+ ? // If `experimental.renderBuiltUrl` is used, the dependencies might be relative to the current chunk.
200+ // If relative base is used, the dependencies are relative to the current chunk.
201+ // The importerUrl is passed as third parameter to __vitePreload in this case
202+ `function(dep, importerUrl) { return new URL(dep, importerUrl).href }`
203+ : // If the base isn't relative, then the deps are relative to the projects `outDir` and the base
204+ // is appended inside __vitePreload too.
205+ `function(dep) { return ${ JSON . stringify ( environment . config . base ) } +dep }`
206+ const preloadCode = `const scriptRel = ${ scriptRel } ;const assetsURL = ${ assetsURL } ;const seen = {};export const ${ preloadMethod } = ${ preload . toString ( ) } `
207+ return preloadCode
208+ }
209+
178210/**
179211 * Build only. During serve this is performed as part of ./importAnalysis.
180212 */
181213export function buildImportAnalysisPlugin ( config : ResolvedConfig ) : Plugin {
182- const getInsertPreload = ( environment : Environment ) =>
214+ const getInsertPreload = ( environment : PartialEnvironment ) =>
183215 environment . config . consumer === 'client' &&
184216 ! config . isWorker &&
185217 ! config . build . lib
@@ -200,30 +232,11 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
200232 load : {
201233 handler ( id ) {
202234 if ( id === preloadHelperId ) {
203- const { modulePreload } = this . environment . config . build
204-
205- const scriptRel =
206- modulePreload && modulePreload . polyfill
207- ? `'modulepreload'`
208- : `/* @__PURE__ */ (${ detectScriptRel . toString ( ) } )()`
209-
210- // There are two different cases for the preload list format in __vitePreload
211- //
212- // __vitePreload(() => import(asyncChunk), [ ...deps... ])
213- //
214- // This is maintained to keep backwards compatibility as some users developed plugins
215- // using regex over this list to workaround the fact that module preload wasn't
216- // configurable.
217- const assetsURL =
218- renderBuiltUrl || isRelativeBase
219- ? // If `experimental.renderBuiltUrl` is used, the dependencies might be relative to the current chunk.
220- // If relative base is used, the dependencies are relative to the current chunk.
221- // The importerUrl is passed as third parameter to __vitePreload in this case
222- `function(dep, importerUrl) { return new URL(dep, importerUrl).href }`
223- : // If the base isn't relative, then the deps are relative to the projects `outDir` and the base
224- // is appended inside __vitePreload too.
225- `function(dep) { return ${ JSON . stringify ( config . base ) } +dep }`
226- const preloadCode = `const scriptRel = ${ scriptRel } ;const assetsURL = ${ assetsURL } ;const seen = {};export const ${ preloadMethod } = ${ preload . toString ( ) } `
235+ const preloadCode = getPreloadCode (
236+ this . environment ,
237+ ! ! renderBuiltUrl ,
238+ isRelativeBase ,
239+ )
227240 return { code : preloadCode , moduleSideEffects : false }
228241 }
229242 } ,
0 commit comments