@@ -4,6 +4,7 @@ import autoprefixer from 'autoprefixer'
44import history from 'connect-history-api-fallback'
55import type { AcceptedPlugin } from 'postcss'
66import postcssrc from 'postcss-load-config'
7+ import type { GetModuleInfo } from 'rollup'
78import type { AliasOptions , Connect , Plugin , UserConfig } from 'vite'
89
910/**
@@ -96,7 +97,30 @@ import '@vuepress/client/app'
9697 // also add hash to ssr entry file, so that users could build multiple sites in a single process
9798 entryFileNames : `[name].[hash].mjs` ,
9899 }
99- : { } ) ,
100+ : {
101+ manualChunks ( id , ctx ) {
102+ // move known framework code into a stable chunk so that
103+ // custom theme changes do not invalidate hash for all pages
104+ if (
105+ id . includes ( 'plugin-vue:export-helper' ) ||
106+ / @ v u e \/ ( r u n t i m e | s h a r e d | r e a c t i v i t y ) / . test ( id ) ||
107+ / @ v u e p r e s s \/ ( c l i e n t | s h a r e d ) / . test ( id )
108+ ) {
109+ return 'framework'
110+ }
111+
112+ // check if a module is statically imported by at least one entry.
113+ if (
114+ id . includes ( 'node_modules' ) &&
115+ ! / \. c s s ( $ | \\ ? ) / . test ( id ) &&
116+ isStaticallyImportedByEntry ( id , ctx . getModuleInfo )
117+ ) {
118+ return 'vendor'
119+ }
120+
121+ return undefined
122+ } ,
123+ } ) ,
100124 } ,
101125 preserveEntrySignatures : 'allow-extension' ,
102126 } ,
@@ -223,3 +247,33 @@ const resolveDefine = async ({
223247
224248 return define
225249}
250+
251+ const cache = new Map < string , boolean > ( )
252+ const isStaticallyImportedByEntry = (
253+ id : string ,
254+ getModuleInfo : GetModuleInfo ,
255+ importStack : string [ ] = [ ]
256+ ) : boolean => {
257+ if ( cache . has ( id ) ) {
258+ return ! ! cache . get ( id )
259+ }
260+ if ( importStack . includes ( id ) ) {
261+ // circular deps!
262+ cache . set ( id , false )
263+ return false
264+ }
265+ const mod = getModuleInfo ( id )
266+ if ( ! mod ) {
267+ cache . set ( id , false )
268+ return false
269+ }
270+ if ( mod . isEntry ) {
271+ cache . set ( id , true )
272+ return true
273+ }
274+ const someImporterIs = mod . importers . some ( ( item ) =>
275+ isStaticallyImportedByEntry ( item , getModuleInfo , importStack . concat ( id ) )
276+ )
277+ cache . set ( id , someImporterIs )
278+ return someImporterIs
279+ }
0 commit comments