Skip to content

Commit 5fd0af3

Browse files
committed
fix(beasties): set $$name on style elements for additionalStylesheets
resolves #177
1 parent 32d9630 commit 5fd0af3

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

packages/beasties/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ export default class Beasties {
276276
styleSheetsIncluded.push(cssFile)
277277
const style = document.createElement('style')
278278
style.$$external = true
279+
style.$$name = cssFile
279280
return this.getCssAsset(cssFile, style).then(sheet => [sheet, style] as const)
280281
}),
281282
)

packages/beasties/test/beasties.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)