@@ -15,7 +15,10 @@ import {
15
15
isInNodeModules ,
16
16
numberToPos ,
17
17
} from '../utils'
18
- import type { Plugin } from '../plugin'
18
+ import {
19
+ type Plugin ,
20
+ createBuiltinPluginWithEnvironmentSupport ,
21
+ } from '../plugin'
19
22
import type { ResolvedConfig } from '../config'
20
23
import { toOutputFilePathInJS } from '../build'
21
24
import { genSourceMapUrl } from '../server/sourcemap'
@@ -168,21 +171,12 @@ function preload(
168
171
} )
169
172
}
170
173
171
- /**
172
- * Build only. During serve this is performed as part of ./importAnalysis.
173
- */
174
- export function buildImportAnalysisPlugin ( config : ResolvedConfig ) : [ Plugin ] {
175
- const getInsertPreload = ( environment : Environment ) =>
176
- environment . config . consumer === 'client' &&
177
- ! config . isWorker &&
178
- ! config . build . lib
179
-
180
- const enableNativePlugin = config . experimental . enableNativePlugin
181
- const renderBuiltUrl = config . experimental . renderBuiltUrl
182
- const isRelativeBase = config . base === './' || config . base === ''
183
-
184
- // TODO: make this environment-specific
185
- const { modulePreload } = config . build // this.environment.config.build
174
+ function getPreloadCode (
175
+ environment : Environment ,
176
+ renderBuiltUrlBoolean : boolean ,
177
+ isRelativeBase : boolean ,
178
+ ) {
179
+ const { modulePreload } = environment . config . build
186
180
187
181
const scriptRel =
188
182
modulePreload && modulePreload . polyfill
@@ -197,15 +191,30 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): [Plugin] {
197
191
// using regex over this list to workaround the fact that module preload wasn't
198
192
// configurable.
199
193
const assetsURL =
200
- renderBuiltUrl || isRelativeBase
194
+ renderBuiltUrlBoolean || isRelativeBase
201
195
? // If `experimental.renderBuiltUrl` is used, the dependencies might be relative to the current chunk.
202
196
// If relative base is used, the dependencies are relative to the current chunk.
203
197
// The importerUrl is passed as third parameter to __vitePreload in this case
204
198
`function(dep, importerUrl) { return new URL(dep, importerUrl).href }`
205
199
: // If the base isn't relative, then the deps are relative to the projects `outDir` and the base
206
200
// is appended inside __vitePreload too.
207
- `function(dep) { return ${ JSON . stringify ( config . base ) } +dep }`
201
+ `function(dep) { return ${ JSON . stringify ( environment . config . base ) } +dep }`
208
202
const preloadCode = `const scriptRel = ${ scriptRel } ;const assetsURL = ${ assetsURL } ;const seen = {};export const ${ preloadMethod } = ${ preload . toString ( ) } `
203
+ return preloadCode
204
+ }
205
+
206
+ /**
207
+ * Build only. During serve this is performed as part of ./importAnalysis.
208
+ */
209
+ export function buildImportAnalysisPlugin ( config : ResolvedConfig ) : [ Plugin ] {
210
+ const getInsertPreload = ( environment : Environment ) =>
211
+ environment . config . consumer === 'client' &&
212
+ ! config . isWorker &&
213
+ ! config . build . lib
214
+
215
+ const enableNativePlugin = config . experimental . enableNativePlugin
216
+ const renderBuiltUrl = config . experimental . renderBuiltUrl
217
+ const isRelativeBase = config . base === './' || config . base === ''
209
218
210
219
const jsPlugin = {
211
220
name : 'vite:build-import-analysis' ,
@@ -217,6 +226,11 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): [Plugin] {
217
226
218
227
load ( id ) {
219
228
if ( id === preloadHelperId ) {
229
+ const preloadCode = getPreloadCode (
230
+ this . environment ,
231
+ ! ! renderBuiltUrl ,
232
+ isRelativeBase ,
233
+ )
220
234
return { code : preloadCode , moduleSideEffects : false }
221
235
}
222
236
} ,
@@ -731,15 +745,24 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): [Plugin] {
731
745
return [
732
746
jsPlugin ,
733
747
enableNativePlugin
734
- ? nativeBuildImportAnalysisPlugin ( {
735
- preloadCode : preloadCode ,
736
- // @ts -expect-error make this environment-specific
737
- insertPreload : getInsertPreload ( { config : { consumer : 'client' } } ) ,
738
- /// this field looks redundant, put a dummy value for now
739
- optimizeModulePreloadRelativePaths : false ,
740
- renderBuiltUrl : Boolean ( renderBuiltUrl ) ,
741
- isRelativeBase : isRelativeBase ,
742
- } )
748
+ ? createBuiltinPluginWithEnvironmentSupport (
749
+ 'native:import-analysis-build' ,
750
+ ( environment ) => {
751
+ const preloadCode = getPreloadCode (
752
+ environment ,
753
+ ! ! renderBuiltUrl ,
754
+ isRelativeBase ,
755
+ )
756
+ return nativeBuildImportAnalysisPlugin ( {
757
+ preloadCode,
758
+ insertPreload : getInsertPreload ( environment ) ,
759
+ // this field looks redundant, put a dummy value for now
760
+ optimizeModulePreloadRelativePaths : false ,
761
+ renderBuiltUrl : ! ! renderBuiltUrl ,
762
+ isRelativeBase,
763
+ } )
764
+ } ,
765
+ )
743
766
: null ,
744
767
] . filter ( Boolean ) as [ Plugin ]
745
768
}
0 commit comments