Skip to content

Commit

Permalink
fix worker run
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Oct 6, 2024
1 parent 3f2f37e commit 180f0b2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
19 changes: 15 additions & 4 deletions cli/build_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,27 @@ import noCors from "../middlewares/no_cors.ts";
import notFound from "../middlewares/not_found.ts";
import reload from "../middlewares/reload.ts";
import { buildSite } from "./utils.ts";
import "./missing_worker_apis.ts";
import { initLocalStorage } from "./missing_worker_apis.ts";

addEventListener("message", (event) => {
const { type } = event.data;

if (type === "build" || type === "rebuild") {
return build(event.data);
}

if (type === "localStorage") {
return initLocalStorage(event.data.data);
}
});

interface BuildOptions {
type: "build" | "rebuild";
config?: string;
serve?: boolean;
}

onmessage = async (event) => {
const { type, config, serve } = event.data as BuildOptions;
async function build({ type, config, serve }: BuildOptions) {
const site = await buildSite(config);

// Set the live reload environment variable to add hash to the URLs in the module loader
Expand Down Expand Up @@ -122,4 +133,4 @@ onmessage = async (event) => {
}

server.start();
};
}
19 changes: 15 additions & 4 deletions cli/cms_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@ import { normalizePath } from "../core/utils/path.ts";
import { fromFileUrl } from "../deps/path.ts";
import { setEnv } from "../core/utils/env.ts";
import { createSite } from "./utils.ts";
import "./missing_worker_apis.ts";
import { initLocalStorage } from "./missing_worker_apis.ts";

addEventListener("message", (event) => {
const { type } = event.data;

if (type === "build" || type === "rebuild") {
return build(event.data);
}

if (type === "localStorage") {
return initLocalStorage(event.data.data);
}
});

interface CMSOptions {
type: "build" | "rebuild";
config?: string;
}

onmessage = async (event) => {
const { type, config } = event.data as CMSOptions;
async function build({ type, config }: CMSOptions) {
const cmsConfig = await getConfigFile(undefined, ["_cms.ts", "_cms.js"]);

if (!cmsConfig) {
Expand Down Expand Up @@ -79,4 +90,4 @@ onmessage = async (event) => {
}
},
});
};
}
17 changes: 8 additions & 9 deletions cli/missing_worker_apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,15 @@ class S implements Storage {
}
}

globalThis.addEventListener("message", (event) => {
if (event.data.type === "localStorage") {
const storage = new S("localStorage");
// sessionStorage is always empty
globalThis.sessionStorage = new S();

for (const [key, value] of Object.entries(event.data.data)) {
storage.setItem(key, value);
}
export function initLocalStorage(data: Record<string, string>) {
const storage = new S("localStorage");

globalThis.localStorage = storage;
for (const [key, value] of Object.entries(data)) {
storage.setItem(key, value);
}
});

globalThis.sessionStorage = new S();
globalThis.localStorage = storage;
}
1 change: 1 addition & 0 deletions core/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class FSWriter implements Writer {
}

const filename = posix.join(this.dest, outputPath);
console.log(page.data.url, page.src);
const id = this.caseSensitiveUrls ? filename : filename.toLowerCase();
const hash = await sha1(content);
const previous = this.#outputs.get(id);
Expand Down

0 comments on commit 180f0b2

Please sign in to comment.