@@ -152,6 +152,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
152
152
d : dynamicIndex
153
153
} = imports [ index ]
154
154
155
+ const isDynamic = dynamicIndex > - 1
156
+
155
157
// import.meta.glob
156
158
if (
157
159
source . slice ( start , end ) === 'import.meta' &&
@@ -189,11 +191,13 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
189
191
continue
190
192
}
191
193
192
- if ( dynamicIndex > - 1 && insertPreload ) {
194
+ if ( isDynamic && insertPreload ) {
193
195
needPreloadHelper = true
194
- const original = source . slice ( expStart , expEnd )
195
- const replacement = `${ preloadMethod } (() => ${ original } ,${ isModernFlag } ?"${ preloadMarker } ":void 0)`
196
- str ( ) . overwrite ( expStart , expEnd , replacement , { contentOnly : true } )
196
+ str ( ) . prependLeft ( expStart , `${ preloadMethod } (() => ` )
197
+ str ( ) . appendRight (
198
+ expEnd ,
199
+ `,${ isModernFlag } ?"${ preloadMarker } ":void 0)`
200
+ )
197
201
}
198
202
199
203
// Differentiate CSS imports that use the default export from those that
@@ -203,12 +207,14 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
203
207
if (
204
208
specifier &&
205
209
isCSSRequest ( specifier ) &&
206
- source . slice ( expStart , start ) . includes ( 'from' ) &&
210
+ // always inject ?used query when it is a dynamic import
211
+ // because there is no way to check whether the default export is used
212
+ ( source . slice ( expStart , start ) . includes ( 'from' ) || isDynamic ) &&
207
213
// edge case for package names ending with .css (e.g normalize.css)
208
214
! ( bareImportRE . test ( specifier ) && ! specifier . includes ( '/' ) )
209
215
) {
210
216
const url = specifier . replace ( / \? | $ / , ( m ) => `?used${ m ? '&' : '' } ` )
211
- str ( ) . overwrite ( start , end , dynamicIndex > - 1 ? `'${ url } '` : url , {
217
+ str ( ) . overwrite ( start , end , isDynamic ? `'${ url } '` : url , {
212
218
contentOnly : true
213
219
} )
214
220
}
0 commit comments