File tree Expand file tree Collapse file tree 4 files changed +43
-4
lines changed
packages/vite-plugin-beasties Expand file tree Collapse file tree 4 files changed +43
-4
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 11.test-content {
22 color : blue;
33 font-weight : bold;
4- }
4+ }
Original file line number Diff line number Diff 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 ( ) => {
You can’t perform that action at this time.
0 commit comments