@@ -318,7 +318,64 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
318318 } ,
319319
320320 generateBundle ( { format } , bundle ) {
321- if ( format !== 'es' || ssr || isWorker ) {
321+ if ( format !== 'es' ) {
322+ return
323+ }
324+
325+ if ( ssr || isWorker ) {
326+ const removedPureCssFiles = removedPureCssFilesCache . get ( config )
327+ if ( removedPureCssFiles && removedPureCssFiles . size > 0 ) {
328+ for ( const file in bundle ) {
329+ const chunk = bundle [ file ]
330+ if ( chunk . type === 'chunk' && chunk . code . includes ( 'import' ) ) {
331+ const code = chunk . code
332+ let imports ! : ImportSpecifier [ ]
333+ try {
334+ imports = parseImports ( code ) [ 0 ] . filter ( ( i ) => i . d > - 1 )
335+ } catch ( e : any ) {
336+ const loc = numberToPos ( code , e . idx )
337+ this . error ( {
338+ name : e . name ,
339+ message : e . message ,
340+ stack : e . stack ,
341+ cause : e . cause ,
342+ pos : e . idx ,
343+ loc : { ...loc , file : chunk . fileName } ,
344+ frame : generateCodeFrame ( code , loc ) ,
345+ } )
346+ }
347+
348+ for ( const imp of imports ) {
349+ const {
350+ n : name ,
351+ s : start ,
352+ e : end ,
353+ ss : expStart ,
354+ se : expEnd ,
355+ } = imp
356+ let url = name
357+ if ( ! url ) {
358+ const rawUrl = code . slice ( start , end )
359+ if ( rawUrl [ 0 ] === `"` && rawUrl [ rawUrl . length - 1 ] === `"` )
360+ url = rawUrl . slice ( 1 , - 1 )
361+ }
362+ if ( ! url ) continue
363+
364+ const normalizedFile = path . posix . join (
365+ path . posix . dirname ( chunk . fileName ) ,
366+ url ,
367+ )
368+ if ( removedPureCssFiles . has ( normalizedFile ) ) {
369+ // remove with Promise.resolve({}) while preserving source map location
370+ chunk . code =
371+ chunk . code . slice ( 0 , expStart ) +
372+ `Promise.resolve({${ '' . padEnd ( expEnd - expStart - 19 , ' ' ) } })` +
373+ chunk . code . slice ( expEnd )
374+ }
375+ }
376+ }
377+ }
378+ }
322379 return
323380 }
324381
0 commit comments