This commit is contained in:
Rizky 2024-09-02 16:12:42 +07:00
parent e551c0bb85
commit cb544118d9
3 changed files with 19 additions and 11 deletions

View File

@ -8,6 +8,7 @@ import { fdir } from "fdir";
import { statSync } from "fs"; import { statSync } from "fs";
import { listAsync, removeAsync, writeAsync } from "fs-jetpack"; import { listAsync, removeAsync, writeAsync } from "fs-jetpack";
import { platform } from "os"; import { platform } from "os";
import { $ } from "bun";
const brotli = await brotliPromise; const brotli = await brotliPromise;
@ -87,7 +88,7 @@ const build_all =
process.argv[process.argv.length - 1] === "main" ? false : true; process.argv[process.argv.length - 1] === "main" ? false : true;
if (build_all) { if (build_all) {
await removeAsync(dir.path("app/web/.parcel-cache")); await removeAsync(dir.path("app/web/.parcel-cache"));
await removeAsync(dir.path("app/static")); await removeAsync(dir.data("static-temp"));
await writeAsync( await writeAsync(
dir.path("app/web/timestamp.ts"), dir.path("app/web/timestamp.ts"),
@ -106,7 +107,7 @@ if (build_all) {
// "--no-optimize", // "--no-optimize",
"--no-scope-hoist", "--no-scope-hoist",
"--dist-dir", "--dist-dir",
dir.path(`app/static`), dir.data(`static`),
]; ];
let build_static = let build_static =
@ -155,25 +156,27 @@ if (build_all) {
}); });
await parcel.exited; await parcel.exited;
const static_br = dir.path("app/static-br"); const static_br = dir.data("static-br-temp");
await removeAsync(static_br); await removeAsync(static_br);
const static_files = await listAsync(dir.path("app/static")); const static_files = await listAsync(dir.data("static-temp"));
if (static_files) { if (static_files) {
await Promise.all( await Promise.all(
static_files static_files
.filter((file) => statSync(dir.path(`app/static/${file}`)).isFile()) .filter((file) => statSync(dir.data(`static-temp/${file}`)).isFile())
.map(async (file) => { .map(async (file) => {
if (!(await Bun.file(dir.path(`app/static-br/${file}`)).exists())) { if (
!(await Bun.file(dir.data(`static-br-temp/${file}`)).exists())
) {
const br = brotli.compress( const br = brotli.compress(
new Uint8Array( new Uint8Array(
await Bun.file(dir.path(`app/static/${file}`)).arrayBuffer() await Bun.file(dir.data(`static-temp/${file}`)).arrayBuffer()
), ),
{ quality: 11 } { quality: 11 }
); );
if (br) { if (br) {
console.log(`Compressing [static] ${file}`); console.log(`Compressing [static] ${file}`);
await writeAsync( await writeAsync(
dir.path(`app/static-br/${file}`), dir.data(`static-br-temp/${file}`),
Buffer.from(br) Buffer.from(br)
); );
} }
@ -181,5 +184,10 @@ if (build_all) {
}) })
); );
} }
$`rm -rf ${dir.data(`static-br`)}`.nothrow();
$`rm -rf ${dir.data(`static`)}`.nothrow();
$`mv ${dir.data(`static-br-temp`)} ${dir.data(`static-br`)}`.nothrow();
$`mv ${dir.data(`static-temp`)} ${dir.data(`static`)}`.nothrow();
} }
} }

View File

@ -27,7 +27,7 @@ export const serveStatic = {
} }
["app/static", "app/web/public"].forEach((base_path) => { ["app/static", "app/web/public"].forEach((base_path) => {
watch(dir.path(`app/static`), async (event, filename) => { watch(dir.data(`static`), async (event, filename) => {
if (filename) { if (filename) {
cache.dev = {}; cache.dev = {};
} }

View File

@ -19,7 +19,7 @@ export const parcelBuild = async () => {
"./src/index.html", "./src/index.html",
g.mode === "dev" ? "--no-hmr" : "", g.mode === "dev" ? "--no-hmr" : "",
"--dist-dir", "--dist-dir",
dir.path(`app/static`), dir.data(`static`),
]; ];
if (g.mode === "dev") { if (g.mode === "dev") {