@@ -8,6 +8,7 @@ import { init, parse as parseImports } from 'es-module-lexer'
8
8
import type { SourceMap } from 'rolldown'
9
9
import type { RawSourceMap } from '@ampproject/remapping'
10
10
import convertSourceMap from 'convert-source-map'
11
+ import { buildImportAnalysisPlugin as nativeBuildImportAnalysisPlugin } from 'rolldown/experimental'
11
12
import {
12
13
combineSourcemaps ,
13
14
generateCodeFrame ,
@@ -169,15 +170,42 @@ function preload(
169
170
/**
170
171
* Build only. During serve this is performed as part of ./importAnalysis.
171
172
*/
172
- export function buildImportAnalysisPlugin ( config : ResolvedConfig ) : Plugin {
173
+ export function buildImportAnalysisPlugin ( config : ResolvedConfig ) : [ Plugin ] {
173
174
const ssr = ! ! config . build . ssr
174
175
const isWorker = config . isWorker
176
+ const enableNativePlugin = config . experimental . enableNativePlugin
175
177
const insertPreload = ! ( ssr || ! ! config . build . lib || isWorker )
176
178
177
179
const renderBuiltUrl = config . experimental . renderBuiltUrl
178
180
const isRelativeBase = config . base === './' || config . base === ''
179
181
180
- return {
182
+ // TODO: make this environment-specific
183
+ const { modulePreload } = config . build // this.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
+ renderBuiltUrl || 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 ( config . base ) } +dep }`
206
+ const preloadCode = `const scriptRel = ${ scriptRel } ;const assetsURL = ${ assetsURL } ;const seen = {};export const ${ preloadMethod } = ${ preload . toString ( ) } `
207
+
208
+ const jsPlugin = {
181
209
name : 'vite:build-import-analysis' ,
182
210
resolveId ( id ) {
183
211
if ( id === preloadHelperId ) {
@@ -187,30 +215,6 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
187
215
188
216
load ( id ) {
189
217
if ( id === preloadHelperId ) {
190
- const { modulePreload } = this . environment . config . build
191
-
192
- const scriptRel =
193
- modulePreload && modulePreload . polyfill
194
- ? `'modulepreload'`
195
- : `/* @__PURE__ */ (${ detectScriptRel . toString ( ) } )()`
196
-
197
- // There are two different cases for the preload list format in __vitePreload
198
- //
199
- // __vitePreload(() => import(asyncChunk), [ ...deps... ])
200
- //
201
- // This is maintained to keep backwards compatibility as some users developed plugins
202
- // using regex over this list to workaround the fact that module preload wasn't
203
- // configurable.
204
- const assetsURL =
205
- renderBuiltUrl || isRelativeBase
206
- ? // If `experimental.renderBuiltUrl` is used, the dependencies might be relative to the current chunk.
207
- // If relative base is used, the dependencies are relative to the current chunk.
208
- // The importerUrl is passed as third parameter to __vitePreload in this case
209
- `function(dep, importerUrl) { return new URL(dep, importerUrl).href }`
210
- : // If the base isn't relative, then the deps are relative to the projects `outDir` and the base
211
- // is appended inside __vitePreload too.
212
- `function(dep) { return ${ JSON . stringify ( config . base ) } +dep }`
213
- const preloadCode = `const scriptRel = ${ scriptRel } ;const assetsURL = ${ assetsURL } ;const seen = {};export const ${ preloadMethod } = ${ preload . toString ( ) } `
214
218
return { code : preloadCode , moduleSideEffects : false }
215
219
}
216
220
} ,
@@ -715,5 +719,23 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
715
719
}
716
720
}
717
721
} ,
722
+ } as Plugin
723
+ if ( enableNativePlugin ) {
724
+ delete jsPlugin . transform
725
+ delete jsPlugin . resolveId
726
+ delete jsPlugin . load
718
727
}
728
+ return [
729
+ jsPlugin ,
730
+ enableNativePlugin
731
+ ? nativeBuildImportAnalysisPlugin ( {
732
+ preloadCode : preloadCode ,
733
+ insertPreload : insertPreload ,
734
+ /// this field looks redundant, put a dummy value for now
735
+ optimizeModulePreloadRelativePaths : false ,
736
+ renderBuiltUrl : Boolean ( renderBuiltUrl ) ,
737
+ isRelativeBase : isRelativeBase ,
738
+ } )
739
+ : null ,
740
+ ] . filter ( Boolean ) as [ Plugin ]
719
741
}
0 commit comments