Skip to content

Commit c9f04e0

Browse files
authored
feat(build): add useWebFonts option (#1531)
1 parent 09fcc46 commit c9f04e0

File tree

6 files changed

+43
-4
lines changed

6 files changed

+43
-4
lines changed

src/client/theme-default/styles/base.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ body {
2626
color: var(--vp-c-text-1);
2727
background-color: var(--vp-c-bg);
2828
direction: ltr;
29-
font-synthesis: none;
29+
font-synthesis: style;
3030
text-rendering: optimizeLegibility;
3131
-webkit-font-smoothing: antialiased;
3232
-moz-osx-font-smoothing: grayscale;

src/client/theme-default/styles/fonts.css

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/* webfont-marker-begin */
2+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@100..900&display=swap');
3+
/* webfont-marker-end */
4+
15
@font-face {
26
font-family: 'Inter var';
37
font-weight: 100 900;

src/client/theme-default/styles/vars.css

+3-3
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@
145145
* -------------------------------------------------------------------------- */
146146

147147
:root {
148-
--vp-font-family-base: 'Inter var experimental', 'Inter var', ui-sans-serif,
149-
system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
150-
'Helvetica Neue', Helvetica, Arial, 'Noto Sans', sans-serif,
148+
--vp-font-family-base: 'Inter var experimental', 'Inter var', 'Inter',
149+
ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI',
150+
Roboto, 'Helvetica Neue', Helvetica, Arial, 'Noto Sans', sans-serif,
151151
'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
152152
--vp-font-family-mono: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Monaco,
153153
Consolas, 'Liberation Mono', 'Courier New', monospace;

src/node/config.ts

+14
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ export interface UserConfig<ThemeConfig = any> {
8787
*/
8888
cleanUrls?: CleanUrlsMode
8989

90+
/**
91+
* Use web fonts instead of emitting font files to dist.
92+
* The used theme should import a file named `fonts.(s)css` for this to work.
93+
* If you are a theme author, to support this, place your web font import
94+
* between `webfont-marker-begin` and `webfont-marker-end` comments.
95+
*
96+
* @default true in webcontainers, else false
97+
*/
98+
useWebFonts?: boolean
99+
90100
/**
91101
* Build end hook: called when SSG finish.
92102
* @param siteConfig The resolved configuration.
@@ -142,6 +152,7 @@ export interface SiteConfig<ThemeConfig = any>
142152
| 'lastUpdated'
143153
| 'ignoreDeadLinks'
144154
| 'cleanUrls'
155+
| 'useWebFonts'
145156
| 'buildEnd'
146157
| 'transformHead'
147158
| 'transformHtml'
@@ -230,6 +241,9 @@ export async function resolveConfig(
230241
mpa: !!userConfig.mpa,
231242
ignoreDeadLinks: userConfig.ignoreDeadLinks,
232243
cleanUrls: userConfig.cleanUrls || 'disabled',
244+
useWebFonts:
245+
userConfig.useWebFonts ??
246+
typeof process.versions.webcontainer === 'string',
233247
buildEnd: userConfig.buildEnd,
234248
transformHead: userConfig.transformHead,
235249
transformHtml: userConfig.transformHtml,

src/node/plugin.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { slash } from './utils/slash'
1313
import { OutputAsset, OutputChunk } from 'rollup'
1414
import { staticDataPlugin } from './staticDataPlugin'
1515
import { PageDataPayload } from './shared'
16+
import { webFontsPlugin } from './webFontsPlugin'
1617

1718
const hashRE = /\.(\w+)\.js$/
1819
const staticInjectMarkerRE =
@@ -315,6 +316,7 @@ export async function createVitePressPlugin(
315316
return [
316317
vitePressPlugin,
317318
vuePlugin,
319+
webFontsPlugin(siteConfig.useWebFonts),
318320
...(userViteConfig?.plugins || []),
319321
staticDataPlugin
320322
]

src/node/webFontsPlugin.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Plugin } from 'vite'
2+
3+
const webfontMarkerRE =
4+
/\/(?:\*|\/) *webfont-marker-begin *(?:\*\/|\n|\r|\n\r|\r\n)([^]*?)\/(?:\*|\/) *webfont-marker-end *(?:\*\/|\n|\r|\n\r|\r\n|$)/
5+
6+
export const webFontsPlugin = (enabled = false): Plugin => ({
7+
name: 'vitepress:webfonts',
8+
enforce: 'pre',
9+
10+
transform(code, id) {
11+
if (/[\\/]fonts\.s?css/.test(id)) {
12+
if (enabled) {
13+
return code.match(webfontMarkerRE)?.[1]
14+
} else {
15+
return code.replace(webfontMarkerRE, '')
16+
}
17+
}
18+
}
19+
})

0 commit comments

Comments
 (0)