-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
44 lines (37 loc) · 1.27 KB
/
main.js
File metadata and controls
44 lines (37 loc) · 1.27 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
import { io, exec, plugin } from "@shevky/base";
const PLUGIN_NAME = "shevky-esbuild";
const PLUGIN_VERSION = "0.0.3";
const PLUGIN_ROOT = import.meta.dirname ?? process.cwd();
/** @type {import("@shevky/base").PluginHooks} */
const hooks = {
[plugin.hooks.ASSETS_COPY]: async function (ctx) {
const projectRoot = io.path.resolve(process.cwd(), ctx.paths.root);
const sourePath = io.path.resolve(projectRoot, "src", "js", "app.js");
const distPath = io.path.resolve(projectRoot, "dist", "output.js");
if (!(await ctx.file.exists(sourePath))) {
ctx.log.err(
`[${PLUGIN_NAME}] Skipping JS bundling because the source file is missing at ${sourePath}.`,
);
return;
}
const args = [
"esbuild",
sourePath,
"--bundle",
"--format=esm",
"--target=es2018",
"--outfile=" + distPath,
];
if (ctx.config.build.minify) {
args.push("--minify");
args.push("--drop:debugger");
args.push("--drop:console");
args.push("--ignore-annotations");
args.push("--sourcemap");
}
await exec.executeNpx(args, PLUGIN_ROOT);
ctx.log.debug(`[${PLUGIN_NAME}] ESBuild has been completed.`);
},
};
const PLUGIN = { name: PLUGIN_NAME, version: PLUGIN_VERSION, hooks };
export default PLUGIN;