-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.ts
104 lines (93 loc) · 2.3 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
"use strict";
const path = require("path");
const minimist = require("minimist");
const gulp = require("gulp");
const gulpClean = require("gulp-clean");
const gulpUglify = require("gulp-uglify");
const rollup = require("rollup");
const alias = require("@rollup/plugin-alias");
const { nodeResolve } = require("@rollup/plugin-node-resolve");
const replace = require("@rollup/plugin-replace");
const typescript = require("@rollup/plugin-typescript");
const cleanDist = () => {
return gulp.src("dist", { allowEmpty: true }).pipe(gulpClean());
};
const checkArgv = () => {
const args = [
"platform",
"domain",
"app_id",
"app_key",
"xor_key",
"token_key",
"slogan",
"share_img_url",
];
const argv = minimist(process.argv.slice(2));
let flag = false;
const ret = {};
args.some((arg) => {
if (!argv[arg]) {
flag = true;
console.log(`Missing --${arg}`);
return true;
}
ret[`__${arg}__`] = argv[arg];
return false;
});
if (flag) return Promise.resolve(false);
return Promise.resolve(ret);
};
const generateSdk = async () => {
const replaces = await checkArgv();
if (!replaces) {
process.exit();
}
return rollup
.rollup({
input: "./src/main.ts",
treeshake: true,
plugins: [
alias({
entries: [
{
find: "@",
replacement: path.resolve(__dirname, "./src"),
},
],
customResolver: nodeResolve({
extensions: [".ts"],
}),
}),
replace({
preventAssignment: true,
...replaces,
}),
typescript({
tsconfig: "./tsconfig.json",
}),
],
})
.then((bundle) => {
const writePromises = [];
const moduleTypes = ["esm"];
moduleTypes.map((moduleType) => {
writePromises.push(
bundle.write({
file: "./dist/my-sdk.js",
format: moduleType,
name: "my-sdk",
sourcemap: false,
})
);
});
return Promise.all(writePromises);
})
.catch((err) => {
console.log(err);
});
};
const uglifyJs = () => {
return gulp.src("./dist/*.js").pipe(gulpUglify()).pipe(gulp.dest("./dist/"));
};
exports.default = gulp.series(cleanDist, generateSdk, uglifyJs);