@@ -152,6 +152,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
152152 d : dynamicIndex
153153 } = imports [ index ]
154154
155+ const isDynamic = dynamicIndex > - 1
156+
155157 // import.meta.glob
156158 if (
157159 source . slice ( start , end ) === 'import.meta' &&
@@ -189,11 +191,13 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
189191 continue
190192 }
191193
192- if ( dynamicIndex > - 1 && insertPreload ) {
194+ if ( isDynamic && insertPreload ) {
193195 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+ )
197201 }
198202
199203 // Differentiate CSS imports that use the default export from those that
@@ -203,12 +207,14 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
203207 if (
204208 specifier &&
205209 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 ) &&
207213 // edge case for package names ending with .css (e.g normalize.css)
208214 ! ( bareImportRE . test ( specifier ) && ! specifier . includes ( '/' ) )
209215 ) {
210216 const url = specifier . replace ( / \? | $ / , ( m ) => `?used${ m ? '&' : '' } ` )
211- str ( ) . overwrite ( start , end , dynamicIndex > - 1 ? `'${ url } '` : url , {
217+ str ( ) . overwrite ( start , end , isDynamic ? `'${ url } '` : url , {
212218 contentOnly : true
213219 } )
214220 }
0 commit comments