generated from moonlight-mod/sample-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.mjs
108 lines (94 loc) · 2.33 KB
/
build.mjs
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
import * as esbuild from "esbuild";
import copyStaticFiles from "esbuild-copy-static-files";
import fs from "fs";
const prod = process.env.NODE_ENV === "production";
const watch = process.argv.includes("--watch");
function makeConfig(ext, name) {
const entryPoints = [];
const fileExts = ["js", "jsx", "ts", "tsx"];
for (const fileExt of fileExts) {
const path = `./src/${ext}/${name}.${fileExt}`;
if (fs.existsSync(path)) entryPoints.push(path);
}
const wpModulesDir = `./src/${ext}/webpackModules`;
if (fs.existsSync(wpModulesDir) && name === "index") {
const wpModules = fs.readdirSync(wpModulesDir);
for (const wpModule of wpModules) {
entryPoints.push(`./src/${ext}/webpackModules/${wpModule}`);
}
}
if (entryPoints.length === 0) return null;
const wpImportPlugin = {
name: "webpackImports",
setup(build) {
build.onResolve({ filter: /^@moonlight-mod\/wp\// }, (args) => {
const wpModule = args.path.replace(/^@moonlight-mod\/wp\//, "");
return {
path: wpModule,
external: true
};
});
}
};
const timeFormatter = new Intl.DateTimeFormat(undefined, {
hour: "numeric",
minute: "numeric",
second: "numeric",
hour12: false
});
const buildLogPlugin = {
name: "buildLog",
setup(build) {
build.onEnd(() => {
console.log(`[${timeFormatter.format(new Date())}] [${ext}/${name}] build finished`);
});
}
}
return {
entryPoints,
outdir: `./dist/${ext}`,
format: "cjs",
platform: "node",
treeShaking: true,
bundle: true,
minify: prod,
sourcemap: "inline",
external: [
"electron",
"fs",
"path",
"module",
"events",
"original-fs"
],
plugins: [
copyStaticFiles({
src: `./src/${ext}/manifest.json`,
dest: `./dist/${ext}/manifest.json`
}),
wpImportPlugin,
buildLogPlugin
]
};
}
const exts = fs.readdirSync("./src");
const config = exts
.map((x) => [
makeConfig(x, "index"),
makeConfig(x, "node"),
makeConfig(x, "host")
])
.flat()
.filter((c) => c !== null);
if (watch) {
await Promise.all(
config.map(async (c) => {
const ctx = await esbuild.context(c);
await ctx.watch();
})
);
} else {
for (const c of config) {
await esbuild.build(c);
}
}