Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): add useWebFonts option #1531

Merged
merged 4 commits into from Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/client/theme-default/styles/base.css
Expand Up @@ -26,7 +26,7 @@ body {
color: var(--vp-c-text-1);
background-color: var(--vp-c-bg);
direction: ltr;
font-synthesis: none;
font-synthesis: style;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
Expand Down
4 changes: 4 additions & 0 deletions src/client/theme-default/styles/fonts.css
@@ -1,3 +1,7 @@
/* webfont-marker-begin */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@100..900&display=swap');
/* webfont-marker-end */

@font-face {
font-family: 'Inter var';
font-weight: 100 900;
Expand Down
6 changes: 3 additions & 3 deletions src/client/theme-default/styles/vars.css
Expand Up @@ -145,9 +145,9 @@
* -------------------------------------------------------------------------- */

:root {
--vp-font-family-base: 'Inter var experimental', 'Inter var', ui-sans-serif,
system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
'Helvetica Neue', Helvetica, Arial, 'Noto Sans', sans-serif,
--vp-font-family-base: 'Inter var experimental', 'Inter var', 'Inter',
ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI',
Roboto, 'Helvetica Neue', Helvetica, Arial, 'Noto Sans', sans-serif,
'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
--vp-font-family-mono: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Monaco,
Consolas, 'Liberation Mono', 'Courier New', monospace;
Expand Down
14 changes: 14 additions & 0 deletions src/node/config.ts
Expand Up @@ -87,6 +87,16 @@ export interface UserConfig<ThemeConfig = any> {
*/
cleanUrls?: CleanUrlsMode

/**
* Use web fonts instead of emitting font files to dist.
* The used theme should import a file named `fonts.(s)css` for this to work.
* If you are a theme author, to support this, place your web font import
* between `webfont-marker-begin` and `webfont-marker-end` comments.
*
* @default true in webcontainers, else false
*/
useWebFonts?: boolean

/**
* Build end hook: called when SSG finish.
* @param siteConfig The resolved configuration.
Expand Down Expand Up @@ -142,6 +152,7 @@ export interface SiteConfig<ThemeConfig = any>
| 'lastUpdated'
| 'ignoreDeadLinks'
| 'cleanUrls'
| 'useWebFonts'
| 'buildEnd'
| 'transformHead'
| 'transformHtml'
Expand Down Expand Up @@ -230,6 +241,9 @@ export async function resolveConfig(
mpa: !!userConfig.mpa,
ignoreDeadLinks: userConfig.ignoreDeadLinks,
cleanUrls: userConfig.cleanUrls || 'disabled',
useWebFonts:
userConfig.useWebFonts ??
typeof process.versions.webcontainer === 'string',
buildEnd: userConfig.buildEnd,
transformHead: userConfig.transformHead,
transformHtml: userConfig.transformHtml,
Expand Down
2 changes: 2 additions & 0 deletions src/node/plugin.ts
Expand Up @@ -13,6 +13,7 @@ import { slash } from './utils/slash'
import { OutputAsset, OutputChunk } from 'rollup'
import { staticDataPlugin } from './staticDataPlugin'
import { PageDataPayload } from './shared'
import { webFontsPlugin } from './webFontsPlugin'

const hashRE = /\.(\w+)\.js$/
const staticInjectMarkerRE =
Expand Down Expand Up @@ -315,6 +316,7 @@ export async function createVitePressPlugin(
return [
vitePressPlugin,
vuePlugin,
webFontsPlugin(siteConfig.useWebFonts),
...(userViteConfig?.plugins || []),
staticDataPlugin
]
Expand Down
19 changes: 19 additions & 0 deletions src/node/webFontsPlugin.ts
@@ -0,0 +1,19 @@
import { Plugin } from 'vite'

const webfontMarkerRE =
/\/(?:\*|\/) *webfont-marker-begin *(?:\*\/|\n|\r|\n\r|\r\n)([^]*?)\/(?:\*|\/) *webfont-marker-end *(?:\*\/|\n|\r|\n\r|\r\n|$)/

export const webFontsPlugin = (enabled = false): Plugin => ({
name: 'vitepress:webfonts',
enforce: 'pre',

transform(code, id) {
if (/[\\/]fonts\.s?css/.test(id)) {
if (enabled) {
return code.match(webfontMarkerRE)?.[1]
} else {
return code.replace(webfontMarkerRE, '')
}
}
}
})