-
Notifications
You must be signed in to change notification settings - Fork 22
/
gulpfile.babel.js
145 lines (125 loc) 路 4.09 KB
/
gulpfile.babel.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
var del = require('del');
var gulp = require('gulp');
var path = require('path');
var jimp = require('gulp-jimp');
var through = require('through2');
var rename = require('gulp-rename');
var svg2png = require('gulp-svg2png');
var iconfont = require('gulp-iconfont');
var bufferstreams = require('bufferstreams');
var child_process = require('child_process');
var consolidate = require('gulp-consolidate');
var FONT_NAME = 'cubing-icons';
var SVG_FILES = "svgs/*/*.svg";
var STATIC_FILES = "static/*/*";
var TEMPLATE_FILES = 'templates/*.lodash';
var SRC_FILES = [ SVG_FILES, TEMPLATE_FILES ];
var runTimestamp = Math.round(Date.now()/1000);
const defaultTask = gulp.parallel(copySvgs, copyStaticFiles, css);
export default defaultTask;
export function css() {
var fontCss = fontCssPipe();
return gulp.src(SVG_FILES)
// We convert the SVGs to PNG, to BMP, and then back to SVG
// in order to produce the simplest SVGs possible, as iconfont
// is not very resilient in what it accepts.
.pipe(svg2png())
// svg2png looks for files on the filesystem by name, so we cannot
// rename until after svg2png. We should really fix this bug in svg2png.
.pipe(through.obj(function(file, enc, next) {
file.path = path.join(path.dirname(file.path), file.relative.replace("/", "-"));
next(null, file);
}))
.pipe(jimp({
'': {
background: '#FFFFFF', // Convert transparent to white
type: 'bmp',
}
}))
.pipe(bmp2svg())
.pipe(iconfont({
fontName: FONT_NAME,
formats: ['ttf', 'woff'],
timestamp: runTimestamp, // get consistent builds when watching files
normalize: true,
fontHeight: 1000,
}))
.on('glyphs', function(glyphs, options) {
// Stash glyphs so we can generate the CSS at the end.
fontCss.glyphs = glyphs;
gulp.src('templates/index.html.lodash')
.pipe(consolidate('lodash', {
glyphs: glyphs,
}))
.pipe(rename('index.html'))
.pipe(gulp.dest('www/'));
})
.pipe(gulp.dest('www/fonts/'))
.pipe(fontCss);
};
export function copyStaticFiles() {
return gulp.src(STATIC_FILES).pipe(gulp.dest("www/"));
}
export function copySvgs() {
return gulp.src(SVG_FILES)
.pipe(gulp.dest('www/svgs/'));
}
export const watch = gulp.series(defaultTask, function watching() {
gulp.watch(SRC_FILES, defaultTask);
});
export function clean() {
return del('www');
};
function fontCssPipe() {
var fontFiles = [];
return through.obj(function(file, enc, cb) {
file.contents.pipe(new bufferstreams(function(err, buf, cb) {
if(err) {
throw err;
}
fontFiles[file.extname.substring(1)] = buf.toString('base64');
cb();
})).on('finish', cb);
}, function(cb) {
// Modified from https://www.npmjs.com/package/gulp-iconfont
var fontUrls = {
woff: "data:application/x-font-woff;charset=utf-8;base64," + fontFiles.woff,
ttf: "data:application/x-font-ttf;charset=utf-8;base64," + fontFiles.ttf,
};
gulp.src('templates/cubing-icons.css.lodash')
.pipe(consolidate('lodash', {
glyphs: this.glyphs,
fontName: FONT_NAME,
fontUrls: fontUrls,
className: 'cubing-icon',
}))
.pipe(rename('cubing-icons.css'))
.pipe(gulp.dest('www/css/'))
.on('end', cb);
});
}
function bmp2svg() {
return through.obj(function(file, enc, next) {
var potraceProcess = child_process.spawn(
'potrace', ['-o', '-', '-b', 'svg'], {
stdio: [ 'pipe', 'pipe', 'inherit' ]
}
);
potraceProcess.stdin.write(file.contents);
potraceProcess.stdin.end();
// TODO - there must be some way of avoiding this...
var buffer = new Buffer.alloc(0);
potraceProcess.stdout.on('data', function(data) {
buffer = Buffer.concat([ buffer, data ]);
});
potraceProcess.once('close', function(code) {
if(code !== 0) {
next(new Error("potrace exited with code " + code, null));
} else {
file.extname = ".svg";
file.contents = buffer;
next(null, file);
}
});
});
}