Skip to content

Commit

Permalink
perf: avoid including optional features in build when not used
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 30, 2020
1 parent 8d946a3 commit c878e6d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/client/shim.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 9 additions & 6 deletions src/client/theme-default/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
17 changes: 13 additions & 4 deletions src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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[] {
Expand All @@ -35,15 +35,24 @@ export function createVitePressPlugin(
ssr
})

let siteData = initialSiteData
let siteData = site

const isUsingDefaultTheme = themeDir === DEFAULT_THEME_PATH

const vitePressPlugin: Plugin = {
name: 'vitepress',

config() {
return {
alias: aliases,
transformInclude: /\.md$/
transformInclude: /\.md$/,
define: isUsingDefaultTheme
? {
__CARBON__: !!site.themeConfig.carbonAds?.carbon,
__BSA__: !!site.themeConfig.carbonAds?.custom,
__ALGOLIA__: !!site.themeConfig.algolia
}
: {}
}
},

Expand Down

0 comments on commit c878e6d

Please sign in to comment.