@@ -502,6 +502,74 @@ describe('build', () => {
502502 } )
503503 } )
504504
505+ for ( const suffix of [ '?dpl=test' , '#fragment' ] ) {
506+ describe ( `dynamic imports with ${ suffix } ` , ( ) => {
507+ async function buildProject ( library = false ) {
508+ const root = resolve ( dirname , 'fixtures/dynamic-import-query-fragment' )
509+ const entry = resolve ( root , library ? 'css-entry.js' : 'entry.js' )
510+ const result = await build ( {
511+ root,
512+ configFile : false ,
513+ logLevel : 'silent' ,
514+ input : entry ,
515+ build : {
516+ write : false ,
517+ minify : false ,
518+ cssCodeSplit : true ,
519+ lib : library ? { entry, formats : [ 'es' ] } : false ,
520+ } ,
521+ plugins : [
522+ {
523+ name : 'append-query-or-fragment' ,
524+ // Append a query string or fragment before Vite looks up output chunks.
525+ // experimental.renderBuiltUrl rewrites preload URLs after that lookup
526+ // and leaves the import specifiers unchanged.
527+ renderChunk ( code ) {
528+ return code . replace (
529+ / ( i m p o r t \( [ " ' ` ] ) ( [ ^ " ' ` ] + \. m ? j s ) ( [ " ' ` ] \) ) / g,
530+ `$1$2${ suffix } $3` ,
531+ )
532+ } ,
533+ } ,
534+ ] ,
535+ } )
536+ if ( 'on' in result ) throw new Error ( 'Unexpected watcher result' )
537+ return Array . isArray ( result ) ? result [ 0 ] : result
538+ }
539+
540+ test ( 'preserves CSS dependencies of lazy JavaScript chunks' , async ( ) => {
541+ const { output } = await buildProject ( )
542+ const entry = output . find (
543+ ( chunk ) : chunk is OutputChunk =>
544+ chunk . type === 'chunk' && chunk . isEntry ,
545+ )
546+ assert ( entry )
547+ const css = output . find (
548+ ( asset ) => asset . type === 'asset' && asset . fileName . endsWith ( '.css' ) ,
549+ )
550+ assert ( css )
551+ expect ( entry . code ) . toContain ( suffix )
552+ expect ( entry . code ) . toContain ( css . fileName )
553+ } )
554+
555+ test ( 'removes imports of deleted CSS-only chunks in library builds' , async ( ) => {
556+ const { output } = await buildProject ( true )
557+ const entry = output . find (
558+ ( chunk ) : chunk is OutputChunk =>
559+ chunk . type === 'chunk' && chunk . isEntry ,
560+ )
561+ assert ( entry )
562+ expect (
563+ output . some (
564+ ( asset ) =>
565+ asset . type === 'asset' && asset . fileName . endsWith ( '.css' ) ,
566+ ) ,
567+ ) . toBe ( true )
568+ expect ( entry . code ) . not . toMatch ( / \b i m p o r t \s * \( / )
569+ } )
570+ } )
571+ }
572+
505573 test . for ( [
506574 [ true , true ] ,
507575 [ true , false ] ,
0 commit comments