-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgulpfile.babel.js
More file actions
98 lines (88 loc) · 2.63 KB
/
Copy pathgulpfile.babel.js
File metadata and controls
98 lines (88 loc) · 2.63 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
import gulp from 'gulp';
import path from 'path';
import gulpLoadPlugins from 'gulp-load-plugins';
import { headerCase } from 'header-case';
import { lowerCase } from 'lower-case';
import { pascalCase } from 'pascal-case';
const $ = gulpLoadPlugins({});
const PREFIX = 'Icon';
const CLASS_NAME = 'rmi';
const DIST_FOLDER = 'dist';
const LIB_FOLDER = 'lib';
const SRC_FOLDER = 'node_modules/@mdi/svg/svg';
gulp.task('svg', () =>
gulp
.src(`${SRC_FOLDER}/**/*.svg`)
.pipe(
$.svgmin(() => ({
plugins: [
'removeDoctype',
'removeTitle',
'removeStyleElement',
'removeEmptyContainers',
'sortAttrs',
'removeUselessDefs',
'removeEmptyText',
'removeEditorsNSData',
'removeEmptyAttrs',
'removeHiddenElems',
'collapseGroups',
{
name: 'removeAttrs',
params: { attrs: ['id', 'class', 'data-name', 'fill', 'xmlns'] },
},
{
name: 'addAttributesToSVGElement',
params: {
attributes: ['classNameString', { viewBox: '0 0 24 24' }],
},
},
],
}))
)
.pipe(
$.insert.transform((content, file) => {
const name = pascalCase(path.basename(file.relative, path.extname(file.relative)));
return `
import React from 'react';
export default function ${name}${PREFIX}(props) {
return (
${content}
);
}
`;
})
)
.pipe(
$.rename((file) => {
file.basename = `${pascalCase(file.basename)}`;
file.extname = '.js';
})
)
.pipe(gulp.dest(DIST_FOLDER))
);
gulp.task('replace', () =>
gulp.src(`${DIST_FOLDER}/*.js`).pipe(
$.tap((file) => {
const fileName = path.basename(file.path);
const className = lowerCase(headerCase(fileName.replace('.js', '')));
return gulp
.src(`${DIST_FOLDER}/${fileName}`)
.pipe(
$.replace(
'classNameString',
`{...props} className={\`${CLASS_NAME} ${CLASS_NAME}-${className} \${props.className\}\`}`
)
)
.pipe($.replace(/xmlns:xlink=".+?"/g, ``))
.pipe($.replace(/xlink:href=".+?"/g, ``))
.pipe($.replace('fill-rule=', 'fillRule='))
.pipe($.replace('fill-opacity=', 'fillOpacity='))
.pipe($.prettier())
.pipe(gulp.dest(DIST_FOLDER))
.pipe(gulp.dest(LIB_FOLDER));
})
)
);
gulp.task('copySvg', () => gulp.src(`${SRC_FOLDER}/**/*.svg`).pipe(gulp.dest('./svg')));
gulp.task('default', gulp.series('svg', 'replace', 'copySvg'));