Skip to content

Commit

Permalink
fix(icon/lib): 增加base64构建模式
Browse files Browse the repository at this point in the history
affects: @varlet/icons, @varlet/ui
  • Loading branch information
haoziqaq committed Apr 11, 2021
1 parent f6d2803 commit ae682cc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
40 changes: 26 additions & 14 deletions packages/varlet-icons/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const SVG_DIR = resolve(CWD, 'svg')
const DIST_DIR = resolve(CWD, 'dist')
const FONTS_DIR = resolve(DIST_DIR, 'fonts')
const CSS_DIR = resolve(DIST_DIR, 'css')
const formats = ['ttf', 'eot', 'woff', 'woff2']
const formats = ['ttf', 'woff', 'woff2']

const config = require(resolve(CWD, 'varlet-icons.config.js'))

async function build() {
const { publicPath, namespace, fontName, fileName, fontWeight = 'normal', fontStyle = 'normal' } = config
const { base64, publicPath, namespace, fontName, fileName, fontWeight = 'normal', fontStyle = 'normal' } = config

const { ttf, eot, woff, woff2 } = await webfont({
const { ttf, woff, woff2 } = await webfont({
files: `${SVG_DIR}/*.svg`,
fontName,
formats,
Expand All @@ -39,11 +39,32 @@ async function build() {
}
})

const iconNames = icons.map((iconName) => ` "${iconName.name}"`)

const indexTemplate = `\
module.exports = [
${iconNames.join(',\n')}
]
`

const cssTemplate = `\
@font-face {
font-family: "${fontName}";
src: url("${publicPath}${fileName}-webfont.eot");
src: url("${publicPath}${fileName}-webfont.eot") format("embedded-opentype"), url("${publicPath}${fileName}-webfont.woff2") format("woff2"), url("${publicPath}${fileName}-webfont.woff") format("woff"), url("${publicPath}${fileName}-webfont.ttf") format("truetype");
src: url("${
base64
? `data:application/font-woff2;charset=utf-8;base64,${Buffer.from(woff2).toString('base64')}`
: `${publicPath}${fileName}-webfont.woff2`
}") format("woff2"),
url("${
base64
? `data:application/font-woff;charset=utf-8;base64,${woff.toString('base64')}`
: `${publicPath}${fileName}-webfont.woff`
}") format("woff"),
url("${
base64
? `data:font/truetype;charset=utf-8;base64,${ttf.toString('base64')}`
: `${publicPath}${fileName}-webfont.ttf`
}") format("truetype");
font-weight: ${fontWeight};
font-style: ${fontStyle};
}
Expand All @@ -67,17 +88,8 @@ ${icons
.join('\n\n')}
`

const iconNames = icons.map((iconName) => ` "${iconName.name}"`)

const indexTemplate = `\
module.exports = [
${iconNames.join(',\n')}
]
`

await Promise.all([
writeFile(resolve(FONTS_DIR, `${fileName}-webfont.ttf`), ttf),
writeFile(resolve(FONTS_DIR, `${fileName}-webfont.eot`), eot),
writeFile(resolve(FONTS_DIR, `${fileName}-webfont.woff`), woff),
writeFile(resolve(FONTS_DIR, `${fileName}-webfont.woff2`), woff2),
writeFile(resolve(CSS_DIR, `${fileName}.css`), cssTemplate),
Expand Down
2 changes: 1 addition & 1 deletion packages/varlet-icons/varlet-icons.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
namespace: 'var-icon',
publicPath: '~@varlet/icons/dist/fonts/',
fileName: 'varlet-icons',
fontName: 'varlet-icons',
base64: true,
}
1 change: 1 addition & 0 deletions packages/varlet-ui/docs/quickstart.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
```

### Webpack/Vite

```shell
# 通过npm或yarn安装
# npm
Expand Down

0 comments on commit ae682cc

Please sign in to comment.