@@ -11,18 +11,29 @@ import {
11
11
import { APP_PATH } from '../alias'
12
12
import type { SiteConfig } from '../config'
13
13
import { createVitePressPlugin } from '../plugin'
14
- import { sanitizeFileName , slash } from '../shared'
14
+ import { escapeRegExp , sanitizeFileName , slash } from '../shared'
15
15
import { task } from '../utils/task'
16
16
import { buildMPAClient } from './buildMPAClient'
17
17
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 ) (?: $ | \? ) /
21
21
22
22
const clientDir = normalizePath (
23
23
path . resolve ( path . dirname ( fileURLToPath ( import . meta. url ) ) , '../client' )
24
24
)
25
25
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
+
26
37
// bundles the VitePress app for both client AND server.
27
38
export async function bundle (
28
39
config : SiteConfig ,
@@ -47,6 +58,12 @@ export async function bundle(
47
58
input [ slash ( alias ) . replace ( / \/ / g, '_' ) ] = path . resolve ( config . srcDir , file )
48
59
} )
49
60
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
+
50
67
// resolve options to pass to vite
51
68
const { rollupOptions } = options
52
69
@@ -109,12 +126,6 @@ export async function bundle(
109
126
: `${ config . assetsDir } /chunks/[name].[hash].js`
110
127
} ,
111
128
manualChunks ( id , ctx ) {
112
- if ( lazyDefaultThemeComponentsRE . test ( id ) ) {
113
- return
114
- }
115
- if ( id . startsWith ( `${ clientDir } /theme-default` ) ) {
116
- return 'theme'
117
- }
118
129
// move known framework code into a stable chunk so that
119
130
// custom theme changes do not invalidate hash for all pages
120
131
if ( id . startsWith ( '\0vite' ) ) {
@@ -135,6 +146,19 @@ export async function bundle(
135
146
) {
136
147
return 'framework'
137
148
}
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
+ }
138
162
}
139
163
} )
140
164
}
@@ -182,14 +206,15 @@ export async function bundle(
182
206
}
183
207
184
208
const cache = new Map < string , boolean > ( )
209
+ const cacheTheme = new Map < string , boolean > ( )
185
210
186
211
/**
187
212
* Check if a module is statically imported by at least one entry.
188
213
*/
189
214
function isEagerChunk ( id : string , getModuleInfo : Rollup . GetModuleInfo ) {
190
215
if (
191
216
id . includes ( 'node_modules' ) &&
192
- ! / \. c s s ( $ | \\ ? ) / . test ( id ) &&
217
+ ! CSS_LANGS_RE . test ( id ) &&
193
218
staticImportedByEntry ( id , getModuleInfo , cache )
194
219
) {
195
220
return true
@@ -200,6 +225,7 @@ function staticImportedByEntry(
200
225
id : string ,
201
226
getModuleInfo : Rollup . GetModuleInfo ,
202
227
cache : Map < string , boolean > ,
228
+ entryRE : RegExp | null = null ,
203
229
importStack : string [ ] = [ ]
204
230
) : boolean {
205
231
if ( cache . has ( id ) ) {
@@ -216,7 +242,7 @@ function staticImportedByEntry(
216
242
return false
217
243
}
218
244
219
- if ( mod . isEntry ) {
245
+ if ( entryRE ? entryRE . test ( id ) : mod . isEntry ) {
220
246
cache . set ( id , true )
221
247
return true
222
248
}
@@ -225,6 +251,7 @@ function staticImportedByEntry(
225
251
importer ,
226
252
getModuleInfo ,
227
253
cache ,
254
+ entryRE ,
228
255
importStack . concat ( id )
229
256
)
230
257
)
0 commit comments