perf: avoid including optional features in build when not used

pull/198/head
Evan You 4 years ago
parent 8d946a374b
commit c878e6d3b5

@ -1,5 +1,7 @@
declare const __VP_HASH_MAP__: Record<string, string>
declare const __CARBON__: boolean
declare const __BSA__: boolean
declare const __ALGOLIA__: boolean
declare module '*.vue' {
import { ComponentOptions } from 'vue'
const comp: ComponentOptions

@ -77,15 +77,18 @@ import NavBar from './components/NavBar.vue'
import SideBar from './components/SideBar.vue'
import Page from './components/Page.vue'
const Home = defineAsyncComponent(() => import('./components/Home.vue'))
const CarbonAds = defineAsyncComponent(
const NoopComponent = () => null
const CarbonAds = __CARBON__ ? defineAsyncComponent(
() => import('./components/CarbonAds.vue')
)
const BuySellAds = defineAsyncComponent(
) : NoopComponent
const BuySellAds = __BSA__ ? defineAsyncComponent(
() => import('./components/BuySellAds.vue')
)
const AlgoliaSearchBox = defineAsyncComponent(
) : NoopComponent
const AlgoliaSearchBox = __ALGOLIA__ ? defineAsyncComponent(
() => import('./components/AlgoliaSearchBox.vue')
)
) : NoopComponent
// generic state
const route = useRoute()

@ -2,7 +2,7 @@ import path from 'path'
import { Plugin } from 'vite'
import { SiteConfig, resolveSiteData } from './config'
import { createMarkdownToVueRenderFn } from './markdownToVue'
import { APP_PATH, SITE_DATA_REQUEST_PATH } from './alias'
import { APP_PATH, DEFAULT_THEME_PATH, SITE_DATA_REQUEST_PATH } from './alias'
import createVuePlugin from '@vitejs/plugin-vue'
import slash from 'slash'
import { OutputAsset, OutputChunk } from 'rollup'
@ -24,7 +24,7 @@ const isPageChunk = (
export function createVitePressPlugin(
root: string,
{ configPath, aliases, markdown, site: initialSiteData }: SiteConfig,
{ configPath, aliases, markdown, themeDir, site }: SiteConfig,
ssr = false,
pageToHashMap?: Record<string, string>
): Plugin[] {
@ -35,7 +35,9 @@ export function createVitePressPlugin(
ssr
})
let siteData = initialSiteData
let siteData = site
const isUsingDefaultTheme = themeDir === DEFAULT_THEME_PATH
const vitePressPlugin: Plugin = {
name: 'vitepress',
@ -43,7 +45,14 @@ export function createVitePressPlugin(
config() {
return {
alias: aliases,
transformInclude: /\.md$/
transformInclude: /\.md$/,
define: isUsingDefaultTheme
? {
__CARBON__: !!site.themeConfig.carbonAds?.carbon,
__BSA__: !!site.themeConfig.carbonAds?.custom,
__ALGOLIA__: !!site.themeConfig.algolia
}
: {}
}
},

Loading…
Cancel
Save