@@ -741,4 +741,48 @@ describe('beasties', () => {
741741 globalThis . fetch = originalFetch
742742 }
743743 } )
744+
745+ it ( 'works with pruneSource and additionalStylesheets together' , async ( ) => {
746+ // Regression test for https://github.com/danielroe/beasties/issues/177
747+ // Ensure $$name is set on style elements for additionalStylesheets
748+ const tmpDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'beasties-test-' ) )
749+ fs . writeFileSync ( path . join ( tmpDir , 'reset.css' ) , 'h1 { margin: 0; } h2.unused { padding: 0; }' )
750+
751+ const beasties = new Beasties ( {
752+ reduceInlineStyles : false ,
753+ path : tmpDir ,
754+ pruneSource : true ,
755+ additionalStylesheets : [ '/reset.css' ] ,
756+ } )
757+
758+ beasties . writeFile = ( filename , data ) => new Promise ( ( resolve , reject ) => {
759+ try {
760+ fs . writeFileSync ( filename , data )
761+ resolve ( )
762+ }
763+ catch ( err ) {
764+ reject ( err )
765+ }
766+ } )
767+
768+ const result = await beasties . process ( trim `
769+ <html>
770+ <head>
771+ </head>
772+ <body>
773+ <h1>Hello World!</h1>
774+ </body>
775+ </html>
776+ ` )
777+
778+ // Should contain the critical CSS from reset.css
779+ expect ( result ) . toContain ( '<style>h1{margin:0}</style>' )
780+
781+ // The pruned CSS file should only contain non-critical styles
782+ const css = fs . readFileSync ( path . join ( tmpDir , 'reset.css' ) , 'utf-8' )
783+ expect ( css ) . toEqual ( 'h2.unused{padding:0}' )
784+
785+ // Clean up temporary directory
786+ fs . rmSync ( tmpDir , { recursive : true } )
787+ } )
744788} )
0 commit comments