forked from DesModder/DesModder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.mjs
110 lines (100 loc) · 2.87 KB
/
esbuild.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
109
110
/* eslint-disable no-console */
import { esbuildPluginInline } from "./loaders/esbuild-plugin-inline.mjs";
import { esbuildPluginLezer } from "./loaders/esbuild-plugin-lezer.mjs";
import { esbuildPluginReplacements } from "./loaders/esbuild-plugin-replacements.mjs";
import { loadFile } from "./loaders/utils.mjs";
import esbuild from "esbuild";
import { copy } from "esbuild-plugin-copy";
import { lessLoader } from "esbuild-plugin-less";
import { promises as fs } from "fs";
import parseArgs from "minimist-lite";
const argv = parseArgs(process.argv.slice(2));
const unindent2spaces = (s) => s.toString().replaceAll(/\n {2}|\n\s*$/g, "\n");
if (argv.help) {
console.log(
unindent2spaces`Usage:
node esbuild.mjs [options]
Options:
--help Show help
--browser=... Browser target: "firefox", or "chrome" [default: "chrome"]
--watch Watch mode: rebuild on file system changes [default: false]
--outdir=... Output directory [default: "dist"]
Examples:
# Dev server for Chrome
node esbuild.mjs --watch --browser=chrome
# Final build for Firefox
node esbuild.mjs --browser=firefox`
);
process.exit(0);
}
if (
argv.browser !== undefined &&
!["firefox", "chrome"].includes(argv.browser)
) {
console.error(`Invalid browser name: ${argv.browser}`);
process.exit(1);
}
const { version } = JSON.parse(await loadFile("./package.json"));
const browser = argv.browser ?? "chrome";
const watch = !!argv.watch;
const outdir = argv.outdir ?? "dist";
const opts = {
entryPoints: [
"src/background.ts",
"src/script.ts",
"src/preload/content.ts",
"src/preload/script.ts",
],
// don't include source map on release builds
sourcemap: watch ? "inline" : false,
bundle: true,
outdir,
plugins: [
lessLoader(),
esbuildPluginInline(),
esbuildPluginLezer(),
esbuildPluginReplacements(),
// The copy plugin *should* support array or glob "from", but I encountered
// error: Cannot read properties of undefined (reading 'slice')
// at setup (node_modules/esbuild-plugin-copy/dist/index.mjs:69:23)
copy({
resolveFrom: "cwd",
assets: {
from: [`./public/${browser}/*`],
to: outdir,
},
}),
copy({
resolveFrom: "cwd",
assets: {
from: [`./public/common/*`],
to: outdir,
},
}),
],
define: {
BROWSER: JSON.stringify(browser),
VERSION: JSON.stringify(version),
},
loader: {
".ts": "ts",
".ftl": "text",
".woff": "dataurl",
".html": "text",
},
logLevel: "info",
};
// clean dist folder
try {
await fs.rm(outdir, { recursive: true });
} catch (e) {
// permit no dist folder to begin with
if (e?.code !== "ENOENT") throw e;
}
if (watch) {
const ctx = await esbuild.context(opts);
await ctx.rebuild();
await ctx.watch();
} else {
void esbuild.build(opts);
}