This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
gulpfile.ts
223 lines (181 loc) · 5.41 KB
/
gulpfile.ts
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* @file gulpfile 编译文件
* @author Yu Zong([email protected])
*/
import path from 'path';
import del from 'del';
import glob from 'glob';
import gulp from 'gulp';
import babel from 'gulp-babel';
import ts from 'gulp-typescript';
import isPathCwd from 'is-path-cwd';
import isPathInCwd from 'is-path-in-cwd';
import merge from 'merge2';
import minimist from 'minimist';
import config from './tsconfig.json';
function resolve(p: string) {
return path.resolve(process.cwd(), p);
}
const params = minimist(process.argv.slice(2));
const files = [
'packages/*/*/src/**/*.ts',
'packages/*/*/src/**/*.tsx',
'!packages/**/__tests__/**'
];
gulp.task('build-ts', () => {
const realConfig = Object.assign({}, config.compilerOptions, {
module: 'esnext',
target: 'es2018',
rootDir: resolve('.'),
outDir: resolve('dist/ts'),
declarationDir: resolve('dist/ts')
});
const result = gulp.src(files).pipe(ts(realConfig));
return merge([
result.js.pipe(gulp.dest('dist/ts')),
result.dts.pipe(gulp.dest('dist/js')).pipe(gulp.dest('dist/es'))
]);
});
gulp.task('build-ts-dev', () => {
const realConfig = Object.assign({}, config.compilerOptions, {
module: 'esnext',
target: 'es5',
rootDir: resolve('.'),
outDir: resolve('dist/ts'),
declarationDir: resolve('dist/ts')
});
const result = gulp.src(files).pipe(ts(realConfig));
return merge([
result.js.pipe(gulp.dest('dist/es')).pipe(gulp.dest('dist/js')),
result.dts.pipe(gulp.dest('dist/js')).pipe(gulp.dest('dist/es'))
]);
});
gulp.task('build-js', () => {
// JSX只翻译成React就行了,Vue Hooks不需要写任何模板文件
let stream = gulp
.src('dist/ts/**')
.pipe(babel({
presets: [
[
'@babel/preset-env',
{
modules: 'commonjs',
targets: {
browsers: [
'> 1%',
'last 2 versions',
'not ie <= 8'
]
}
}
],
'@babel/preset-react'
],
plugins: [
[
'@babel/plugin-proposal-class-properties',
{
loose: false
}
]
]
}));
return stream.pipe(gulp.dest('dist/js'));
});
gulp.task('build-es', () => {
let stream = gulp
.src('dist/ts/**')
.pipe(babel({
presets: [
[
'@babel/preset-env',
{
modules: false,
targets: {
browsers: [
'> 1%',
'last 2 versions',
'not ie <= 8'
]
}
}
],
'@babel/preset-react'
],
plugins: [
[
'@babel/plugin-proposal-class-properties',
{
loose: false
}
]
]
}));
return stream.pipe(gulp.dest('dist/es'));
});
gulp.task('del-dist', () => {
return del(['dist']);
});
function createCopyTask(type: string, name: string) {
const dev: boolean = !!params.dev;
const target: string | void = params.target;
let p: string;
if (dev) {
if (typeof target !== 'string' || target === '') {
throw new Error('dev mode need target option');
}
p = resolve(target);
if (isPathCwd(p) || isPathInCwd(p)) {
throw new Error('dev mode need target not in current work dir');
}
} else {
p = resolve('packages');
}
gulp.task('tsc-' + type + '-' + name, () => {
const fromPath = type + '/' + name;
const toPath = (dev ? '@' : '') + fromPath;
const js = gulp
.src(['dist/js/' + fromPath + '/src/**'])
.pipe(gulp.dest(p + '/' + toPath + '/lib'));
const es = gulp
.src(['dist/es/' + fromPath + '/src/**'])
.pipe(gulp.dest(p + '/' + toPath + '/es'));
if (!dev) {
return merge([js, es]);
}
const other = gulp
.src(['packages/' + fromPath + '/package.json'])
.pipe(gulp.dest(p + '/' + toPath));
return merge([js, es, other]);
});
return 'tsc-' + type + '-' + name;
}
const tasks = glob.sync('packages/*/*').map(name => {
const keys = name.split('/');
const type = keys[keys.length - 2];
if (type === params.ignore) {
return '';
}
return createCopyTask(type, keys[keys.length - 1]);
}).filter(item => item);
if (params.dev) {
gulp.task('default', gulp.series(
'del-dist',
'build-ts-dev',
...tasks,
'del-dist'
));
} else {
gulp.task('default', gulp.series(
'del-dist',
'build-ts',
'build-es',
'build-js',
...tasks,
'del-dist'
));
}
gulp.task('watch-ts', () => {
gulp.watch(files, gulp.series('default'));
});
gulp.task('watch', gulp.series('default', 'watch-ts'));