forked from trello-archive/iconathon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.coffee
More file actions
253 lines (223 loc) · 6.95 KB
/
gulpfile.coffee
File metadata and controls
253 lines (223 loc) · 6.95 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
gulp = require 'gulp'
gutil = require 'gulp-util'
rename = require 'gulp-rename'
sketch = require 'gulp-sketch'
svgmin = require 'gulp-svgmin'
cheerio = require 'gulp-cheerio'
convert = require 'gulp-rsvg'
iconfont = require 'gulp-iconfont'
shell = require 'gulp-shell'
consolidate = require 'gulp-consolidate'
jsoneditor = require 'gulp-json-editor'
jsonminify = require 'gulp-jsonminify'
less = require 'gulp-less'
LessPluginAutoPrefix = require 'less-plugin-autoprefix'
minifyCSS = require 'gulp-minify-css'
minifyHTML = require 'gulp-minify-html'
fs = require 'fs'
path = require 'path'
_ = require 'lodash'
# https://github.com/svg/svgo/tree/master/plugins
# We use these plugins for all our svg optimizing.
svgoPluginOpts = [
{ removeViewBox: false }
{ removeDesc: true }
{ removeTitle: true }
{ removeRasterImages: true }
{ cleanupNumericValues: false }
]
# This is the distance between the edge of the glyph and the edge of the file.
# For instance, for 16pt18box, the glyph will be 16x16 points, and have a
# 1 point margin on all sides.
# If you want to add more sizes, you can follow the same format, but be sure to
# add to the tasks at the bottom.
sizes = {
'16pt18box': { size: 16, box: 18 }
'20pt24box': { size: 20, box: 24 }
'30pt32box': { size: 30, box: 32 }
}
# This is the thickness of the line in the icon.
weights = [
'100'
'500'
]
getUnits = (size, box) ->
# Hard dependancy on 1920 x 1920 SVGs…
boxDelta = box - size
boundingUnits = ((1920 / size) * boxDelta)
viewBoxValue = 1920 + boundingUnits
viewBox = "0 0 #{viewBoxValue} #{viewBoxValue}"
translateDiff = boundingUnits / 2
translate = "translate(#{translateDiff} #{translateDiff})"
# Points don't make sense for web screens, but these outputs are primarily
# for android.
box = "#{box}pt"
return { box, viewBox, translate }
# Export from Sketch. This will export all the weights, so long as they are
# artboards.
gulp.task 'sketch', ->
gulp
.src ['./src/sketch/*.sketch']
.pipe sketch
export: 'artboards'
formats: 'svg'
.pipe gulp.dest './build/exports/'
# Tasks for individual sizes.
gulpSizeTask = (weight, size, box, viewBox, translate) ->
key = "#{weight}-#{size}"
gulp.task key, ['sketch'], ->
gulp
.src ["./build/exports/#{weight}/*.svg"]
.pipe cheerio({
run: ($, file, done) ->
$('svg')
.attr({ 'height': box, 'width': box })
.attr({ 'viewBox': viewBox })
$('svg > g').attr({ 'transform': translate })
done()
# SVG is XML so this turns out to be pretty important.
# The output is mangled without it.
parserOptions: {
xmlMode: true
}
})
.pipe svgmin
plugins: svgoPluginOpts
.pipe rename
prefix: 'ic_'
suffix: "_#{weight}_#{size}"
# Sure, you could use these SVGs for any platform, but we have Android in
# mind here.
.pipe gulp.dest("./build/weights/#{weight}/#{size}/android")
# iOS uses PDFs.
.pipe cheerio({
# For iOS PDFs, use a pixel value. It will get converted to pixels
# anyway.
run: ($, file, done) ->
pxBox = box.replace("pt", "px")
$('svg')
.attr({ 'height': pxBox, 'width': pxBox })
done()
parserOptions: {
xmlMode: true
}
})
.pipe convert({format: 'pdf'})
.pipe gulp.dest("./build/weights/#{weight}/#{size}/ios")
# For each weight, export for various sizes.
for weight in weights
do (weight) ->
paths = {
sketchFiles: ["./src/sketch/#{weight}/*.sketch"]
exportSvgs: ["./build/exports/#{weight}/*.svg"]
}
# Now, the various sizes…
for size, value of sizes
# getUnits does all the math for the defined size and bounding box, then
# adds a gulp task for each.
units = getUnits(value.size, value.box)
gulpSizeTask(weight, size, units.box, units.viewBox, units.translate)
# We also want the full SVGs without modified height, width, or margin.
gulp.task "#{weight}-full", ["sketch"], ->
gulp
.src paths.exportSvgs
.pipe svgmin
plugins: svgoPluginOpts
.pipe gulp.dest("./build/weights/#{weight}/full/")
# Build the icon font. We mainly use this for rudimentary testing and use
# Icomoon for production.
gulp.task "#{weight}-font", ["sketch"], ->
gulp
.src paths.exportSvgs
.pipe svgmin
plugins: svgoPluginOpts
.pipe iconfont
fontName: "trellicons-#{weight}"
fixedWidth: true
centerHorizontally: true
.on 'codepoints', (codepoints, options) ->
# Outputs a CSS file with the right characters.
templateData = {
glyphs: codepoints
className: 'icon'
weight: weight
fontName: options.fontName
}
gulp
.src './src/demo/templates/icon-css-points.tmpl'
.pipe consolidate 'lodash', templateData
.pipe rename 'icon-points.less'
.pipe gulp.dest "./build/weights/#{weight}/fonts"
.pipe gulp.dest "./build/weights/#{weight}/fonts"
gulp.task 'demo', ['500-font'], ->
# we need the font task to be finished before building the CSS.
autoprefix = new LessPluginAutoPrefix
browsers: [ "last 3 Chrome versions", "last 3 Firefox versions" ]
# Styles
gulp
.src './src/demo/styles/entry/app.less',
base: path.join(__dirname, '/src/demo/styles/')
.pipe less
paths: [ path.join(__dirname, '/src/demo/styles/'), './build/' ]
plugins: [ autoprefix ]
.on 'error', (err) ->
gutil.log(err)
this.emit('end')
.pipe rename 'app.css'
.pipe gulp.dest './build/demo/'
.pipe minifyCSS()
.pipe rename 'app.min.css'
.pipe gulp.dest './build/demo/'
# HTML
gulp
.src './src/demo/templates/demo.html'
.pipe minifyHTML()
.pipe gulp.dest './build/demo/'
# Read the Sketch file names to get data for the demo.
fs.readdir "./src/sketch/", (err, files) ->
# Get the icon names.
iconNames = []
for icon in files
iconNames.push icon.replace(/\.[^/.]+$/, "")
iconNames = _.compact iconNames
# Get the sizes.
sizeData = []
for key, value of sizes
sizeData.push key
gulp
.src "./src/demo/templates/data.json"
.pipe jsoneditor
icons: iconNames
sizes: sizeData
weights: weights
.pipe jsonminify()
.pipe gulp.dest './build/demo'
gulp.task 'watch', ->
gulp.watch './src/demo/**/*', ['demo']
gulp.watch './src/sketch/**/*.sketch', [
'100-16pt18box',
'100-20pt24box',
'100-30pt32box',
'100-full',
'100-font',
'500-16pt18box',
'500-20pt24box',
'500-30pt32box',
'500-full',
'500-font',
'demo'
]
gulp.task 'default', [
'watch',
'100-16pt18box',
'100-20pt24box',
'100-30pt32box',
'100-full',
'100-font',
'500-16pt18box',
'500-20pt24box',
'500-30pt32box',
'500-full',
'500-font',
'demo'
]