forked from lucidlemon/iconfont-plugin-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
210 lines (192 loc) · 6.39 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
'use strict';
const Base = require('run-plugin-webpack');
const SvgiconsToSvgfont = require('svgicons2svgfont');
const Font = require('fonteditor-core').Font;
const woff2 = require('fonteditor-core').woff2;
const fs = require('fs');
const path = require('path');
const svgToTtf = require('svg2ttf');
// checks if a recompilation is necessary
function shouldReplace(svg, cssPath, newCssContent) {
try {
fs.accessSync(svg.path, fs.constants ? fs.constants.R_OK : fs.R_OK);
fs.accessSync(cssPath, fs.constants ? fs.constants.R_OK : fs.R_OK);
} catch(e) {
return true;
}
const oldSvg = fs.readFileSync(svg.path).toString();
const newSvg = svg.contents.toString();
const oldCss = fs.readFileSync(cssPath).toString();
const svgDifferent = oldSvg !== newSvg; // returns true if new SVG is different
const cssDifferent = oldCss !== newCssContent; // returns true if new SCSS is different
// only rerender if svgs or scss are different
return svgDifferent || cssDifferent ? true : false;
}
function noop() {
}
const Plugin = Base.extends(function(options) {
this.options = this.getOptions(options);
});
Plugin.prototype.getOptions = function(options) {
const opts = Object(options);
if(!opts.src || 'string' !== typeof opts.src) {
throw new TypeError('`src` is invalid!');
}
if(!opts.dest || 'string' !== typeof opts.dest.font || 'string' !== typeof opts.dest.css) {
throw new TypeError('`dest` is invalid!');
}
if(typeof opts.dest.alias !== 'undefined' && typeof opts.dest.alias !== 'string') {
throw new TypeError('`alias` is invalid!');
}
if(typeof opts.dest.aliasPath !== 'undefined' && typeof opts.dest.aliasPath !== 'string') {
throw new TypeError('`aliasPath` is invalid!');
}
if(typeof opts.dest.type !== 'undefined' && !Array.isArray(opts.dest.type)) {
throw new TypeError('`type` is invalid!');
}
const defaultTypes = ['eot', 'ttf', 'svg', 'woff', 'woff2'];
const src = path.resolve(opts.src);
const dest = {
font: path.resolve(opts.dest.font),
css: path.resolve(opts.dest.css),
alias: typeof opts.dest.alias === 'string' ? opts.dest.alias : null,
aliasPath: typeof opts.dest.aliasPath === 'string' ? opts.dest.aliasPath : null,
type: opts.dest.type
? opts.dest.type.filter((type) => {
return defaultTypes.find((defaultType) => {
return type === defaultType;
});
})
: defaultTypes
};
const cssTemplate = ('function' === typeof opts.cssTemplate
? opts.cssTemplate
: require('./src/template')
);
return {
src: src,
dest: {
font: dest.font,
css: dest.css,
alias: dest.alias,
aliasPath: dest.aliasPath,
type: dest.type.length < 1 ? defaultTypes : dest.type
},
cssTemplate: cssTemplate,
family: ('string' === typeof opts.family && opts.family) || 'iconfont'
};
};
Plugin.prototype.main = function() {
const src = this.options.src;
const readdir = fs.readdirSync(src);
const useMultipleGroups = readdir.some(function(file) {
return fs.lstatSync(path.join(src, file)).isDirectory();
});
const context = this;
const promises = (useMultipleGroups
? readdir.reduce(function(box, dir) {
const dirPath = path.join(src, dir);
if (fs.lstatSync(dirPath).isDirectory()) {
const files = fs.readdirSync(dirPath).map(function(file) {
return path.resolve(dirPath, file);
});
box.push(context.generateFonts(dir, files));
}
return box;
}, [])
: [ this.generateFonts(this.options.family, readdir.map(function(file) {
return path.resolve(src, file);
})) ]
);
return Promise.all(promises);
};
Plugin.prototype.generateFonts = function(family, files) {
const svgs = files.filter(RegExp.prototype.test.bind(/\.svg$/i));
const context = this;
return new Promise(function(resolve, reject) {
const buffer = [];
const unicodes = [];
const fileStream = new SvgiconsToSvgfont({
fontName: family,
prependUnicode: true,
log: noop,
fontHeight: 5000,
normalize: true
}).on('data', function(data) {
return buffer.push(data);
}).on('end', function() {
return resolve({
contents: Buffer.concat(buffer).toString(),
unicodes: unicodes
});
}).on('error', function(err) {
return reject(err);
});
let startUnicode = 0xEA01;
svgs.forEach(function(file) {
const glyph = fs.createReadStream(file);
const unicode = String.fromCharCode(startUnicode++);
const name = path.parse(file).name;
unicodes.push({ name: name, unicode: unicode });
glyph.metadata = {
name: name,
unicode: [ unicode ]
};
fileStream.write(glyph);
});
fileStream.end();
}).then(function(args) {
const ttf = new Buffer(svgToTtf(args.contents.toString()).buffer);
const fontCreator = Font.create(ttf, {
type: 'ttf',
hinting: true,
compound2simple: true,
inflate: null,
combinePath: true
});
const files = context.options.dest.type.map(async function(type) {
let buffer = null;
if (type === 'woff2') {
buffer = await woff2.init().then(() => {
return fontCreator.write({type: 'woff2'});
});
} else {
buffer = fontCreator.write({
type: type,
hinting: true,
deflate: null
});
}
const filePath = context.options.dest.font
.replace(/\[family\]/g, family)
.replace(/\[type\]/g, type);
return { path:filePath, contents: buffer };
}, []);
return { files: files, unicodes: args.unicodes };
}).then(async function(args) {
const files = await Promise.all(args.files);
const unicodes = args.unicodes;
const pathToFonts = context.options.dest.alias !== null || context.options.dest.aliasPath !== null
? path.resolve(path.dirname(context.options.dest.css), path.dirname(context.options.dest.font)).replace(/\\/g, '/').replace(context.options.dest.aliasPath, context.options.dest.alias)
: path.relative(path.dirname(context.options.dest.css), path.dirname(context.options.dest.font)).replace(/\\/g, '/');
const cssContent = context.options.cssTemplate({
unicodes: unicodes,
family: family,
fontPath: pathToFonts,
fontTypes: context.options.dest.type
});
const cssPath = context.options.dest.css.replace(/\[family\]/g, family);
if(!shouldReplace(files[0], cssPath, cssContent)) {
return;
}
files.forEach(function(file) {
return context.addFile(file.path, file.contents);
});
return Promise.resolve(cssContent)
.then(function(cssContent) {
const cssPath = context.options.dest.css.replace(/\[family\]/g, family);
context.addFile(cssPath, cssContent);
});
}).catch(console.dir);
};
module.exports = Plugin;