49 lines
1.5 KiB
JavaScript
49 lines
1.5 KiB
JavaScript
/**
|
||
* 从 node_modules 的 woff2 生成 base64 内嵌字体样式。
|
||
*
|
||
* 小程序 wxss 的 @font-face 不支持本地路径(渲染层报 do-not-use-local-path,
|
||
* 图标全部变豆腐块),只能用网络 URL 或 base64;base64 免部署免域名配置,
|
||
* 三端(H5/APK/小程序)统一可用。
|
||
*
|
||
* 升级 @fortawesome/fontawesome-free 后执行: node scripts/gen-fa-fonts.js
|
||
* 产物 src/styles/fontawesome.scss 为生成文件,请勿手改。
|
||
*/
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
const fonts = [
|
||
{
|
||
file: 'fa-solid-900.woff2',
|
||
family: 'Font Awesome 7 Free',
|
||
weight: 900,
|
||
style: 'normal'
|
||
},
|
||
{
|
||
file: 'fa-brands-400.woff2',
|
||
family: 'Font Awesome 7 Brands',
|
||
weight: 400,
|
||
style: 'normal'
|
||
}
|
||
]
|
||
|
||
const webfontDir = path.join(
|
||
__dirname,
|
||
'../node_modules/@fortawesome/fontawesome-free/webfonts'
|
||
)
|
||
const outputPath = path.join(__dirname, '../src/styles/fontawesome.scss')
|
||
|
||
const blocks = fonts.map((f) => {
|
||
const b64 = fs.readFileSync(path.join(webfontDir, f.file)).toString('base64')
|
||
return `@font-face {
|
||
font-family: '${f.family}';
|
||
font-style: ${f.style};
|
||
font-weight: ${f.weight};
|
||
font-display: block;
|
||
src: url('data:font/woff2;base64,${b64}') format('woff2');
|
||
}`
|
||
})
|
||
|
||
const content = `/* 自动生成,请勿手改:node scripts/gen-fa-fonts.js */\n\n${blocks.join('\n\n')}\n`
|
||
fs.writeFileSync(outputPath, content, 'utf8')
|
||
console.log(`Generated base64 font-face (${(content.length / 1024).toFixed(0)} KB) -> src/styles/fontawesome.scss`)
|