@@ -32,6 +32,19 @@ async function fileExists(filePath: string) {
3232 }
3333}
3434
35+ async function readThemeCss ( outDir : string , theme : 'light' | 'dark' ) {
36+ const assetsDir = path . join ( outDir , 'assets' )
37+ const pattern =
38+ theme === 'light'
39+ ? / ^ s i t e \. [ a - z 0 - 9 ] + \. c s s $ /
40+ : / ^ s i t e \. [ a - z 0 - 9 ] + \. d a r k \. c s s $ /
41+ const files = ( await fs . readdir ( assetsDir ) ) . filter ( ( file ) =>
42+ pattern . test ( file ) ,
43+ )
44+ expect ( files ) . toHaveLength ( 1 )
45+ return fs . readFile ( path . join ( assetsDir , files [ 0 ] ) , 'utf8' )
46+ }
47+
3548async function readScriptBundle (
3649 outDir : string ,
3750 pageRelPath = path . join ( 'hosts' , 'index.html' ) ,
@@ -60,6 +73,42 @@ describe('incremental builder', () => {
6073 await logger ?. close ( )
6174 } )
6275
76+ it . each ( [ true , false ] ) (
77+ 'builds pages linked to cache-keyed theme files (publish %s)' ,
78+ async ( publish ) => {
79+ await withTempDir ( async ( base ) => {
80+ const contentDir = path . join ( base , 'content' )
81+ const publishDir = path . join ( base , 'publish' )
82+ await fs . mkdir ( contentDir )
83+ await fs . writeFile ( path . join ( contentDir , 'index.md' ) , '# Home' )
84+ const builder = await createIncrementalBuilder ( {
85+ siteConfig : {
86+ rootDir : base ,
87+ contentDir,
88+ publishDir,
89+ outDir : publishDir ,
90+ basePath : '/docs' ,
91+ style : { fileName : 'site.css' , href : '/assets/site.css' } ,
92+ } ,
93+ publish : { enabled : publish } ,
94+ } )
95+ await builder . buildAll ( 'publish CSS cache keys' )
96+ const manifest = await readManifest ( publishDir )
97+ expect ( manifest ?. styles . outputs ) . toHaveLength ( 2 )
98+ const html = await fs . readFile (
99+ path . join ( publishDir , 'index.html' ) ,
100+ 'utf8' ,
101+ )
102+ for ( const output of manifest ?. styles . outputs ?? [ ] ) {
103+ const fileName = path . basename ( output )
104+ expect ( fileName ) . toMatch ( / ^ s i t e \. [ a - z 0 - 9 ] + (?: \. d a r k ) ? \. c s s $ / )
105+ expect ( html ) . toContain ( `/docs/assets/${ fileName } ` )
106+ expect ( ( await fs . readFile ( output , 'utf8' ) ) . length ) . toBeGreaterThan ( 0 )
107+ }
108+ } )
109+ } ,
110+ )
111+
63112 it ( 'keeps style changes during CSS generation pending for the next request' , async ( ) => {
64113 await withTempDir ( async ( base ) => {
65114 const contentDir = path . join ( base , 'content' )
@@ -101,9 +150,9 @@ describe('incremental builder', () => {
101150 expect ( writes ) . toBe ( 1 )
102151 await builder . preparePageAssets ( )
103152 expect ( writes ) . toBe ( 2 )
104- expect (
105- await fs . readFile ( path . join ( outDir , 'assets' , 'site.css' ) , 'utf8' ) ,
106- ) . toContain ( '.concurrent-style' )
153+ expect ( await readThemeCss ( outDir , 'light' ) ) . toContain (
154+ '.concurrent-style' ,
155+ )
107156 await builder . preparePageAssets ( )
108157 expect ( writes ) . toBe ( 2 )
109158 } finally {
@@ -374,14 +423,8 @@ describe('incremental builder', () => {
374423
375424 await builder . buildAll ( 'test highlightjs styles' )
376425
377- const lightCss = await fs . readFile (
378- path . join ( outDir , 'assets' , 'site.css' ) ,
379- 'utf8' ,
380- )
381- const darkCss = await fs . readFile (
382- path . join ( outDir , 'assets' , 'site.dark.css' ) ,
383- 'utf8' ,
384- )
426+ const lightCss = await readThemeCss ( outDir , 'light' )
427+ const darkCss = await readThemeCss ( outDir , 'dark' )
385428
386429 expect ( lightCss ) . not . toContain ( 'pre.shiki.shiki-themes' )
387430 expect ( darkCss ) . not . toContain ( 'pre.shiki.shiki-themes' )
0 commit comments