Skip to content

Commit aaa8b1e

Browse files
authored
Merge 585e594 into 8c345d8
2 parents 8c345d8 + 585e594 commit aaa8b1e

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

packages/bundler-vite/src/plugins/mainPlugin.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import autoprefixer from 'autoprefixer'
44
import history from 'connect-history-api-fallback'
55
import type { AcceptedPlugin } from 'postcss'
66
import postcssrc from 'postcss-load-config'
7+
import type { GetModuleInfo } from 'rollup'
78
import 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+
/@vue\/(runtime|shared|reactivity)/.test(id) ||
107+
/@vuepress\/(client|shared)/.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+
!/\.css($|\\?)/.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

Comments
 (0)