Skip to content

Commit da1691d

Browse files
committed
feat(theme): support extending default theme without importing fonts
1 parent 24735db commit da1691d

File tree

5 files changed

+86
-31
lines changed

5 files changed

+86
-31
lines changed

docs/guide/extending-default-theme.md

+46
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,52 @@ export default DefaultTheme
3636

3737
See [default theme CSS variables](https://github.com/vuejs/vitepress/blob/main/src/client/theme-default/styles/vars.css) that can be overridden.
3838

39+
## Using Different Fonts
40+
41+
VitePress uses [Inter](https://rsms.me/inter/) as the default font, and will include the fonts in the build output. The font is also auto preloaded in production. However, this may not be desirable if you want to use a different main font.
42+
43+
To avoid including Inter in the build output, import the theme from `vitepress/theme-without-fonts` instead:
44+
45+
```js
46+
// .vitepress/theme/index.js
47+
import DefaultTheme from 'vitepress/theme-without-fonts'
48+
import './my-fonts.css'
49+
50+
export default DefaultTheme
51+
```
52+
53+
```css
54+
/* .vitepress/theme/custom.css */
55+
:root {
56+
--vp-font-family-base: /* normal text font */
57+
--vp-font-family-mono: /* code font */
58+
}
59+
```
60+
61+
If your font is a local file referenced via `@font-face`, it will be processed as an asset and included under `.vitepress/dist/assets` with hashed filename. To preload this file, use the [transformHead](/reference/site-config#transformhead) build hook:
62+
63+
```js
64+
// .vitepress/config.js
65+
export default {
66+
transformHead({ assets }) {
67+
// adjust the regex accordingly to match your font
68+
const myFontFile = assets.find(file => /font-name\.\w+\.woff2/)
69+
if (myFontFile) {
70+
return [
71+
'link',
72+
{
73+
rel: 'preload',
74+
href: myFontFile,
75+
as: 'font',
76+
type: 'font/woff2',
77+
crossorigin: ''
78+
}
79+
]
80+
}
81+
}
82+
}
83+
```
84+
3985
## Registering Global Components
4086

4187
```js

docs/reference/site-config.md

+2
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ export default {
477477

478478
```ts
479479
interface TransformContext {
480+
page: string // e.g. index.md (relative to srcDir)
481+
assets: string[] // all non-js/css assets as fully resolved public URL
480482
siteConfig: SiteConfig
481483
siteData: SiteData
482484
pageData: PageData

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
"./theme": {
2222
"types": "./theme.d.ts",
2323
"default": "./dist/client/theme-default/index.js"
24+
},
25+
"./theme-without-fonts": {
26+
"types": "./theme.d.ts",
27+
"default": "./dist/client/theme-default/without-fonts.js"
2428
}
2529
},
2630
"bin": {

src/client/theme-default/index.ts

+1-31
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,4 @@
11
import './styles/fonts.css'
2-
import './styles/vars.css'
3-
import './styles/base.css'
4-
import './styles/utils.css'
5-
import './styles/components/custom-block.css'
6-
import './styles/components/vp-code.css'
7-
import './styles/components/vp-code-group.css'
8-
import './styles/components/vp-doc.css'
9-
import './styles/components/vp-sponsor.css'
10-
11-
import type { Theme } from 'vitepress'
12-
import VPBadge from './components/VPBadge.vue'
13-
import Layout from './Layout.vue'
14-
15-
// Note: if we add more optional components here, i.e. components that are not
16-
// used in the theme by default unless the user imports them, make sure to update
17-
// the `lazyDefaultThemeComponentsRE` regex in src/node/build/bundle.ts.
18-
export { default as VPHomeHero } from './components/VPHomeHero.vue'
19-
export { default as VPHomeFeatures } from './components/VPHomeFeatures.vue'
20-
export { default as VPHomeSponsors } from './components/VPHomeSponsors.vue'
21-
export { default as VPDocAsideSponsors } from './components/VPDocAsideSponsors.vue'
22-
export { default as VPTeamPage } from './components/VPTeamPage.vue'
23-
export { default as VPTeamPageTitle } from './components/VPTeamPageTitle.vue'
24-
export { default as VPTeamPageSection } from './components/VPTeamPageSection.vue'
25-
export { default as VPTeamMembers } from './components/VPTeamMembers.vue'
26-
27-
const theme: Theme = {
28-
Layout,
29-
enhanceApp: ({ app }) => {
30-
app.component('Badge', VPBadge)
31-
}
32-
}
332

3+
import theme from './without-fonts.js'
344
export default theme
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import './styles/vars.css'
2+
import './styles/base.css'
3+
import './styles/utils.css'
4+
import './styles/components/custom-block.css'
5+
import './styles/components/vp-code.css'
6+
import './styles/components/vp-code-group.css'
7+
import './styles/components/vp-doc.css'
8+
import './styles/components/vp-sponsor.css'
9+
10+
import type { Theme } from 'vitepress'
11+
import VPBadge from './components/VPBadge.vue'
12+
import Layout from './Layout.vue'
13+
14+
// Note: if we add more optional components here, i.e. components that are not
15+
// used in the theme by default unless the user imports them, make sure to update
16+
// the `lazyDefaultThemeComponentsRE` regex in src/node/build/bundle.ts.
17+
export { default as VPHomeHero } from './components/VPHomeHero.vue'
18+
export { default as VPHomeFeatures } from './components/VPHomeFeatures.vue'
19+
export { default as VPHomeSponsors } from './components/VPHomeSponsors.vue'
20+
export { default as VPDocAsideSponsors } from './components/VPDocAsideSponsors.vue'
21+
export { default as VPTeamPage } from './components/VPTeamPage.vue'
22+
export { default as VPTeamPageTitle } from './components/VPTeamPageTitle.vue'
23+
export { default as VPTeamPageSection } from './components/VPTeamPageSection.vue'
24+
export { default as VPTeamMembers } from './components/VPTeamMembers.vue'
25+
26+
const theme: Theme = {
27+
Layout,
28+
enhanceApp: ({ app }) => {
29+
app.component('Badge', VPBadge)
30+
}
31+
}
32+
33+
export default theme

0 commit comments

Comments
 (0)