-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
99 lines (80 loc) · 2.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* @file edp-build fontmin
* @author junmer
*/
/* eslint-env node */
var extend = require('extend');
var path = require('path');
var Fontmin = require('fontmin');
/**
* fontMinify 处理器
*
* @param {FileObject} file 处理器文件对象
* @param {processContext} processContext 处理器上下文
* @param {Function} done 完成回调
*/
function fontMinify(file, processContext, done) {
var text = this.text || '';
var entryFiles = this.entryFiles;
if (entryFiles) {
var entryText = [];
processContext
.getFilesByPatterns(entryFiles)
.forEach(function (entryFile) {
entryText.push(entryFile.data);
});
text += entryText.join('');
}
var chineseOnly = this.chineseOnly;
if (chineseOnly) {
text = text.replace(/[^\u4e00-\u9fa5]/g, '');
}
var srcPath = file.path;
var outputDir = processContext.outputDir;
var destPath = path.dirname(file.outputPath);
destPath = path.resolve(outputDir, destPath); // 获取目标地址,传入 fontmin.dest
file.outputPath = null; // 清除 edp 构建结果
var fontmin = new Fontmin()
.src(srcPath)
.use(Fontmin.glyph({
text: text
}))
.use(Fontmin.ttf2eot())
.use(Fontmin.ttf2woff())
.use(Fontmin.ttf2svg())
.dest(destPath);
var me = this;
fontmin.run(function (err, files, stream) {
if (err) {
me.log.error(err);
}
done();
});
}
/**
* fontProcessor 默认配置
*
* @type {Object}
*/
var fontProcessor = {
files: ['*.ttf'],
entryFiles: ['*.html'],
text: '',
chineseOnly: false,
name: 'FontCompressor',
process: fontMinify
};
/**
* FontProcessor 构造函数
*
* @param {Object} opt 配置
* @param {Array} opt.files 字体文件
* @param {Array} opt.entryFiles 引用字体的网页,用来扫描所需字型
* @param {string} opt.text 人肉配置所需字型
* @param {boolean} opt.chineseOnly 只取中文字型,忽略 数字、英文、标点
* @return {Object} FontProcessor instance
*/
function FontProcessor(opt) {
return extend({}, fontProcessor, opt);
}
module.exports = exports = FontProcessor;