-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
127 lines (113 loc) · 3.64 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
126
127
/* eslint-disable func-names */
/* eslint-disable no-param-reassign */
const rimraf = require("rimraf");
const gulp = require("gulp");
const babel = require("gulp-babel");
const ts = require("gulp-typescript");
const merge2 = require("merge2");
const through2 = require("through2");
const minimist = require("minimist");
const { cssInjection } = require("./config/styleUtil");
const transformLess = require("./config/transformLess");
const getBabelCommonConfig = require("./config/getBabelCommonConfig");
const getTSCommonConfig = require("./config/getTSCommonConfig");
const tsConfig = getTSCommonConfig();
const tsDefaultReporter = ts.reporter.defaultReporter();
const argv = minimist(process.argv.slice(2));
const libDir = "lib";
const esDir = "es";
const compile = modules => {
rimraf.sync(modules !== false ? libDir : esDir);
const less = gulp
.src(["src/**/*.less"])
.pipe(
through2.obj(function(file, encoding, next) {
// 将 less 文件转成 css 文件
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(modules === false ? esDir : libDir));
//
const source = [
"src/**/*.tsx",
"src/**/*.ts",
"src/**/*.d.ts",
"!src/**/*.test.js",
"!**/__test__/**"
];
if (tsConfig.allowJs) {
source.unshift("src/**/*.jsx", "src/**/*.js");
}
let error = 0;
function check() {
if (error && !argv["ignore-error"]) {
process.exit(1);
}
}
const tsResult = gulp.src(source).pipe(
ts(tsConfig, {
error(e) {
tsDefaultReporter.error(e);
error = 1;
},
finish: tsDefaultReporter.finish
})
);
tsResult.on("finish", check);
tsResult.on("end", check);
const js = tsResult.js
.pipe(babel({ babelrc: false, ...getBabelCommonConfig(modules) }))
.pipe(
// 将 style 下的 index.js 文件转成 css.js
through2.obj(function z(file, encoding, next) {
this.push(file.clone());
if (file.path.match(/(\/|\\)style(\/|\\)index\.js/)) {
const content = file.contents.toString(encoding);
if (content.indexOf("'react-native'") !== -1) {
// actually in [email protected], this case will never run,
// since we both split style/index.mative.js style/index.js
// but let us keep this check at here
// in case some of our developer made a file name mistake ==
next();
return;
}
file.contents = Buffer.from(cssInjection(content));
file.path = file.path.replace(/index\.js/, "css.js");
this.push(file);
next();
} else {
next();
}
})
)
.pipe(gulp.dest(modules === false ? esDir : libDir));
const dts = tsResult.dts.pipe(gulp.dest(modules === false ? esDir : libDir));
return merge2([less, js, dts]);
};
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("watch", function() {
// 实时监听
gulp.watch("src/**/*.*", gulp.series("compile-with-es", "compile-with-lib"));
});
gulp.task("default", gulp.series("compile-with-es", "compile-with-lib"));