-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
125 lines (107 loc) · 3.19 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
const del = require('del');
const glob = require('glob');
const path = require('path');
const babel = require('gulp-babel');
const gulp = require('gulp');
const svgmin = require('gulp-svgmin');
const { svgr } = require('@svgr/core');
const gulpSvgr = require('@proscom/gulp-svgr');
const svgo = require('svgo');
const { Transform } = require('stream');
const { writeFile } = require('fs/promises');
const babelrc = require('./babel.config');
const svgoConfig = require('./svgo.config.js');
const SVG_DIR = './src/svg/';
const DIST_DIR = './lib';
const COMPONENTS_DIR = './src/components';
const MIN_DIR = './src/min';
function cleanDist(done) {
del.sync([DIST_DIR], { force: true });
done();
}
function cleanComponents(done) {
del.sync([COMPONENTS_DIR], { force: true });
done();
}
function cleanAll(done){
cleanDist(done)
cleanComponents(done)
del.sync([MIN_DIR], { force: true });
done()
}
// 构建库
function buildLib() {
return gulp
.src('src/svg/*.svg')
.pipe(svgoMini(svgoConfig))
.pipe(
gulpSvgr({
svgr: {
icon: true,
svgoConfig,
svgProps: {
fill: 'currentColor',
},
plugins: ['@svgr/plugin-svgo', '@svgr/plugin-jsx'],
},
createIndex: 'index.js',
prefix: false,
}),
)
.pipe(babel())
.pipe(gulp.dest(DIST_DIR));
}
// svgo
function testSvg() {
return gulp
.src('src/svg/*.svg')
.pipe(svgoMini(svgoConfig))
.pipe(
gulpSvgr({
svgr: {
icon: true,
svgoConfig,
svgProps: {
fill: 'currentColor',
},
plugins: ['@svgr/plugin-svgo', '@svgr/plugin-jsx'],
},
createIndex: 'index.js',
prefix: false,
}),
)
.pipe(gulp.dest(COMPONENTS_DIR));
}
function optimzeSvg() {
console.log(svgoConfig);
return gulp.src('src/svg/*.svg').pipe(svgmin(svgoConfig)).pipe(gulp.dest(COMPONENTS_DIR));
}
//
function svgoMini(options) {
const optionsFunction = typeof options === 'function';
const stream = new Transform({ objectMode: true });
stream._transform = (file, encoding, cb) => {
if (file.isStream()) {
return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
if (file.isBuffer()) {
const result = svgo.optimize(String(file.contents), {
path: file.path,
...options,
});
if (typeof result.data === 'string') {
file.contents = Buffer.from(result.data);
return cb(null, file);
}
throw result.error;
}
return cb(null, file);
};
return stream;
}
function customSvgo() {
return gulp.src('src/svg/*.svg').pipe(svgoMini(svgoConfig)).pipe(gulp.dest(MIN_DIR));
}
exports.build = gulp.series(cleanAll, buildLib);
exports.svgo = gulp.series(cleanComponents, testSvg);
exports.min = gulp.series(customSvgo);