-
Notifications
You must be signed in to change notification settings - Fork 1
/
build-static.js
57 lines (48 loc) · 1.25 KB
/
build-static.js
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
const { build } = require("esbuild");
const envFilePlugin = require("esbuild-envfile-plugin");
const fs = require("fs-extra");
const { buildHTML } = require("./utils/html.js");
const { OUT_DIR } = process.env;
const filterFunc = (src, dest) => {
const dontCopyFiles = ["script.js", "index.html"];
const shouldCopy = !dontCopyFiles.some((file) => src.includes(file));
if (shouldCopy) {
console.log("copying: ", src);
}
return shouldCopy;
};
async function emptyDir() {
try {
await fs.emptyDir(OUT_DIR);
console.log("/static directory emptied\n");
} catch (err) {
console.error(err);
}
}
async function copyFiles() {
try {
await fs.copy("public", OUT_DIR, { filter: filterFunc });
console.log("\nSuccess copying files");
} catch (err) {
console.error("\nError copying files: ", err);
}
}
(async () => {
await emptyDir();
await copyFiles();
/**
* Build the /static HTML file
*/
fs.writeFile("./static/index.html", await buildHTML(true), (error) => {
if (error) {
console.error("Error writing index.html file: ", error);
}
});
await build({
bundle: true,
entryPoints: ["src/dev.tsx"],
outfile: `${OUT_DIR}/script.js`,
plugins: [envFilePlugin],
sourcemap: true,
});
})();