-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.js
104 lines (94 loc) · 2.54 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 { task, series, src, dest } = require('gulp');
const babel = require('gulp-babel');
const ts = require('gulp-typescript');
const del = require('del');
const less = require('gulp-less');
const through2 = require('through2');
//给组件中追加引入css
const pushCss = () => {
return through2.obj(function (file, encoding, next) {
this.push(file.clone());
if (file.path.match(/(\/|\\)src(\/|\\)([a-z]*-[a-z]*|[a-z]*)(\/|\\)index\.js/)) {
let content = file.contents.toString(encoding);
file.contents = Buffer.from((content += `\nrequire('./style/index.css');`));
}
this.push(file);
next();
});
};
//把流中的less后缀改为css
const less2css = () => {
return through2.obj(function (file, encoding, next) {
this.push(file.clone());
const content = file.contents.toString(encoding);
file.contents = Buffer.from(content.replace(/\.less/g, '.css'));
this.push(file);
next();
});
};
//清除编译制品
task('clean', async function () {
await del('lib/**');
await del('es/**');
await del('dist/**');
});
//生成lib包
task('cjs', function () {
const tsProject = ts.createProject('tsconfig.json', {
module: 'CommonJS',
});
return tsProject
.src()
.pipe(tsProject())
.pipe(
babel({
configFile: '../../.babelrc',
}),
)
.pipe(less2css())
.pipe(pushCss())
.pipe(dest('lib/'));
});
//生成es module
task('es', function () {
const tsProject = ts.createProject('tsconfig.json', {
module: 'ESNext',
});
return tsProject
.src()
.pipe(tsProject())
.pipe(
babel({
configFile: '../../.babelrc',
}),
)
.pipe(less2css())
.pipe(pushCss())
.pipe(dest('es/'));
});
//处理less样式文件
task('less', function () {
return src('src/**/*.less').pipe(less()).pipe(dest('es/')).pipe(dest('lib/'));
});
//拷贝json文件
task('json', function () {
return src('src/**/*.json').pipe(dest('es/')).pipe(dest('lib/'));
});
//处理ts声明
task('declaration', function () {
try {
const tsProject = ts.createProject('tsconfig.json', {
declaration: true,
// noEmitOnError: true,
// emitDeclarationOnly: true,
});
return tsProject.src().pipe(tsProject()).pipe(less2css()).pipe(pushCss()).pipe(dest('es/')).pipe(dest('lib/'));
} catch (error) {
console.log(error);
}
});
//拷贝readme
task('copyReadme', async function () {
await src('../../README.md').pipe(dest('../../packages/tantd'));
});
exports.default = series('clean', 'cjs', 'es', 'less', 'json', 'declaration', 'copyReadme');