-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
138 lines (118 loc) · 3.32 KB
/
index.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
import * as esbuild from "esbuild";
import fs from "node:fs";
import path from "node:path";
import deduplicatedLogging from "./plugins/deduplicatedLogging.js";
import taggedBuildLog from "./plugins/taggedBuildLog.js";
import wpImport from "./plugins/wpImport.js";
import copyFile from "./plugins/copyFile.js";
const prod = process.env.NODE_ENV === "production";
const external = [
"electron",
"fs",
"path",
"module",
"events",
"original-fs",
"discord" // mappings
];
const sides = ["index", "webpackModules", "node", "host"];
const fileExts = ["js", "jsx", "ts", "tsx"];
export function makeExtConfig({
src,
dst,
ext,
side,
extraExternal = [],
extraPlugins = [],
extraConfig = {},
esm = false
}) {
const entryPoints = [];
if (side !== "webpackModules") {
for (const fileExt of fileExts) {
const filePath = path.join(src, `${side}.${fileExt}`);
if (fs.existsSync(filePath)) entryPoints.push(filePath);
}
}
const wpModulesDir = path.join(src, "webpackModules");
if (side === "webpackModules" && fs.existsSync(wpModulesDir)) {
const wpModules = fs.readdirSync(wpModulesDir);
for (const wpModule of wpModules) {
if (fs.statSync(path.join(wpModulesDir, wpModule)).isDirectory()) {
for (const fileExt of fileExts) {
const filePath = path.join(
wpModulesDir,
wpModule,
`index.${fileExt}`
);
if (fs.existsSync(filePath))
entryPoints.push({
in: filePath,
out: wpModule
});
}
} else {
entryPoints.push(path.join(wpModulesDir, wpModule));
}
}
}
if (entryPoints.length === 0) return null;
return {
entryPoints,
outdir: side === "webpackModules" ? path.join(dst, "webpackModules") : dst,
format: esm && side === "index" ? "esm" : "iife",
globalName: "module.exports",
platform: ["index", "webpackModules"].includes(side) ? "browser" : "node",
treeShaking: true,
bundle: true,
minify: prod,
sourcemap: "inline",
external: [...external, ...extraExternal],
plugins: [
copyFile(
path.join(src, "manifest.json"),
path.join(dst, "manifest.json")
),
copyFile(path.join(src, "style.css"), path.join(dst, "style.css")),
wpImport,
deduplicatedLogging,
taggedBuildLog(`${ext}/${side}`),
...extraPlugins
],
...extraConfig
};
}
export function makeExtConfigs(cfg) {
return sides
.map((side) => makeExtConfig({ ...cfg, side }))
.filter((cfg) => cfg != null);
}
export async function buildExt(cfg) {
const cfgs = makeExtConfigs(cfg);
const builds = [];
for (const cfg of cfgs) {
builds.push(await esbuild.build(cfg));
}
return builds;
}
export async function watchExt(cfg) {
const buildCfgs = makeExtConfigs(cfg);
const watchers = [];
for (const buildCfg of buildCfgs) {
const ctx = await esbuild.context(buildCfg);
async function rebuild() {
try {
await ctx.rebuild();
} catch {
// esbuild will log errors, we just need to not do anything
}
}
await rebuild();
const watcher = fs.watch(cfg.src, { recursive: true }, async () => {
await rebuild();
});
watchers.push(watcher);
}
return watchers;
}
export { deduplicatedLogging, taggedBuildLog, wpImport };