/
build-font.js
73 lines (64 loc) · 2.05 KB
/
build-font.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
const webfont = require('webfont').default;
const path = require('path');
const fs = require('fs');
const buildDir = path.join(__dirname, '..', 'build');
if(!fs.existsSync(buildDir)) {
fs.mkdirSync(buildDir);
}
const codepoints = {};
const fontName = 'font-ballerina';
const webfontConfig = {
files: path.resolve(__dirname, '../src/icons/**/*.svg'),
fontHeight: 1000,
normalize: true,
fontName,
templateClassName: 'fw',
template: path.resolve(__dirname, '../src/template/template.css.njk'),
glyphTransformFn: (obj) => {
codepoints[obj.name] = obj.unicode;
return obj;
},
hash: new Date().getTime(),
}
webfont(webfontConfig)
.then(result => {
if(!fs.existsSync(path.join(buildDir, 'font'))) {
fs.mkdirSync(path.join(buildDir, 'font'));
}
['svg', 'ttf', 'eot', 'woff', 'woff2'].forEach(ext => {
const fileName = `font-ballerina.${ext}`;
const filePath = path.join(buildDir, 'font', fileName);
fs.writeFile(filePath, result[ext], err => {
if (err) {
throw err;
}
console.log(`${fileName} written.`);
});
});
// Write the css file
const cssPath = path.join(buildDir, 'font', 'font-ballerina.css')
fs.writeFile(cssPath, result.template, err => {
if (err) {
throw err;
}
console.log('font-ballerina.css written.');
});
// Write the codepoints
const codepointsPath = path.join(buildDir, 'codepoints.json')
fs.writeFile(codepointsPath, JSON.stringify(codepoints), err => {
if (err) {
throw err;
}
console.log('codepoints.json written.');
});
// We write to a .js file also so it can be included with a <script> tag
const codepointsJsPath = path.join(buildDir, 'codepoints.js')
fs.writeFile(codepointsJsPath,
`window.BallerinaFontCodepoints = ${JSON.stringify(codepoints)};`,
err => {
if (err) {
throw err;
}
console.log('codepoints.js written.');
});
});