@@ -145,11 +145,15 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
145
145
d : dynamicIndex
146
146
} = imports [ index ]
147
147
148
- if ( dynamicIndex > - 1 && insertPreload ) {
148
+ const isDynamic = dynamicIndex > - 1
149
+
150
+ if ( isDynamic && insertPreload ) {
149
151
needPreloadHelper = true
150
- const original = source . slice ( expStart , expEnd )
151
- const replacement = `${ preloadMethod } (() => ${ original } ,${ isModernFlag } ?"${ preloadMarker } ":void 0)`
152
- str ( ) . overwrite ( expStart , expEnd , replacement , { contentOnly : true } )
152
+ str ( ) . prependLeft ( expStart , `${ preloadMethod } (() => ` )
153
+ str ( ) . appendRight (
154
+ expEnd ,
155
+ `,${ isModernFlag } ?"${ preloadMarker } ":void 0)`
156
+ )
153
157
}
154
158
155
159
// Differentiate CSS imports that use the default export from those that
@@ -159,14 +163,16 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
159
163
if (
160
164
specifier &&
161
165
isCSSRequest ( specifier ) &&
162
- source . slice ( expStart , start ) . includes ( 'from' ) &&
166
+ // always inject ?used query when it is a dynamic import
167
+ // because there is no way to check whether the default export is used
168
+ ( source . slice ( expStart , start ) . includes ( 'from' ) || isDynamic ) &&
163
169
// already has ?used query (by import.meta.glob)
164
170
! specifier . match ( / \? u s e d ( & | $ ) / ) &&
165
171
// edge case for package names ending with .css (e.g normalize.css)
166
172
! ( bareImportRE . test ( specifier ) && ! specifier . includes ( '/' ) )
167
173
) {
168
174
const url = specifier . replace ( / \? | $ / , ( m ) => `?used${ m ? '&' : '' } ` )
169
- str ( ) . overwrite ( start , end , dynamicIndex > - 1 ? `'${ url } '` : url , {
175
+ str ( ) . overwrite ( start , end , isDynamic ? `'${ url } '` : url , {
170
176
contentOnly : true
171
177
} )
172
178
}
0 commit comments