@@ -11,18 +11,29 @@ import {
1111import { APP_PATH } from '../alias'
1212import type { SiteConfig } from '../config'
1313import { createVitePressPlugin } from '../plugin'
14- import { sanitizeFileName , slash } from '../shared'
14+ import { escapeRegExp , sanitizeFileName , slash } from '../shared'
1515import { task } from '../utils/task'
1616import { buildMPAClient } from './buildMPAClient'
1717
18- // A list of default theme components that should only be loaded on demand.
19- const lazyDefaultThemeComponentsRE =
20- / V P ( H o m e S p o n s o r s | D o c A s i d e S p o n s o r s | T e a m P a g e | T e a m M e m b e r s | L o c a l S e a r c h B o x | A l g o l i a S e a r c h B o x | C a r b o n A d s | D o c A s i d e C a r b o n A d s | S p o n s o r s ) /
18+ // https://github.com/vitejs/vite/blob/d2aa0969ee316000d3b957d7e879f001e85e369e/packages/vite/src/node/plugins/splitVendorChunk.ts#L14
19+ const CSS_LANGS_RE =
20+ / \. ( c s s | l e s s | s a s s | s c s s | s t y l | s t y l u s | p c s s | p o s t c s s | s s s ) (?: $ | \? ) /
2121
2222const clientDir = normalizePath (
2323 path . resolve ( path . dirname ( fileURLToPath ( import . meta. url ) ) , '../client' )
2424)
2525
26+ // these deps are also being used in the client code (outside of the theme)
27+ // exclude them from the theme chunk so there is no circular dependency
28+ const excludedModules = [
29+ '/@siteData' ,
30+ 'node_modules/@vueuse/core/' ,
31+ 'node_modules/@vueuse/shared/' ,
32+ 'node_modules/vue/' ,
33+ 'node_modules/vue-demi/' ,
34+ clientDir
35+ ]
36+
2637// bundles the VitePress app for both client AND server.
2738export async function bundle (
2839 config : SiteConfig ,
@@ -47,6 +58,12 @@ export async function bundle(
4758 input [ slash ( alias ) . replace ( / \/ / g, '_' ) ] = path . resolve ( config . srcDir , file )
4859 } )
4960
61+ const themeEntryRE = new RegExp (
62+ `^${ escapeRegExp (
63+ path . resolve ( config . themeDir , 'index.js' ) . replace ( / \\ / g, '/' )
64+ ) . slice ( 0 , - 2 ) } m?(j|t)s`
65+ )
66+
5067 // resolve options to pass to vite
5168 const { rollupOptions } = options
5269
@@ -109,12 +126,6 @@ export async function bundle(
109126 : `${ config . assetsDir } /chunks/[name].[hash].js`
110127 } ,
111128 manualChunks ( id , ctx ) {
112- if ( lazyDefaultThemeComponentsRE . test ( id ) ) {
113- return
114- }
115- if ( id . startsWith ( `${ clientDir } /theme-default` ) ) {
116- return 'theme'
117- }
118129 // move known framework code into a stable chunk so that
119130 // custom theme changes do not invalidate hash for all pages
120131 if ( id . startsWith ( '\0vite' ) ) {
@@ -135,6 +146,19 @@ export async function bundle(
135146 ) {
136147 return 'framework'
137148 }
149+
150+ if (
151+ ( id . startsWith ( `${ clientDir } /theme-default` ) ||
152+ ! excludedModules . some ( ( i ) => id . includes ( i ) ) ) &&
153+ staticImportedByEntry (
154+ id ,
155+ ctx . getModuleInfo ,
156+ cacheTheme ,
157+ themeEntryRE
158+ )
159+ ) {
160+ return 'theme'
161+ }
138162 }
139163 } )
140164 }
@@ -182,14 +206,15 @@ export async function bundle(
182206}
183207
184208const cache = new Map < string , boolean > ( )
209+ const cacheTheme = new Map < string , boolean > ( )
185210
186211/**
187212 * Check if a module is statically imported by at least one entry.
188213 */
189214function isEagerChunk ( id : string , getModuleInfo : Rollup . GetModuleInfo ) {
190215 if (
191216 id . includes ( 'node_modules' ) &&
192- ! / \. c s s ( $ | \\ ? ) / . test ( id ) &&
217+ ! CSS_LANGS_RE . test ( id ) &&
193218 staticImportedByEntry ( id , getModuleInfo , cache )
194219 ) {
195220 return true
@@ -200,6 +225,7 @@ function staticImportedByEntry(
200225 id : string ,
201226 getModuleInfo : Rollup . GetModuleInfo ,
202227 cache : Map < string , boolean > ,
228+ entryRE : RegExp | null = null ,
203229 importStack : string [ ] = [ ]
204230) : boolean {
205231 if ( cache . has ( id ) ) {
@@ -216,7 +242,7 @@ function staticImportedByEntry(
216242 return false
217243 }
218244
219- if ( mod . isEntry ) {
245+ if ( entryRE ? entryRE . test ( id ) : mod . isEntry ) {
220246 cache . set ( id , true )
221247 return true
222248 }
@@ -225,6 +251,7 @@ function staticImportedByEntry(
225251 importer ,
226252 getModuleInfo ,
227253 cache ,
254+ entryRE ,
228255 importStack . concat ( id )
229256 )
230257 )
0 commit comments