-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
155 lines (135 loc) · 3.41 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
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
const fs = require("fs");
const path = require("path");
const eventStream = require("event-stream");
const gulp = require("gulp");
const gutil = require("gulp-util");
const argv = require("yargs").argv;
const shell = require("gulp-shell");
const watch = require("gulp-watch");
const runSequence = require("run-sequence");
function handleError(err) {
console.log(err);
}
const platformConfigs = {
web: {
extensions: [".web.tsx", ".web.ts", ".tsx", ".ts"]
},
ios: {
extensions: [
".ios.tsx",
".ios.ts",
".native.tsx",
".native.ts",
".tsx",
".ts"
]
},
android: {
extensions: [
".android.tsx",
".android.ts",
".native.tsx",
".native.ts",
".tsx",
".ts"
]
}
};
function getPlatform() {
const platform = argv.platform || "web";
if (!Object.keys(platformConfigs).includes(platform)) {
throw `Unsupported platform: ${platform}`;
}
return platform;
}
// Handle args
const platform = getPlatform();
gutil.log(gutil.colors.yellow(`platform: ${platform}`));
const paths = {
src: path.resolve(__dirname, "src"),
dist: path.resolve(__dirname, "dist"),
obj: path.resolve(__dirname, "obj")
};
const getObjPath = () => {
return path.resolve(paths.obj, platform);
};
const srcGlob = path.resolve(paths.src, "**/*");
function copyFileForPlatform(origFile) {
const file = origFile.clone({ contents: false });
if (file.isDirectory()) {
return false;
}
// Check the file is exactly for the platform
const { extensions } = platformConfigs[platform];
const fileName = path.basename(file.path);
const [baseName, ...extNames] = fileName.split(".");
const extName = "." + extNames.join(".");
if (extName === ".d.ts") {
file.path = origFile.path.replace(paths.src, getObjPath());
return file;
}
const extIndex = extensions.indexOf(extName);
if (extIndex < 0) {
return false;
}
const notExactly = extensions
.slice(0, extIndex)
.map(ext => origFile.path.replace(new RegExp(`${extName}$`), ext))
.find(path => fs.existsSync(path));
if (notExactly) {
return false;
}
file.path = origFile.path
.replace(paths.src, getObjPath())
.replace(
new RegExp(`${fileName}$`),
`${baseName}.${extNames[extNames.length - 1]}`
);
// Map to tmp path for each platform
return file;
}
function platformify() {
return eventStream.map((origFile, cb) => {
// console.log(file.path);
const file = copyFileForPlatform(origFile);
if (!file) {
return cb();
}
cb(null, file);
});
}
gulp.task("copy", () => {
return gulp
.src(srcGlob)
.pipe(platformify())
.pipe(gulp.dest(paths.dist))
.on("error", handleError);
});
gulp.task("watch-copy", () => {
return watch(srcGlob)
.pipe(platformify())
.pipe(gulp.dest(paths.dist))
.on("error", handleError);
});
gulp.task(
"tsc-check",
shell.task(`node_modules/.bin/tsc -p tsconfig.${platform}.json`)
);
gulp.task(
"tsc-watch",
shell.task(`node_modules/.bin/tsc -p tsconfig.${platform}.json --watch`)
);
gulp.task(
"webpack-build",
shell.task(`node_modules/.bin/webpack --env.platform=${platform}`)
);
gulp.task("build", cb =>
runSequence(["copy", "tsc-check", "webpack-build"], cb)
);
gulp.task(
"webpack-watch",
shell.task(`node_modules/.bin/webpack --watch --env.platform=${platform}`)
);
gulp.task("watch", cb =>
runSequence("copy", ["watch-copy", "tsc-watch", "webpack-watch"], cb)
);