-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
119 lines (103 loc) · 3 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
const gulp = require('gulp');
const webpack = require('webpack');
const rimraf = require('rimraf');
const through2 = require('through2');
const merge2 = require('merge2');
const babel = require('gulp-babel');
const getWebpackConfig = require('./script/getWebpackConfig');
const getBabelCommonConfig = require('./script/getBabelCommonConfig');
const { getProjectPath, transformLess } = require('./script/utils');
const libDir = getProjectPath('lib');
const esDir = getProjectPath('es');
const distDir = getProjectPath('dist');
function compileDist(done) {
rimraf.sync(distDir);
const webpackConfig = getWebpackConfig();
webpack(webpackConfig, (err, stats) => {
if (err) {
console.error(err.stack || err);
if (err.details) {
console.error(err.details);
}
return;
}
const info = stats.toJson();
if (stats.hasErrors()) {
console.error('err', info.errors);
}
if (stats.hasWarnings()) {
console.warn('warn', info.warnings);
}
const buildInfo = stats.toString({
colors: true,
children: false,
chunks: false,
modules: false,
chunkModules: false,
hash: false,
version: false,
});
// show build info in terminal
console.log(buildInfo);
done(0);
});
}
function compileLess(dir) {
const less = gulp
.src(['components/**/*.less'])
.pipe(
through2.obj(function (file, encoding, next) {
this.push(file.clone());
if (file.path.match(/(\/|\\)style(\/|\\)index\.less$/)) {
transformLess(file.path)
.then((css) => {
file.contents = Buffer.from(css);
file.path = file.path.replace(/\.less$/, '.css');
this.push(file);
next();
})
.catch((e) => {
console.error(e);
});
} else {
next();
}
})
)
.pipe(gulp.dest(dir));
return less;
}
function babelify(modules, dir) {
const babelConfig = getBabelCommonConfig(modules);
delete babelConfig.cacheDirectory;
const source = [
'components/**/*.jsx',
'components/**/*.js',
'!components/**/__tests__/**',
];
const stream = gulp.src(source).pipe(babel(babelConfig));
return stream.pipe(gulp.dest(dir));
}
function compile(modules) {
const destDir = modules === false ? esDir : libDir;
rimraf.sync(destDir);
const less = compileLess(destDir);
const jsFilesStream = babelify(modules, destDir);
const assets = gulp
.src(['components/**/*.@(png|svg)'])
.pipe(gulp.dest(modules === false ? esDir : libDir));
return merge2([less, jsFilesStream, assets]);
}
gulp.task('compile-with-es', (done) => {
console.log('[Parallel] Compile to es...');
compile(false).on('finish', done);
});
gulp.task('compile-with-lib', (done) => {
console.log('[Parallel] Compile to js...');
compile().on('finish', done);
});
gulp.task('compile-with-dist', gulp.series(compileDist));
gulp.task(
'build',
gulp.series(gulp.parallel('compile-with-es', 'compile-with-lib'))
);