@@ -13,7 +13,7 @@ import type { Plugin } from '../plugin'
13
13
import type { ResolvedConfig } from '../config'
14
14
import { toOutputFilePathInJS } from '../build'
15
15
import { genSourceMapUrl } from '../server/sourcemap'
16
- import type { Environment } from '../environment '
16
+ import type { PartialEnvironment } from '../baseEnvironment '
17
17
import { removedPureCssFilesCache } from './css'
18
18
import { createParseErrorInfo } from './importAnalysis'
19
19
@@ -175,11 +175,43 @@ function preload(
175
175
} )
176
176
}
177
177
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
+
178
210
/**
179
211
* Build only. During serve this is performed as part of ./importAnalysis.
180
212
*/
181
213
export function buildImportAnalysisPlugin ( config : ResolvedConfig ) : Plugin {
182
- const getInsertPreload = ( environment : Environment ) =>
214
+ const getInsertPreload = ( environment : PartialEnvironment ) =>
183
215
environment . config . consumer === 'client' &&
184
216
! config . isWorker &&
185
217
! config . build . lib
@@ -200,30 +232,11 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
200
232
load : {
201
233
handler ( id ) {
202
234
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
+ )
227
240
return { code : preloadCode , moduleSideEffects : false }
228
241
}
229
242
} ,
0 commit comments