diff --git a/Dockerfile b/Dockerfile index b54014b..71435eb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -88,7 +88,7 @@ COPY ./LICENSE ./ ENV LD_LIBRARY_PATH=/app/lib ENV PATH=/app/bin:$PATH -RUN deno task fetch && deno task server-build && mkdir -p ./thumbnails && chmod 777 ./thumbnails && mkdir -p ./downloads && chmod 777 ./downloads && mkdir -p ./data && chmod 777 ./data && chmod 777 /deno-dir +RUN deno task fetch && deno task server-build && deno task prebuild && mkdir -p ./thumbnails && chmod 777 ./thumbnails && mkdir -p ./downloads && chmod 777 ./downloads && mkdir -p ./data && chmod 777 ./data && chmod 777 /deno-dir ENV DENO_DEPLOYMENT_ID=${DENO_DEPLOYMENT_ID} ENV DOCKER=true ENV DB_USE_FFI=true diff --git a/deno.json b/deno.json index e1aed39..bbb043d 100644 --- a/deno.json +++ b/deno.json @@ -11,7 +11,8 @@ "compile_full": "deno compile --allow-read --allow-write --allow-run=tasklist.exe --allow-env=DENO_DEPLOYMENT_ID --allow-net --unstable", "fetch": "deno run --allow-read=./ --allow-write=./ --allow-net fetch_static_files.ts", "gen_meili_server_key": "deno run --allow-net scripts/gen_meili_server_key.ts", - "server-build": "deno run -A --unstable server-dev.ts build" + "server-build": "deno run -A --unstable server-dev.ts build", + "prebuild": "deno run -A scripts/prebuild.ts" }, "fmt": { "indentWidth": 4, diff --git a/routes/_middleware.ts b/routes/_middleware.ts index ca1cef1..38e6bde 100644 --- a/routes/_middleware.ts +++ b/routes/_middleware.ts @@ -1,65 +1,19 @@ import { MiddlewareHandlerContext } from "$fresh/server.ts"; -import { build } from "esbuild/mod.js"; -import { join, resolve } from "std/path/mod.ts"; -import { asyncForEach, calFileMd5, checkMapFile } from "../utils.ts"; +import { join } from "std/path/mod.ts"; import { get_file_response, GetFileResponseOptions, } from "../server/get_file_response.ts"; import { exists } from "std/fs/exists.ts"; import { get_task_manager } from "../server.ts"; +import { build_sw } from "../server/build_sw.ts"; const STATIC_FILES = ["/common.css", "/scrollBar.css", "/sw.js", "/sw.js.map"]; export async function handler(req: Request, ctx: MiddlewareHandlerContext) { const url = new URL(req.url); if (url.pathname == "/sw.js") { - let base = import.meta.resolve("../static").slice(7); - if (Deno.build.os === "windows") { - base = base.slice(1); - } - const map_file = join(base, "sw.meta.json"); - if (!(await checkMapFile(map_file))) { - const data = await build({ - entryPoints: [join(base, "sw.ts")], - outfile: join(base, "sw.js"), - metafile: true, - bundle: true, - minify: true, - sourcemap: true, - }); - const map = data.metafile; - await asyncForEach( - Object.getOwnPropertyNames(map.inputs), - async (k) => { - const p = resolve(k); - if (p !== k) { - map.inputs[p] = map.inputs[k]; - delete map.inputs[k]; - k = p; - } - const data = map.inputs[k]; - // @ts-ignore add custom property - data.md5 = await calFileMd5(k); - }, - ); - await asyncForEach( - Object.getOwnPropertyNames(map.outputs), - async (k) => { - const p = resolve(k); - if (p !== k) { - map.outputs[p] = map.outputs[k]; - delete map.outputs[k]; - k = p; - } - const data = map.outputs[k]; - // @ts-ignore add custom property - data.md5 = await calFileMd5(k); - }, - ); - await Deno.writeTextFile(map_file, JSON.stringify(map)); - console.log("Rebuild."); - } + build_sw(); } if (STATIC_FILES.includes(url.pathname)) { let base = import.meta.resolve("../static").slice(7); diff --git a/scripts/prebuild.ts b/scripts/prebuild.ts new file mode 100644 index 0000000..5069c3f --- /dev/null +++ b/scripts/prebuild.ts @@ -0,0 +1,4 @@ +import { build_sw } from "../server/build_sw.ts"; + +await build_sw(); +Deno.exit(0); diff --git a/server/build_sw.ts b/server/build_sw.ts new file mode 100644 index 0000000..d9a8fee --- /dev/null +++ b/server/build_sw.ts @@ -0,0 +1,52 @@ +import { build } from "esbuild/mod.js"; +import { join, resolve } from "std/path/mod.ts"; +import { asyncForEach, calFileMd5, checkMapFile } from "../utils.ts"; + +export async function build_sw() { + let base = import.meta.resolve("../static").slice(7); + if (Deno.build.os === "windows") { + base = base.slice(1); + } + const map_file = join(base, "sw.meta.json"); + if (!(await checkMapFile(map_file))) { + const data = await build({ + entryPoints: [join(base, "sw.ts")], + outfile: join(base, "sw.js"), + metafile: true, + bundle: true, + minify: true, + sourcemap: true, + }); + const map = data.metafile; + await asyncForEach( + Object.getOwnPropertyNames(map.inputs), + async (k) => { + const p = resolve(k); + if (p !== k) { + map.inputs[p] = map.inputs[k]; + delete map.inputs[k]; + k = p; + } + const data = map.inputs[k]; + // @ts-ignore add custom property + data.md5 = await calFileMd5(k); + }, + ); + await asyncForEach( + Object.getOwnPropertyNames(map.outputs), + async (k) => { + const p = resolve(k); + if (p !== k) { + map.outputs[p] = map.outputs[k]; + delete map.outputs[k]; + k = p; + } + const data = map.outputs[k]; + // @ts-ignore add custom property + data.md5 = await calFileMd5(k); + }, + ); + await Deno.writeTextFile(map_file, JSON.stringify(map)); + console.log("Rebuild."); + } +}