forked from google/material-design-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
executable file
·166 lines (145 loc) · 3.81 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
import _ from 'lodash';
import File from 'vinyl';
import gulp from 'gulp';
import merge from 'merge-stream';
import sprity from 'sprity';
import svgSprite from 'gulp-svg-sprite';
import through2 from 'through2';
import { humanize, titleize } from 'underscore.string';
/** Names of directories containing icons. */
const ICON_CATEGORIES = [
'action',
'alert',
'av',
'communication',
'content',
'device',
'editor',
'file',
'hardware',
'image',
'maps',
'navigation',
'notification',
'places',
'social',
'toggle',
];
/** Standard PNG colors. */
const PNG_COLORS = [
'black',
'white',
];
/**
* Generates PNG sprites and their corresponding CSS files for each category of
* icon, and places them in `sprites/css-sprite`.
*
* TODO(shyndman): Add support for double density sprites.
*/
gulp.task('png-sprites', () =>
_(getCategoryColorPairs())
.map(([ category, color ]) =>
sprity.src({
src: `./${ category }/1x_web/*_${ color }_24dp.png`,
style: `sprite-${ category }-${ color }.css`,
name: `sprite-${ category }-${ color }`,
engine: 'sprity-gm',
orientation: 'left-right'
}))
.thru(merge)
.value()
.pipe(gulp.dest('./sprites/css-sprite/')));
/**
* Generates CSS and Symbol-based SVG sprites for each category, and places
* them in `sprites/svg-sprite`.
*/
gulp.task('svg-sprites', () =>
_(ICON_CATEGORIES)
.map((category) =>
gulp.src(`./${ category }/svg/production/*_24px.svg`)
.pipe(svgSprite(getSvgSpriteConfig(category))))
.thru(merge)
.value()
.pipe(gulp.dest('./sprites/svg-sprite')));
/**
* Generates a file to allow the consumption of the icon font by Iconjar
* (http://geticonjar.com/).
*/
gulp.task('iconjar', () =>
gulp.src('./iconfont/codepoints')
.pipe(generateIjmap('MaterialIcons-Regular.ijmap'))
.pipe(gulp.dest('./iconfont/')));
/** Runs all tasks. */
gulp.task('default', ['png-sprites', 'svg-sprites', 'iconjar']);
/**
* Returns a stream that transforms between our icon font's codepoint file
* and an Iconjar ijmap.
*/
function generateIjmap(ijmapPath) {
return through2.obj((codepointsFile, encoding, callback) => {
const ijmap = {
icons: codepointsToIjmap(codepointsFile.contents.toString())
};
callback(null, new File({
path: ijmapPath,
contents: new Buffer(JSON.stringify(ijmap), 'utf8')
}));
function codepointsToIjmap(codepoints) {
return _(codepoints)
.split('\n') // split into lines
.reject(_.isEmpty) // remove empty lines
.reduce((codepointMap, line) => { // build up the codepoint map
let [ name, codepoint ] = line.split(' ');
codepointMap[codepoint] = { name: titleize(humanize(name)) };
return codepointMap;
}, {});
}
});
}
/**
* Returns the SVG sprite configuration for the specified category.
*/
function getSvgSpriteConfig(category) {
return {
shape: {
dimension: {
maxWidth: 24,
maxHeight: 24
},
},
mode: {
css : {
bust: false,
dest: './',
sprite: `./svg-sprite-${ category }.svg`,
example: {
dest: `./svg-sprite-${ category }.html`
},
render: {
css: {
dest: `./svg-sprite-${ category }.css`
}
}
},
symbol : {
bust: false,
dest: './',
sprite: `./svg-sprite-${ category }-symbol.svg`,
example: {
dest: `./svg-sprite-${ category }-symbol.html`
}
}
}
};
}
/**
* Returns the catesian product of categories and colors.
*/
function getCategoryColorPairs() {
return _(ICON_CATEGORIES)
.map((category) =>
_.zip(_.times(PNG_COLORS.length, () => category), PNG_COLORS))
.flatten() // flattens 1 level
.value();
}