From c878e6d3b56ecbd71bd75ff4360446d6dacbd70b Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 30 Dec 2020 12:07:14 -0500 Subject: [PATCH] perf: avoid including optional features in build when not used --- src/client/shim.d.ts | 4 +++- src/client/theme-default/Layout.vue | 15 +++++++++------ src/node/plugin.ts | 17 +++++++++++++---- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/client/shim.d.ts b/src/client/shim.d.ts index 166b35e3..c6c262e5 100644 --- a/src/client/shim.d.ts +++ b/src/client/shim.d.ts @@ -1,5 +1,7 @@ declare const __VP_HASH_MAP__: Record - +declare const __CARBON__: boolean +declare const __BSA__: boolean +declare const __ALGOLIA__: boolean declare module '*.vue' { import { ComponentOptions } from 'vue' const comp: ComponentOptions diff --git a/src/client/theme-default/Layout.vue b/src/client/theme-default/Layout.vue index c7b820ee..d18d9ad3 100644 --- a/src/client/theme-default/Layout.vue +++ b/src/client/theme-default/Layout.vue @@ -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() diff --git a/src/node/plugin.ts b/src/node/plugin.ts index 7bf4d5df..a32d6b9c 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -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 ): 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 + } + : {} } },