forked from ThadeuLuz/mui-icons
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.babel.js
219 lines (184 loc) · 6.63 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
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
/* eslint-disable no-param-reassign */
import gulp from 'gulp';
import svgscaler from 'svg-scaler';
import rename from 'gulp-rename';
import del from 'del';
import svgo from 'gulp-svgo';
import concatFilenames from 'gulp-concat-filenames';
import _, { upperFirst, camelCase, template, includes } from 'lodash';
import transform from 'gulp-transform';
import babel from 'gulp-babel';
const babelConfig = { presets: ['latest', 'stage-0', 'react'] };
// Source svgs
const globs = {
mdi: './node_modules/material-design-icons/*/svg/production/*_24px.svg',
evilicons: './node_modules/evil-icons/assets/icons/*.svg',
octicons: './node_modules/octicons/build/svg/*.svg',
ionicons: './node_modules/ionicons/dist/svg/*.svg',
// Couldn't find them on npm, copied them to src/icons
typicons: './src/icons/typicons/*.svg',
cmdi: './src/icons/cmdi/*.svg',
fontawesome: './src/icons/fontawesome/*.svg',
};
const compiled = template(
`import React from 'react';
import SvgIcon from 'material-ui/SvgIcon';
const Icon = props => (
<SvgIcon {...props}>
<g><%= content %></g>
</SvgIcon>
);
export default Icon;
`);
// The glob on mdi returns files with the same name. Exporting both causes an error on react.
const exported = [];
const concatConfig = c => ({
root: `./${c}/`,
template: (f) => {
const moduleName = upperFirst(camelCase(`${c}-${f.replace('.js', '')}`));
if (exported.includes(moduleName)) return `// export ${moduleName} from './${f}'`;
exported.push(moduleName);
return `export ${moduleName} from './${f}'`;
},
});
const fix = (opt = {}) => (_content) => {
let content = _content;
// Remove xml
content = _.replace(content, '<?xml version="1.0"?>', '');
// Remove svg tags
content = _.replace(content, /<svg .*?>/, '');
content = _.replace(content, '</svg>', '');
// Remove style tags and their content
content = _.replace(content, /<style.*?\/style>/, '');
// Remove fill
content = _.replace(content, /fill=".*?"/, '');
content = _.replace(content, /fill-opacity=".*?"/, '');
content = _.replace(content, /fill-rule=".*?"/, '');
// Remove namespace atts
content = _.replace(content, /xlink:href=".*?"/, '');
// Evilicons and Ionicons can't be scaled with svgscaler. We have to use css.
if (opt.scale) {
// We are in react, remember? So style={{...}}
content = `<g style={{ transform: 'scale(${opt.scale})'}}>${content}</g>`;
}
if (opt.translate) {
content = `<g style={{ transform: 'translate(${opt.translate})'}}>${content}</g>`;
}
return compiled({ content });
};
// Logs errors on common problems
const check = (content, v) => {
if (!includes(content, 'export') ||
!includes(content, '</g>') ||
includes(content, 'NaN') ||
includes(content, 'xml version')) {
console.error('Broken:', v.path); // eslint-disable-line no-console
}
return content;
};
gulp.task('cmdi', ['clean'], () => gulp.src(globs.cmdi)
.pipe(svgscaler({ width: 24 }))
.pipe(svgo())
.pipe(transform(fix(), { encoding: 'utf8' }))
.pipe(rename({ dirname: '/', extname: '.js' }))
.pipe(transform(check, { encoding: 'utf8' }))
.pipe(babel(babelConfig))
.pipe(gulp.dest('./cmdi/'))
.pipe(concatFilenames('index.js', concatConfig('cmdi')))
.pipe(babel(babelConfig))
.pipe(gulp.dest('./cmdi/')));
gulp.task('evilicons', ['clean'], () => gulp.src(globs.evilicons)
// .pipe(svgscaler({ width: 24 })) // This breaks on evil icons! throws a bunch of NaN
.pipe(svgo())
// This is hacky, but we have to apply a manual transform/scale
.pipe(transform(fix({ scale: '0.6', translate: '-3px,-3px' }), { encoding: 'utf8' }))
.pipe(rename((path) => {
path.dirname = '/';
path.basename = path.basename.replace('ei-', '');
path.extname = '.js';
}))
.pipe(transform(check, { encoding: 'utf8' }))
.pipe(babel(babelConfig))
.pipe(gulp.dest('./evilicons/'))
.pipe(concatFilenames('index.js', concatConfig('evilicons')))
.pipe(babel(babelConfig))
.pipe(gulp.dest('./evilicons/')));
gulp.task('fontawesome', ['clean'], () => gulp.src(globs.fontawesome)
.pipe(svgscaler({ width: 24 }))
.pipe(svgo())
.pipe(transform(fix(), { encoding: 'utf8' }))
.pipe(rename({ dirname: '/', extname: '.js' }))
.pipe(transform(check, { encoding: 'utf8' }))
.pipe(babel(babelConfig))
.pipe(gulp.dest('./fontawesome/'))
.pipe(concatFilenames('index.js', concatConfig('fontawesome')))
.pipe(babel(babelConfig))
.pipe(gulp.dest('./fontawesome/')));
gulp.task('ionicons', ['clean'], () => gulp.src(globs.ionicons)
// .pipe(svgscaler({ width: 24 })) // Breaks everything. Throws a bunch of NaN
.pipe(svgo())
// This is hacky, but we have to apply css scale
.pipe(transform(fix({ scale: '0.045' }), { encoding: 'utf8' }))
.pipe(rename({ dirname: '/', extname: '.js' }))
.pipe(transform(check, { encoding: 'utf8' }))
.pipe(babel(babelConfig))
.pipe(gulp.dest('./ionicons/'))
.pipe(concatFilenames('index.js', concatConfig('ionicons')))
.pipe(babel(babelConfig))
.pipe(gulp.dest('./ionicons/')));
gulp.task('mdi', ['clean'], () => gulp.src(globs.mdi)
.pipe(svgscaler({ width: 24 }))
.pipe(svgo())
.pipe(transform(fix(), { encoding: 'utf8' }))
.pipe(rename((path) => {
path.dirname = '/';
path.basename = path.basename.replace('_24px', '').replace('ic_', '');
path.extname = '.js';
}))
.pipe(transform(check, { encoding: 'utf8' }))
.pipe(babel(babelConfig))
.pipe(gulp.dest('./mdi/'))
.pipe(concatFilenames('index.js', concatConfig('mdi')))
.pipe(babel(babelConfig))
.pipe(gulp.dest('./mdi/')));
gulp.task('octicons', ['clean'], () => gulp.src(globs.octicons)
.pipe(svgscaler({ width: 24 }))
.pipe(svgo())
.pipe(transform(fix(), { encoding: 'utf8' }))
.pipe(rename({ dirname: '/', extname: '.js' }))
.pipe(transform(check, { encoding: 'utf8' }))
.pipe(babel(babelConfig))
.pipe(gulp.dest('./octicons/'))
.pipe(concatFilenames('index.js', concatConfig('octicons')))
.pipe(babel(babelConfig))
.pipe(gulp.dest('./octicons/')));
gulp.task('typicons', ['clean'], () => gulp.src(globs.typicons)
.pipe(svgscaler({ width: 24 }))
.pipe(svgo())
.pipe(transform(fix(), { encoding: 'utf8' }))
.pipe(rename({ dirname: '/', extname: '.js' }))
.pipe(transform(check, { encoding: 'utf8' }))
.pipe(babel(babelConfig))
.pipe(gulp.dest('./typicons/'))
.pipe(concatFilenames('index.js', concatConfig('typicons')))
.pipe(babel(babelConfig))
.pipe(gulp.dest('./typicons/')));
gulp.task('build', [
'cmdi',
'evilicons',
'fontawesome',
'ionicons',
'mdi',
'octicons',
'typicons',
]);
gulp.task('clean', () => del([
'./cmdi/',
'./evilicons/',
'./fontawesome/',
'./ionicons/',
'./mdi/',
'./octicons/',
'./typicons/',
]));
gulp.task('default', ['build']);