-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgruntfile.js
84 lines (67 loc) · 3.02 KB
/
gruntfile.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
module.exports = function(grunt) {
grunt.initConfig({
uglify: {
fontloader: {
files: {
'build/fontloader.js': 'src/js/**/*.js'
}
}
},
replace: {
inlinejs: {
src: ['src/demo*.html'],
dest: 'build/',
//files: {
//},
//src: 'src/demo*.html',
//dest: 'build/demo.html',
replacements: [{
from: '{{ fontloader.js }}',
to: '<%= grunt.file.read("build/fontloader.js") %>'
}]
}
}
});
grunt.registerTask('default', ['uglify', 'replace']);
grunt.registerTask('generateFonts', function() {
var fs = require('fs');
var util = require('util');
var woffFile = fs.openSync('build/fonts.woff.css', 'w');
var woff2File = fs.openSync('build/fonts.woff2.css', 'w');
var files = fs.readdirSync('src/fonts/subsets');
files.forEach(function(file) {
if (!/\.woff/.test(file)) {
return;
}
var b64 = fs.readFileSync('src/fonts/subsets/' + file).toString('base64');
var format = file.indexOf('.woff2') >= 0 ? "woff2" : "woff";
var fontWeight = /Semibold/i.test(file) ? 600 : 400;
var fontStyle = /It/.test(file) ? 'italic' : 'normal';
var fontFamily = file.split('.')[0].split('-')[0].split(/(?=[A-Z])/).join(' ');
var template = '@font-face{font-family:%s;src:url(data:application/x-font-%s;charset=utf-8;base64,%s) format("%s");font-weight:%d;font-style:%s}\n';
var css = util.format(template, fontFamily, format, b64, format, fontWeight, fontStyle);
fs.write(format === 'woff' ? woffFile : woff2File, css);
});
fs.closeSync(woffFile);
fs.closeSync(woff2File);
/*
var cfg = grunt.file.readJSON('src/fonts/config.json');
for (var filename in cfg) {
var path = 'build/' + filename;
fs.writeFileSync(path, '');
cfg[filename].forEach(function(c) {
console.log(c);
var b64 = fs.readFileSync('src/fonts/subsets/' + c.file).toString('base64');
var fontFamily = c['font-family'];
var fontWeight = c['font-weight'] || '400';
var fontStyle = c['font-style'] || 'normal';
var format = c['format'] || 'woff';
var template = '@font-face{font-family:%s;src:local("%s"),url(data:application/x-font-%s;charset=utf-8;base64,%s) format("%s");font-weight:%d;font-style:%s}';
var css = util.format(template, fontFamily, )
fs.appendFileSync(path, '');
});
}*/
});
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-text-replace');
};