This repository was archived by the owner on Aug 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
132 lines (113 loc) · 3.16 KB
/
Copy pathbuild.js
File metadata and controls
132 lines (113 loc) · 3.16 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
const esbuild = require("esbuild");
const fs = require("fs/promises");
const fsSync = require("fs");
const path = require("path");
const args = process.argv.slice(2);
const mode =
args.includes("--prod") || args.includes("-p") ? "production" : "development";
const watch = args.includes("--watch") || args.includes("-w");
const isProd = mode === "production";
console.log(
`\nBuilding in ${mode} mode${watch ? " (watch enabled)" : ""}...\n`,
);
async function copyFolder(src, dest) {
try {
await fs.mkdir(dest, { recursive: true });
const entries = await fs.readdir(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
await copyFolder(srcPath, destPath);
} else {
await fs.copyFile(srcPath, destPath);
}
}
} catch (err) {
console.error(`Error copying ${src}:`, err.message);
throw err;
}
}
const commonOptions = {
bundle: true,
treeShaking: true,
format: "esm",
logLevel: "info",
};
const jsOptions = {
...commonOptions,
entryPoints: ["resources/pub/_js/**/*.js"],
outdir: "public/_dist/js",
minify: isProd,
sourcemap: !isProd,
target: isProd ? ["es2020"] : ["esnext"],
};
const cssOptions = {
...commonOptions,
entryPoints: ["resources/pub/_css/**/*.css"],
outdir: "public/_dist/css",
loader: {
".css": "css",
".webp": "file",
".png": "file",
".jpg": "file",
".jpeg": "file",
".svg": "dataurl",
},
minify: isProd,
sourcemap: !isProd,
};
async function build() {
try {
const startTime = Date.now();
await Promise.all([esbuild.build(jsOptions), esbuild.build(cssOptions)]);
await copyFolder("resources/pub/_assets", "public/_assets");
const duration = Date.now() - startTime;
console.log(`\nBuild completed in ${duration}ms\n`);
} catch (err) {
console.error("\nBuild failed:", err);
process.exit(1);
}
}
async function startWatch() {
try {
console.log("Starting watch mode...\n");
const jsContext = await esbuild.context(jsOptions);
const cssContext = await esbuild.context(cssOptions);
await Promise.all([jsContext.watch(), cssContext.watch()]);
const assetsWatcher = fsSync.watch(
"resources/pub/_assets",
{ recursive: true },
async (eventType, filename) => {
if (filename) {
console.log(`Asset changed: ${filename}`);
try {
await copyFolder("resources/pub/_assets", "public/_assets");
console.log("Assets copied\n");
} catch (err) {
console.error("Error copying assets:", err.message);
}
}
},
);
console.log("Watch mode active. Press Ctrl+C to stop.\n");
process.on("SIGINT", async () => {
console.log("\nStopping watch mode...");
assetsWatcher.close();
await jsContext.dispose();
await cssContext.dispose();
process.exit(0);
});
} catch (err) {
console.error("Watch mode failed:", err);
process.exit(1);
}
}
(async () => {
if (watch) {
await build();
await startWatch();
} else {
await build();
}
})();