-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild.mjs
More file actions
160 lines (130 loc) · 4.04 KB
/
build.mjs
File metadata and controls
160 lines (130 loc) · 4.04 KB
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
156
157
158
159
160
import { readFile, writeFile, readdir, mkdir } from "fs/promises";
import { createHash } from "crypto";
import { rollup } from "rollup";
import esbuild from "rollup-plugin-esbuild";
import commonjs from "@rollup/plugin-commonjs";
import nodeResolve from "@rollup/plugin-node-resolve";
import alias from '@rollup/plugin-alias';
import { fileURLToPath } from 'url';
import { dirname, resolve, extname } from 'path';
const __dirname = dirname(fileURLToPath(import.meta.url));
import swc from "@swc/core";
import os from "os";
import express from "express";
import { isProd } from "./config.js";
const extensions = [".js", ".jsx", ".mjs", ".ts", ".tsx", ".cts", ".mts"];
const PORT = 8000;
const stripVersions = (str) => str.replace(/\s?v\d+.\d+.\w+/, "");
const commonPlugins = [
alias({
entries: [
{ find: '~lib', replacement: resolve(__dirname, 'lib') },
],
}),
nodeResolve(),
commonjs(),
{
name: "swc",
async transform(code, id) {
const ext = extname(id);
if (!extensions.includes(ext)) return null;
const ts = ext.includes("ts");
const tsx = ts ? ext.endsWith("x") : undefined;
const jsx = !ts ? ext.endsWith("x") : undefined;
const result = await swc.transform(code, {
filename: id,
jsc: {
externalHelpers: true,
parser: {
syntax: ts ? "typescript" : "ecmascript",
tsx,
jsx,
},
},
env: {
targets: "defaults",
include: [
"transform-classes",
"transform-arrow-functions",
],
},
});
return result.code;
},
},
];
const minifyPlugin = esbuild({ minify: true });
const nonMinifyPlugin = esbuild({ minify: false });
async function buildPlugin(isDebug = false, NOTE, path, distro, plugins, usesKeyword = "@vendetta") {
const files = await readdir(`./${path}`);
for (let plug of files) {
const manifest = JSON.parse(await readFile(`./${path}/${plug}/manifest.json`));
if(!manifest || !manifest.main) {
console.log(`Skipped => ${plug} => invalid manifest entry.`)
continue;
}
const outPath = `${distro}/${plug}/index.js`;
// await readdir("./debug").catch(() => mkdir("./debug"))
// await readdir("./dist").catch(() => mkdir("./dist"))
// console.log(manifest)
try {
const bundle = await rollup({
input: `./${path}/${plug}/${manifest.main}`,
onwarn: () => {},
plugins,
});
await bundle.write({
file: outPath,
globals(id) {
if (id.startsWith(usesKeyword)) return id.substring(1).replace(/\//g, ".");
const map = {
react: "window.React",
};
return map[id] || null;
},
format: "iife",
compact: true,
exports: "named",
});
await bundle.close();
const toHash = await readFile(outPath);
manifest.hash = createHash("sha256").update(toHash).digest("hex");
manifest.main = "index.js";
if(isDebug) {
manifest.name = `[DEBUG] ${manifest.name}`;
if(manifest?.originalName) {
manifest.originalName = `[DEBUG] ${manifest.originalName}`;
}
}
await writeFile(`${distro}/${plug}/manifest.json`, JSON.stringify(manifest));
console.log(`[${isDebug ? "debug/": ""}${path}/${plug}] [${NOTE}] Successfully built ${manifest.name}!`);
} catch (e) {
console.error("Failed to build plugin...", e);
process.exit(1);
}
}
if(files?.length) {
console.log(NOTE + " | Done Building");
} else {
console.log(NOTE + " | ENDED WITHOUT ANY FILES");
}
}
// Build Plugin
// Debug
await buildPlugin(true, "DEBUG", "angel", "./dist/debug/angel", [...commonPlugins, nonMinifyPlugin], "@vendetta");
// Prod
console.log('\n')
await buildPlugin(false, "PRODUCTION", "angel", "./dist/angel", [...commonPlugins, minifyPlugin], "@vendetta");
// Serve if Local
if (!isProd) {
const IPs = Object.values(os.networkInterfaces())
.flat()
.filter(({ family, internal }) => family === "IPv4" && !internal)
.map(({ address }) => address);
const app = express();
app.use(express.static('dist'));
app.use(express.static('debug'));
app.listen(PORT);
console.log(`\nServed on ${IPs[0]}:${PORT}`);
app.get("*", (req, res) => console.log(req?.url));
}