Skip to content

Commit 1ef69e2

Browse files
committed
feat: improve default chunk strategy + page hash stability
1 parent 9f7323f commit 1ef69e2

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

src/client/theme-default/Layout.vue

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import { useRoute, useData } from 'vitepress'
44
import { isSideBarEmpty, getSideBarConfig } from './support/sideBar'
55
66
// components
7+
import Home from './components/Home.vue'
78
import NavBar from './components/NavBar.vue'
89
import SideBar from './components/SideBar.vue'
910
import Page from './components/Page.vue'
1011
11-
const Home = defineAsyncComponent(() => import('./components/Home.vue'))
12-
1312
const NoopComponent = () => null
1413
1514
const CarbonAds = __CARBON__

src/node/build/bundle.ts

+68-7
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,25 @@ export async function bundle(
7373
...(ssr
7474
? {}
7575
: {
76-
chunkFileNames(chunk): string {
77-
if (!chunk.isEntry && /runtime/.test(chunk.name)) {
78-
return `assets/framework.[hash].js`
76+
chunkFileNames(chunk) {
77+
// avoid ads chunk being intercepted by adblock
78+
return /(?:Carbon|BuySell)Ads/.test(chunk.name)
79+
? `assets/chunks/ui-custom.[hash].js`
80+
: `assets/chunks/[name].[hash].js`
81+
},
82+
manualChunks(id, ctx) {
83+
// move known framework code into a stable chunk so that
84+
// custom theme changes do not invalidate hash for all pages
85+
if (id.includes('plugin-vue:export-helper')) {
86+
return 'framework'
87+
}
88+
if (
89+
isEagerChunk(id, ctx) &&
90+
(/@vue\/(runtime|shared|reactivity)/.test(id) ||
91+
/vitepress\/dist\/client/.test(id))
92+
) {
93+
return 'framework'
7994
}
80-
return adComponentRE.test(chunk.name)
81-
? `assets/ui-custom.[hash].js`
82-
: `assets/[name].[hash].js`
8395
}
8496
})
8597
}
@@ -133,4 +145,53 @@ export async function bundle(
133145
return { clientResult, serverResult, pageToHashMap }
134146
}
135147

136-
const adComponentRE = /(?:Carbon|BuySell)Ads/
148+
const cache = new Map<string, boolean>()
149+
150+
/**
151+
* Check if a module is statically imported by at least one entry.
152+
*/
153+
function isEagerChunk(id: string, { getModuleInfo }: any) {
154+
if (
155+
id.includes('node_modules') &&
156+
!/\.css($|\\?)/.test(id) &&
157+
staticImportedByEntry(id, getModuleInfo, cache)
158+
) {
159+
return 'vendor'
160+
}
161+
}
162+
163+
function staticImportedByEntry(
164+
id: string,
165+
getModuleInfo: any,
166+
cache: Map<string, boolean>,
167+
importStack: string[] = []
168+
): boolean {
169+
if (cache.has(id)) {
170+
return cache.get(id) as boolean
171+
}
172+
if (importStack.includes(id)) {
173+
// circular deps!
174+
cache.set(id, false)
175+
return false
176+
}
177+
const mod = getModuleInfo(id)
178+
if (!mod) {
179+
cache.set(id, false)
180+
return false
181+
}
182+
183+
if (mod.isEntry) {
184+
cache.set(id, true)
185+
return true
186+
}
187+
const someImporterIs = mod.importers.some((importer: string) =>
188+
staticImportedByEntry(
189+
importer,
190+
getModuleInfo,
191+
cache,
192+
importStack.concat(id)
193+
)
194+
)
195+
cache.set(id, someImporterIs)
196+
return someImporterIs
197+
}

0 commit comments

Comments
 (0)