This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
build.ts
131 lines (121 loc) · 3.93 KB
/
build.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
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
import { BuildOptions, build } from "esbuild";
import { copy, emptyDir, ensureDir, readdir, writeFile } from "fs-extra";
import { dirname, sep as s } from "path";
import { getAllLocators, getPackageInformation } from "pnpapi";
type Package = {
packageLocation: string;
packageDependencies: Map<string, string>;
};
type Packages = Record<string, Record<string, Package>>;
const unpackedPackages = ["chii"];
const junkFiles = [
".d.ts",
".markdown",
".md",
"/.eslintrc",
"/.eslintrc.js",
"/.prettierrc",
"/.nycrc",
".yml",
".yaml",
".bak",
"/.editorconfig",
"/bower.json",
"/.jscs.json",
"/AUTHORS",
"/LICENSE",
"/License",
"/yarn.lock",
"/package-lock.json",
".map",
".debug.js",
".min.js",
".test.js",
"/test/",
"/bin/",
"/tests/",
"/.github/",
];
const isProduction = process.env["NODE_ENV"] == "production";
const commonOptions: Partial<BuildOptions> = {
target: "node18",
bundle: true,
platform: "node",
write: true,
allowOverwrite: true,
sourcemap: isProduction ? false : "inline",
minify: isProduction,
treeShaking: isProduction,
};
async function buildBundles() {
const buildPromise = Promise.all([
build({
...commonOptions,
entryPoints: [`src${s}main${s}main.ts`],
outfile: `dist${s}_${s}qqntim.js`,
external: ["electron", "./index.js", ...unpackedPackages],
}),
build({
...commonOptions,
entryPoints: [`src${s}renderer${s}main.ts`],
outfile: `dist${s}_${s}qqntim-renderer.js`,
external: ["electron", ...unpackedPackages],
}),
]);
return await buildPromise;
}
async function buildBuiltinPlugins() {
const rootDir = `src${s}builtins`;
const pluginsDir = await readdir(rootDir);
await Promise.all(
pluginsDir.map(async (dir) => {
const pluginDir = `${rootDir}${s}${dir}`;
const distDir = `dist${s}_${s}builtins${s}${dir}`;
await ensureDir(pluginDir);
await build({
...commonOptions,
entryPoints: [`${pluginDir}${s}src${s}main.ts`, `${pluginDir}${s}src${s}renderer.ts`],
outdir: distDir,
external: ["electron", "react", "react/jsx-runtime", "react-dom", "react-dom/client", "qqntim/main", "qqntim/renderer"],
format: "cjs",
});
await copy(`${pluginDir}${s}publish`, `${distDir}`);
}),
);
}
async function prepareDistDir() {
await emptyDir("dist");
await ensureDir(`dist${s}_`);
await copy("publish", "dist");
}
function collectDeps() {
const packages: Packages = {};
getAllLocators().forEach((locator) => {
if (!packages[locator.name]) packages[locator.name] = {};
packages[locator.name][locator.reference] = getPackageInformation(locator);
});
return packages;
}
async function unpackPackage(packages: Packages, rootDir: string, name: string, reference?: string) {
const item = packages[name];
if (!item) return;
const location = item[reference ? reference : Object.keys(item)[0]];
const dir = `${rootDir}${s}node_modules${s}${name}`;
await ensureDir(dir);
await copy(location.packageLocation, dir, {
filter: (src) => {
for (const file of junkFiles) {
if (src.includes(file)) return false;
}
return true;
},
});
const promises: Promise<void>[] = [];
location.packageDependencies.forEach((depReference, depName) => {
if (name == depName) return;
promises.push(unpackPackage(packages, dir, depName, depReference));
});
await Promise.all(promises);
}
const packages = collectDeps();
prepareDistDir().then(() => Promise.all([buildBundles(), buildBuiltinPlugins(), Promise.all(unpackedPackages.map((unpackedPackage) => unpackPackage(packages, `dist${s}_`, unpackedPackage)))]));