Skip to content

Commit c878e6d

Browse files
committed
perf: avoid including optional features in build when not used
1 parent 8d946a3 commit c878e6d

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/client/shim.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
declare const __VP_HASH_MAP__: Record<string, string>
2-
2+
declare const __CARBON__: boolean
3+
declare const __BSA__: boolean
4+
declare const __ALGOLIA__: boolean
35
declare module '*.vue' {
46
import { ComponentOptions } from 'vue'
57
const comp: ComponentOptions

src/client/theme-default/Layout.vue

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,18 @@ import NavBar from './components/NavBar.vue'
7777
import SideBar from './components/SideBar.vue'
7878
import Page from './components/Page.vue'
7979
const Home = defineAsyncComponent(() => import('./components/Home.vue'))
80-
const CarbonAds = defineAsyncComponent(
80+
81+
const NoopComponent = () => null
82+
83+
const CarbonAds = __CARBON__ ? defineAsyncComponent(
8184
() => import('./components/CarbonAds.vue')
82-
)
83-
const BuySellAds = defineAsyncComponent(
85+
) : NoopComponent
86+
const BuySellAds = __BSA__ ? defineAsyncComponent(
8487
() => import('./components/BuySellAds.vue')
85-
)
86-
const AlgoliaSearchBox = defineAsyncComponent(
88+
) : NoopComponent
89+
const AlgoliaSearchBox = __ALGOLIA__ ? defineAsyncComponent(
8790
() => import('./components/AlgoliaSearchBox.vue')
88-
)
91+
) : NoopComponent
8992
9093
// generic state
9194
const route = useRoute()

src/node/plugin.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from 'path'
22
import { Plugin } from 'vite'
33
import { SiteConfig, resolveSiteData } from './config'
44
import { createMarkdownToVueRenderFn } from './markdownToVue'
5-
import { APP_PATH, SITE_DATA_REQUEST_PATH } from './alias'
5+
import { APP_PATH, DEFAULT_THEME_PATH, SITE_DATA_REQUEST_PATH } from './alias'
66
import createVuePlugin from '@vitejs/plugin-vue'
77
import slash from 'slash'
88
import { OutputAsset, OutputChunk } from 'rollup'
@@ -24,7 +24,7 @@ const isPageChunk = (
2424

2525
export function createVitePressPlugin(
2626
root: string,
27-
{ configPath, aliases, markdown, site: initialSiteData }: SiteConfig,
27+
{ configPath, aliases, markdown, themeDir, site }: SiteConfig,
2828
ssr = false,
2929
pageToHashMap?: Record<string, string>
3030
): Plugin[] {
@@ -35,15 +35,24 @@ export function createVitePressPlugin(
3535
ssr
3636
})
3737

38-
let siteData = initialSiteData
38+
let siteData = site
39+
40+
const isUsingDefaultTheme = themeDir === DEFAULT_THEME_PATH
3941

4042
const vitePressPlugin: Plugin = {
4143
name: 'vitepress',
4244

4345
config() {
4446
return {
4547
alias: aliases,
46-
transformInclude: /\.md$/
48+
transformInclude: /\.md$/,
49+
define: isUsingDefaultTheme
50+
? {
51+
__CARBON__: !!site.themeConfig.carbonAds?.carbon,
52+
__BSA__: !!site.themeConfig.carbonAds?.custom,
53+
__ALGOLIA__: !!site.themeConfig.algolia
54+
}
55+
: {}
4756
}
4857
},
4958

0 commit comments

Comments
 (0)