Skip to content

Commit

Permalink
FIx sw.js build issue
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc committed Jan 22, 2024
1 parent 11e358d commit 828e800
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
52 changes: 3 additions & 49 deletions routes/_middleware.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
4 changes: 4 additions & 0 deletions scripts/prebuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { build_sw } from "../server/build_sw.ts";

await build_sw();
Deno.exit(0);
52 changes: 52 additions & 0 deletions server/build_sw.ts
Original file line number Diff line number Diff line change
@@ -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.");
}
}

0 comments on commit 828e800

Please sign in to comment.