cleanup
This commit is contained in:
parent
0b7b6cf79c
commit
4576d15bea
9 changed files with 20 additions and 41 deletions
|
|
@ -21,4 +21,4 @@ node_modules:
|
||||||
|
|
||||||
.PHONY: cleanup
|
.PHONY: cleanup
|
||||||
cleanup: test
|
cleanup: test
|
||||||
prefix "$@" git checkout -- src/platform.js
|
prefix "$@" git checkout -- src/platform.js src/worker.js
|
||||||
|
|
|
||||||
|
|
@ -36,13 +36,14 @@ async function buildDynamicFiles(platform) {
|
||||||
const platformPath = join(SRC_DIR, "platform.js");
|
const platformPath = join(SRC_DIR, "platform.js");
|
||||||
await writeFile(platformPath, platformContent);
|
await writeFile(platformPath, platformContent);
|
||||||
|
|
||||||
const workerContent =
|
const workerSource =
|
||||||
platform === "node"
|
platform === "node"
|
||||||
? `export * from "./worker.node.js";`
|
? join(SRC_DIR, "worker.node.js")
|
||||||
: `export * from "./worker.browser.js";`;
|
: join(SRC_DIR, "worker.browser.js");
|
||||||
|
|
||||||
const workerPath = join(SRC_DIR, "worker.js");
|
const workerTarget = join(SRC_DIR, "worker.js");
|
||||||
await writeFile(workerPath, workerContent);
|
const workerContent = await readFile(workerSource, "utf8");
|
||||||
|
await writeFile(workerTarget, workerContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function buildAndCopy(buildType) {
|
async function buildAndCopy(buildType) {
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
"name": "@terrastruct/d2",
|
"name": "@terrastruct/d2",
|
||||||
"author": "Terrastruct, Inc.",
|
"author": "Terrastruct, Inc.",
|
||||||
"description": "D2.js is a wrapper around the WASM build of D2, the modern text-to-diagram language.",
|
"description": "D2.js is a wrapper around the WASM build of D2, the modern text-to-diagram language.",
|
||||||
"version": "0.1.17",
|
"version": "0.1.19",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/terrastruct/d2.git",
|
"url": "git+https://github.com/terrastruct/d2.git",
|
||||||
|
|
|
||||||
|
|
@ -54,10 +54,7 @@ export class D2 {
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.worker.onerror = (error) => {
|
this.worker.onerror = (error) => {
|
||||||
console.error("Worker detailed error:", error);
|
console.error("Worker encountered an error:", error.message || error);
|
||||||
console.error("Error message:", error.message);
|
|
||||||
console.error("Error filename:", error.filename);
|
|
||||||
console.error("Error lineno:", error.lineno);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,34 +18,12 @@ export async function createWorker() {
|
||||||
);
|
);
|
||||||
let workerScript = await response.text();
|
let workerScript = await response.text();
|
||||||
|
|
||||||
// Create global Go without IIFE in module context
|
let blob = new Blob([wasmExecJs, workerScript], {
|
||||||
let blob = new Blob(
|
|
||||||
[
|
|
||||||
// First establish Go in global scope
|
|
||||||
wasmExecJs,
|
|
||||||
// Then the module code
|
|
||||||
workerScript,
|
|
||||||
],
|
|
||||||
{
|
|
||||||
type: "text/javascript;charset=utf-8",
|
type: "text/javascript;charset=utf-8",
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
|
||||||
console.log("about to create worker");
|
|
||||||
const worker = new Worker(URL.createObjectURL(blob), {
|
const worker = new Worker(URL.createObjectURL(blob), {
|
||||||
type: "module",
|
type: "module",
|
||||||
});
|
});
|
||||||
console.log("worker", worker);
|
|
||||||
|
|
||||||
// Add error handler to see initialization errors
|
|
||||||
worker.onerror = (error) => {
|
|
||||||
console.error("Worker initialization error:", {
|
|
||||||
message: error.message,
|
|
||||||
filename: error.filename,
|
|
||||||
lineno: error.lineno,
|
|
||||||
error: error.error,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
return worker;
|
return worker;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,4 +7,4 @@ async function initWasmBrowser(wasmBinary) {
|
||||||
return self.d2;
|
return self.d2;
|
||||||
}
|
}
|
||||||
|
|
||||||
setupMessageHandler(self, initWasmBrowser);
|
setupMessageHandler(false, self, initWasmBrowser);
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
export * from "./worker.node.js";
|
// Replaced at build time
|
||||||
|
|
|
||||||
|
|
@ -8,4 +8,4 @@ async function initWasmNode(wasmBinary) {
|
||||||
return global.d2;
|
return global.d2;
|
||||||
}
|
}
|
||||||
|
|
||||||
setupMessageHandler(parentPort, initWasmNode);
|
setupMessageHandler(true, parentPort, initWasmNode);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
let currentPort;
|
let currentPort;
|
||||||
let d2;
|
let d2;
|
||||||
|
|
||||||
export function setupMessageHandler(port, initWasm) {
|
export function setupMessageHandler(isNode, port, initWasm) {
|
||||||
currentPort = port;
|
currentPort = port;
|
||||||
|
|
||||||
const handleMessage = async (e) => {
|
const handleMessage = async (e) => {
|
||||||
|
|
@ -10,6 +10,9 @@ export function setupMessageHandler(port, initWasm) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "init":
|
case "init":
|
||||||
try {
|
try {
|
||||||
|
if (isNode) {
|
||||||
|
eval(data.wasmExecContent);
|
||||||
|
}
|
||||||
d2 = await initWasm(data.wasm);
|
d2 = await initWasm(data.wasm);
|
||||||
currentPort.postMessage({ type: "ready" });
|
currentPort.postMessage({ type: "ready" });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
@ -41,7 +44,7 @@ export function setupMessageHandler(port, initWasm) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (typeof process !== "undefined" && process.release?.name === "node") {
|
if (isNode) {
|
||||||
port.on("message", handleMessage);
|
port.on("message", handleMessage);
|
||||||
} else {
|
} else {
|
||||||
port.onmessage = (e) => handleMessage(e.data);
|
port.onmessage = (e) => handleMessage(e.data);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue