Fuera automática de alternativa basada en métricas de fuentes
En el proyecto de juegos, habilitar / deshabilitar fontaine hace la siguiente diferencia de representación / , sin requerido la personalización:
| Antes | Después | |
|---|---|---|
| CLS | 0.24 | 0.054 |
| Actuación | 92 | 100 |
Con pnpm
pnpm add -D fontaine O, con npm
npm install -D fontaine O, con yarn
yarn add -D fontaine import { FontaineTransform } from 'fontaine'
// Astro config - astro.config.mjs
import { defineConfig } from 'astro/config'
const options = {
fallbacks : [ 'BlinkMacSystemFont' , 'Segoe UI' , 'Helvetica Neue' , 'Arial' , 'Noto Sans' ] ,
// You may need to resolve assets like `/fonts/Roboto.woff2` to a particular directory
resolvePath : id => `file:///path/to/public/dir ${ id } ` ,
// overrideName: (originalName) => `${name} override`
// sourcemap: false
// skipFontFaceGeneration: (fallbackName) => fallbackName === 'Roboto override'
}
// Vite
export default {
plugins : [ FontaineTransform . vite ( options ) ]
}
// Next.js
export default {
webpack ( config ) {
config . plugins = config . plugins || [ ]
config . plugins . push ( FontaineTransform . webpack ( options ) )
return config
} ,
}
// Docusaurus plugin - to be provided to the plugins option of docusaurus.config.js
// n.b. you'll likely need to require fontaine rather than importing it
const fontaine = require ( 'fontaine' )
function fontainePlugin ( _context , _options ) {
return {
name : 'fontaine-plugin' ,
configureWebpack ( _config , _isServer ) {
return {
plugins : [
fontaine . FontaineTransform . webpack ( options ) ,
] ,
}
} ,
}
}
// Gatsby config - gatsby-node.js
const { FontaineTransform } = require ( 'fontaine' )
exports . onCreateWebpackConfig = ( { stage , actions , getConfig } ) => {
const config = getConfig ( )
config . plugins . push ( FontaineTransform . webpack ( options ) )
actions . replaceWebpackConfig ( config )
}
export default defineConfig ( {
integrations : [ ] ,
vite : {
plugins : [
FontaineTransform . vite ( {
fallbacks : [ 'Arial' ] ,
resolvePath : id => new URL ( `./public ${ id } ` , import . meta . url ) , // id is the font src value in the CSS
} ) ,
] ,
} ,
} )Nota Si está utilizando Nuxt, consulte Nuxt-Font-Metrics que usa
fontainedebajo del capó.
Si su fuente personalizada se usa a través del mecanismo de las variables CSS, deberá hacer un ajuste a sus variables CSS para ayudar a Fontaine. DocUSaurus es un ejemplo de esto, utiliza la variable --ifm-font-family-base para hacer referencia a una fuente personalizada. Para que Fontaine pueda conectar la variable con la fuente, necesitamos agregar un sufijo {Name of Font} override a esa variable. ¿Cómo es esto? Bueno, imagine que estábamos usando las Poppins de fuentes personalizadas a las que se hace referencia a la variable --ifm-font-family-base , haríamos el siguiente ajuste:
:root {
/* ... */
- --ifm-font-family-base: 'Poppins';
+ --ifm-font-family-base: 'Poppins', 'Poppins override'; Detrás de escena, hay una regla de 'Poppins Anule' @font-face que ha sido creada por Fontaine. Al agregar manualmente esta familia de fuentes de anulación a nuestra variable CSS, hacemos que nuestro sitio use la regla Fallback @font-face con las métricas de fuentes correctas que Fontaine genera.
fontaine escaneará sus reglas de @font-face y generará reglas de alojamiento con las métricas correctas. Por ejemplo:
@font-face {
font-family : 'Roboto' ;
font-display : swap;
src : url ( '/fonts/Roboto.woff2' ) format ( 'woff2' ) , url ( '/fonts/Roboto.woff' )
format ( 'woff' );
font-weight : 700 ;
}
/* This additional font-face declaration will be added to your CSS. */
@font-face {
font-family : 'Roboto override' ;
src : local ( 'BlinkMacSystemFont' ) , local ( 'Segoe UI' ) , local ( 'Helvetica Neue' ) ,
local ( 'Arial' ) , local ( 'Noto Sans' );
ascent-override : 92.7734375 % ;
descent-override : 24.4140625 % ;
line-gap-override : 0 % ;
} Luego, cada vez que use font-family: 'Roboto' , fontaine agregará la anulación a la familia de fuentes:
: root {
font-family : 'Roboto' ;
/* This becomes */
font-family : 'Roboto' , 'Roboto override' ;
}corepack enable Enable (use npm i -g corepack para Node.js <16.10)pnpm installpnpm dev ; Iniciar un servidor VITE usando el código fuente con pnpm demo:dev Esto no hubiera sido posible sin:
Hecho con ❤️
Publicado bajo la licencia MIT.