-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
169 lines (150 loc) · 4.68 KB
/
gulpfile.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
'use strict';
const crypto = require('crypto')
const fs = require('fs')
const path = require('path')
const gulp = require('gulp')
const gutil = require('gulp-util')
const rename = require('gulp-rename')
const concat = require('gulp-concat')
const iconfont = require('gulp-iconfont')
const consolidate = require('gulp-consolidate')
const svgstore = require('gulp-svgstore')
const svgmin = require('gulp-svgmin')
const through = require('through2')
const glob = require('glob')
const bs = require('browser-sync').create()
const _ = require('lodash')
const svgs = 'assets/icons/**/*.svg'
const templates = 'templates/**/*.{js,css,html}'
const fontName = 'iconfont'
const className = 'iconfont'
const classPrefix = 'icon-'
const template = 'fontawesome-style'
const timestamp = Math.round(Date.now() / 1000)
const calcHash = function(files) {
let hash = crypto.createHash('md5')
files.forEach(function(file) {
hash.update(fs.readFileSync(file, 'utf8'))
})
return hash.digest('hex')
}
gulp.task('svgstore', () =>
gulp.src(svgs)
.pipe(svgmin(file => {
const prefix = path.basename(file.relative, path.extname(file.relative));
return {
plugins: [{
cleanupIDs: {
prefix: prefix + '-',
minify: true
}
}]
}
}))
.pipe(svgstore())
.pipe(pipeSvgSpriteData())
.pipe(rename({ basename: fontName }))
.pipe(gulp.dest('dist/icons/'))
)
gulp.task('build', gulp.series('svgstore', () =>
gulp.src(svgs)
.pipe(iconfont({
fontName,
formats: ['ttf', 'eot', 'woff', 'svg'],
normalize: true,
fontHeight: 10000,
timestamp,
log: () => {}
}))
.on('glyphs', function(glyphs) {
const options = {
className,
classPrefix,
fontName,
timestamp,
hash: calcHash(glob.sync(svgs)),
fontPath: '../fonts/',
glyphs: glyphs.map(mapGlyphs)
}
gulp.src([`templates/${template}-fontface.css`, `templates/${template}.css`])
.pipe(consolidate('lodash', options))
.pipe(concat(`${fontName}.css`))
.pipe(gulp.dest('dist/styles/'))
gulp.src('templates/demo.css')
.pipe(gulp.dest('dist/styles'))
gulp.src(`templates/${template}.html`)
.pipe(consolidate('lodash', options))
.pipe(rename({ basename: 'preview_fontclass' }))
.pipe(gulp.dest('dist/'))
gulp.src(`templates/${template}-symbol.html`)
.pipe(consolidate('lodash', options))
.pipe(rename({ basename: 'preview_symbol' }))
.pipe(gulp.dest('dist/'))
this.on('end', () => {
setTimeout(buildBase64font.bind(this, options), 50)
})
})
.pipe(gulp.dest('dist/fonts/'))
))
function buildBase64font(options) {
gulp.src('dist/fonts/*.woff')
.pipe(pipeFontBase64Data(options))
.pipe(rename({ basename: `${fontName}-base64` }))
.pipe(gulp.dest('dist/styles/'))
}
function pipeFontBase64Data(data) {
return through.obj(function(file, enc, callback) {
if (file.isNull()) {
this.push(file);
return callback();
}
if (file.isBuffer()) {
const base64 = file.contents.toString('base64');
const fontfaceBuffer = fs.readFileSync(`templates/${template}-fontface64.css`);
const cssBuffer = fs.readFileSync(`templates/${template}.css`);
const contents = Buffer.concat([fontfaceBuffer, cssBuffer], fontfaceBuffer.length + cssBuffer.length);
const compiled = _.template(contents.toString())(Object.assign({base64: base64}, data));
file.contents = Buffer.from(compiled);
file.path = gutil.replaceExtension(file.path, '.css');
return callback(null, file);
} else {
return callback();
}
});
}
function pipeSvgSpriteData() {
return through.obj(function(file, enc, callback) {
if (file.isNull()) {
this.push(file);
return callback();
}
if (file.isBuffer()) {
const svgSprite = file.contents.toString().replace(/<defs>[\s\S]+<\/defs>/, '');
const tmpJs = fs.readFileSync(`templates/${template}.js`).toString();
file.contents = Buffer.from(tmpJs.replace('${svgSprite}', svgSprite));
file.path = gutil.replaceExtension(file.path, '.js');
return callback(null, file);
} else {
return callback();
}
});
}
gulp.task(
'start',
gulp.series('build', () => {
bs.init({
files: 'dist/*.html',
server: 'dist/',
startPath: '/preview_fontclass.html',
middleware: cacheControl
})
gulp.watch([svgs, templates], gulp.series('build'))
})
)
function mapGlyphs (glyph) {
return { name: glyph.name, codepoint: glyph.unicode[0].charCodeAt(0) }
}
function cacheControl (req, res, next) {
res.setHeader('Cache-control', 'no-store')
next()
}