Skip to content

Commit 91be213

Browse files
committed
fix(vite): remove css files when fully inlined or under inline threshold
1 parent ede66d6 commit 91be213

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

packages/vite-plugin-beasties/src/index.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ export function beasties(options: ViteBeastiesOptions = {}): Plugin {
6464
delete bundle[name]
6565
return true
6666
}
67-
bundle[name]!.source = sheetInverse
67+
else if (!sheetInverse.length) {
68+
delete bundle[name]
69+
return true
70+
}
71+
else {
72+
bundle[name]!.source = sheetInverse
73+
}
6874
}
6975
else {
7076
console.warn(`pruneSource is enabled, but a style (${name}) has no corresponding asset.`)
@@ -73,6 +79,26 @@ export function beasties(options: ViteBeastiesOptions = {}): Plugin {
7379
return isStyleInlined
7480
}
7581

82+
const originalCheckInline = beastiesInstance.checkInlineThreshold.bind(beastiesInstance)
83+
beastiesInstance.checkInlineThreshold = function checkInlineThreshold(style, before, sheetInverse) {
84+
const isStyleInlined = originalCheckInline(style, before, sheetInverse)
85+
86+
if (isStyleInlined || !sheetInverse.length) {
87+
// @ts-expect-error internal property
88+
const name = style.$$name.replace(/^\//, '') as string
89+
if (name in bundle && bundle[name]!.type === 'asset') {
90+
delete bundle[name]
91+
}
92+
else {
93+
console.warn(
94+
`${name} was not found in assets. the resource may still be emitted but will be unreferenced.`,
95+
)
96+
}
97+
}
98+
99+
return isStyleInlined
100+
}
101+
76102
try {
77103
return await beastiesInstance.process(html)
78104
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<!DOCTYPE html>
22
<html>
3+
34
<head>
45
<title>Test Page</title>
56
<link rel="stylesheet" href="./style.css">
67
</head>
8+
79
<body>
810
<h1>Hello Beasties</h1>
911
<div class="test-content">This is a test</div>
1012
<script type="module" src="./main.js"></script>
1113
</body>
12-
</html>
14+
15+
</html>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.test-content {
22
color: blue;
33
font-weight: bold;
4-
}
4+
}

packages/vite-plugin-beasties/test/index.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,22 @@ describe('vite-plugin-beasties', () => {
4747
expect(html).toContain('<style>')
4848
expect(html).toContain('.test-content')
4949

50+
expect(html).not.toContain('.css')
51+
5052
const hasCssColor = html?.includes('color: blue') || html?.includes('color:#00f')
5153
expect(hasCssColor).toBe(true)
5254

55+
const files = output.map(f => f.fileName.replace(/-[^.]+/, ''))
56+
expect(files).toMatchInlineSnapshot(`
57+
[
58+
"assets/index.js",
59+
"index.html",
60+
]
61+
`)
62+
5363
// prunes source
5464
const css = output.find(file => file.fileName.endsWith('.css')) as any
55-
expect(css.source.trim()).toMatchInlineSnapshot(`""`)
65+
expect(css?.source.trim()).toMatchInlineSnapshot(`undefined`)
5666
})
5767

5868
it('allows disabling pruning of source CSS files during the build', async () => {

0 commit comments

Comments
 (0)