-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
104 lines (92 loc) · 3.13 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
const path = require('path');
const gulp = require('gulp');
const clean = require('gulp-clean');
const sourcemaps = require('gulp-sourcemaps');
const babel = require('gulp-babel');
const less = require('gulp-less');
const bump = require('gulp-bump');
const LessPluginAutoPrefix = require('less-plugin-autoprefix');
const NpmImportPlugin = require('less-plugin-npm-import');
const merge2 = require('merge2');
const through2 = require('through2');
const babelConfig = {
presets: [['@babel/preset-env'], '@babel/preset-react'],
plugins: [
'@babel/plugin-proposal-class-properties',
['@babel/plugin-transform-runtime', { helpers: false }],
],
};
const source = [
'./components/**/*.js',
'./components/**/*.jsx',
'!./components/**/demo',
'!./components/**/demo/**',
];
const libDir = path.join(process.cwd(), 'lib');
gulp.task('clean', function() {
return gulp.src(libDir, { allowEmpty: true }).pipe(clean());
});
gulp.task('image', function() {
return gulp.src('./components/**/assets/**').pipe(gulp.dest(libDir));
});
gulp.task('js', function() {
const jsFilesStream = babelify(gulp.src(source));
return jsFilesStream;
});
function babelify(js) {
let stream = js
.pipe(sourcemaps.init())
.pipe(babel(babelConfig))
.pipe(
through2.obj(function(file, encoding, next) {
this.push(file.clone());
if (file.path.match(/(\/|\\)style(\/|\\)index\.js/)) {
const content = file.contents.toString(encoding);
file.contents = Buffer.from(
content
.replace(/\/style\/?'/g, "/style/css'")
.replace(/\/style\/?"/g, '/style/css"')
.replace(/\.less/g, '.css'),
);
file.path = file.path.replace(/index\.js/, 'css.js');
this.push(file);
}
next();
}),
)
.pipe(sourcemaps.write('.'));
return stream.pipe(gulp.dest(libDir));
}
gulp.task('less', function() {
const lessStream = gulp.src('./components/**/*.less');
const copyLess = lessStream
.pipe(
through2.obj(function(file, encoding, next) {
this.push(file.clone());
next();
}),
)
.pipe(gulp.dest(libDir));
const lessToCss = lessStream
.pipe(
less({
javascriptEnabled: true,
plugins: [
new NpmImportPlugin({ prefix: '~' }),
new LessPluginAutoPrefix({ browsers: ['last 2 versions', 'IE 9'] }),
],
modifyVars: {
'root-entry-name': 'default',
},
}),
)
.pipe(gulp.dest(libDir));
return merge2([copyLess, lessToCss]);
});
gulp.task('compile', gulp.series(['clean', 'image', 'js', 'less']));
gulp.task('bump', function() {
return gulp
.src('./package.json')
.pipe(bump({ type: process.argv.slice(3)[0].replace(/-/g, '') }))
.pipe(gulp.dest('./'));
});