feat(Icon): encode woff2

This commit is contained in:
chenjiahan 2020-11-01 20:43:00 +08:00
parent fa5d88d8a3
commit 045c4dc10c
3 changed files with 980 additions and 11 deletions

View File

@ -3,7 +3,6 @@
*/
const { src, dest, series } = require('gulp');
const fs = require('fs-extra');
const path = require('path');
const glob = require('fast-glob');
const shell = require('shelljs');
const encode = require('./build-encode');
@ -12,11 +11,12 @@ const iconfont = require('gulp-iconfont');
const iconfontCss = require('gulp-iconfont-css');
const config = require('../src/config');
const codepoints = require('./codepoints');
const { join } = require('path');
const srcDir = path.join(__dirname, '../src');
const svgDir = path.join(__dirname, '../assets/svg');
const sketch = path.join(__dirname, '../assets/icons.sketch');
const template = path.join(__dirname, './template.tpl');
const srcDir = join(__dirname, '../src');
const svgDir = join(__dirname, '../assets/svg');
const sketch = join(__dirname, '../assets/icons.sketch');
const template = join(__dirname, './template.tpl');
const formats = ['ttf', 'woff', 'woff2'];
// get md5 from sketch
@ -24,9 +24,7 @@ const md5 = md5File.sync(sketch).slice(0, 6);
const fontName = `${config.name}-${md5}`;
// remove previous fonts
const prevFonts = glob.sync(
formats.map((ext) => path.join(srcDir, '*.' + ext))
);
const prevFonts = glob.sync(formats.map((ext) => join(srcDir, '*.' + ext)));
prevFonts.forEach((font) => fs.removeSync(font));
// generate font from svg && build index.less
@ -51,16 +49,30 @@ function font() {
.pipe(dest(srcDir));
}
function encodeWoff2(done) {
const cdnPath = `https://img.yzcdn.cn/vant/${fontName}.woff2`;
const srcFile = join(srcDir, 'index.less');
const woff2Base64 = fs.readFileSync(`../src/${fontName}.woff2`, 'base64');
const woff2DataUrl = `data:font/ttf;base64,${woff2Base64}`;
fs.writeFileSync(
join(srcDir, 'encode-woff2.less'),
fs.readFileSync(srcFile, 'utf-8').replace(cdnPath, woff2DataUrl)
);
done();
}
function upload(done) {
// generate encode.less
encode(fontName, srcDir);
// upload font to cdn
formats.forEach((ext) => {
shell.exec(`superman-cdn /vant ${path.join(srcDir, fontName + '.' + ext)}`);
shell.exec(`superman-cdn /vant ${join(srcDir, fontName + '.' + ext)}`);
});
done();
}
exports.default = series(font, upload);
exports.default = series(font, encodeWoff2, upload);

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long