-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2280 from alixander/npm-fix
separate node esm and browser esm builds
- Loading branch information
Showing
15 changed files
with
196 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ node_modules | |
.npm | ||
bun.lockb | ||
|
||
src/wasm-loader.browser.js | ||
wasm/d2.wasm | ||
dist/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,110 @@ | ||
import { build } from "bun"; | ||
import { copyFile, mkdir } from "node:fs/promises"; | ||
import { join } from "node:path"; | ||
import { copyFile, mkdir, writeFile, readFile, rm } from "node:fs/promises"; | ||
import { join, resolve } from "node:path"; | ||
|
||
await mkdir("./dist/esm", { recursive: true }); | ||
await mkdir("./dist/cjs", { recursive: true }); | ||
const __dirname = new URL(".", import.meta.url).pathname; | ||
const ROOT_DIR = resolve(__dirname); | ||
const SRC_DIR = resolve(ROOT_DIR, "src"); | ||
|
||
await rm("./dist", { recursive: true, force: true }); | ||
await mkdir("./dist/browser", { recursive: true }); | ||
await mkdir("./dist/node-esm", { recursive: true }); | ||
await mkdir("./dist/node-cjs", { recursive: true }); | ||
|
||
const wasmBinary = await readFile("./wasm/d2.wasm"); | ||
const wasmExecJs = await readFile("./wasm/wasm_exec.js", "utf8"); | ||
|
||
await writeFile( | ||
join(SRC_DIR, "wasm-loader.browser.js"), | ||
`export const wasmBinary = Uint8Array.from(atob("${Buffer.from(wasmBinary).toString( | ||
"base64" | ||
)}"), c => c.charCodeAt(0)); | ||
export const wasmExecJs = ${JSON.stringify(wasmExecJs)};` | ||
); | ||
|
||
const commonConfig = { | ||
target: "node", | ||
splitting: false, | ||
sourcemap: "external", | ||
minify: true, | ||
naming: { | ||
entry: "[dir]/[name].js", | ||
chunk: "[name]-[hash].js", | ||
asset: "[name]-[hash][ext]", | ||
}, | ||
}; | ||
|
||
async function buildAndCopy(format) { | ||
const outdir = `./dist/${format}`; | ||
async function buildPlatformFile(platform) { | ||
const platformContent = | ||
platform === "node" | ||
? `export * from "./platform.node.js";` | ||
: `export * from "./platform.browser.js";`; | ||
|
||
const platformPath = join(SRC_DIR, "platform.js"); | ||
await writeFile(platformPath, platformContent); | ||
} | ||
|
||
await build({ | ||
async function buildAndCopy(buildType) { | ||
const configs = { | ||
browser: { | ||
outdir: resolve(ROOT_DIR, "dist/browser"), | ||
format: "esm", | ||
target: "browser", | ||
platform: "browser", | ||
loader: { | ||
".js": "jsx", | ||
}, | ||
entrypoints: [ | ||
resolve(SRC_DIR, "index.js"), | ||
resolve(SRC_DIR, "worker.js"), | ||
resolve(SRC_DIR, "platform.js"), | ||
resolve(SRC_DIR, "wasm-loader.browser.js"), | ||
], | ||
}, | ||
"node-esm": { | ||
outdir: resolve(ROOT_DIR, "dist/node-esm"), | ||
format: "esm", | ||
target: "node", | ||
platform: "node", | ||
entrypoints: [ | ||
resolve(SRC_DIR, "index.js"), | ||
resolve(SRC_DIR, "worker.js"), | ||
resolve(SRC_DIR, "platform.js"), | ||
], | ||
}, | ||
"node-cjs": { | ||
outdir: resolve(ROOT_DIR, "dist/node-cjs"), | ||
format: "cjs", | ||
target: "node", | ||
platform: "node", | ||
entrypoints: [ | ||
resolve(SRC_DIR, "index.js"), | ||
resolve(SRC_DIR, "worker.js"), | ||
resolve(SRC_DIR, "platform.js"), | ||
], | ||
}, | ||
}; | ||
|
||
const config = configs[buildType]; | ||
await buildPlatformFile(config.platform); | ||
|
||
const result = await build({ | ||
...commonConfig, | ||
entrypoints: ["./src/index.js", "./src/worker.js", "./src/platform.js"], | ||
outdir, | ||
format, | ||
...config, | ||
}); | ||
|
||
await copyFile("./wasm/d2.wasm", join(outdir, "d2.wasm")); | ||
await copyFile("./wasm/wasm_exec.js", join(outdir, "wasm_exec.js")); | ||
if (!result.outputs || result.outputs.length === 0) { | ||
throw new Error(`No outputs generated for ${buildType} build`); | ||
} | ||
|
||
if (buildType !== "browser") { | ||
await copyFile(resolve(ROOT_DIR, "wasm/d2.wasm"), join(config.outdir, "d2.wasm")); | ||
await copyFile( | ||
resolve(ROOT_DIR, "wasm/wasm_exec.js"), | ||
join(config.outdir, "wasm_exec.js") | ||
); | ||
} | ||
} | ||
|
||
await buildAndCopy("esm"); | ||
await buildAndCopy("cjs"); | ||
try { | ||
await buildAndCopy("browser"); | ||
await buildAndCopy("node-esm"); | ||
await buildAndCopy("node-cjs"); | ||
} catch (error) { | ||
console.error("Build failed:", error); | ||
process.exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { wasmBinary, wasmExecJs } from "./wasm-loader.browser.js"; | ||
|
||
export async function loadFile(path) { | ||
if (path === "./d2.wasm") { | ||
return wasmBinary.buffer; | ||
} | ||
if (path === "./wasm_exec.js") { | ||
return new TextEncoder().encode(wasmExecJs).buffer; | ||
} | ||
throw new Error(`Unexpected file request: ${path}`); | ||
} | ||
|
||
export async function createWorker() { | ||
// Combine wasmExecJs with worker script | ||
const workerResponse = await fetch(new URL("./worker.js", import.meta.url)); | ||
if (!workerResponse.ok) { | ||
throw new Error( | ||
`Failed to load worker.js: ${workerResponse.status} ${workerResponse.statusText}` | ||
); | ||
} | ||
const workerJs = await workerResponse.text(); | ||
|
||
const blob = new Blob(["(() => {", wasmExecJs, "})();", workerJs], { | ||
type: "application/javascript", | ||
}); | ||
|
||
return new Worker(URL.createObjectURL(blob)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1 @@ | ||
export async function loadFile(path) { | ||
if (typeof window === "undefined") { | ||
const fs = await import("node:fs/promises"); | ||
const { fileURLToPath } = await import("node:url"); | ||
const { join, dirname } = await import("node:path"); | ||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
||
try { | ||
return await fs.readFile(join(__dirname, path)); | ||
} catch (err) { | ||
if (err.code === "ENOENT") { | ||
return await fs.readFile(join(__dirname, "../wasm", path.replace("./", ""))); | ||
} | ||
throw err; | ||
} | ||
} | ||
try { | ||
const response = await fetch(new URL(path, import.meta.url)); | ||
return await response.arrayBuffer(); | ||
} catch { | ||
const response = await fetch( | ||
new URL(`../wasm/${path.replace("./", "")}`, import.meta.url) | ||
); | ||
return await response.arrayBuffer(); | ||
} | ||
} | ||
|
||
export async function createWorker() { | ||
if (typeof window === "undefined") { | ||
const { Worker } = await import("node:worker_threads"); | ||
const { fileURLToPath } = await import("node:url"); | ||
const { join, dirname } = await import("node:path"); | ||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
return new Worker(join(__dirname, "worker.js")); | ||
} | ||
return new window.Worker(new URL("./worker.js", import.meta.url)); | ||
} | ||
export * from "./platform.node.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
let nodeModules = null; | ||
|
||
async function loadNodeModules() { | ||
if (!nodeModules) { | ||
nodeModules = { | ||
fs: await import("fs/promises"), | ||
path: await import("path"), | ||
url: await import("url"), | ||
worker: await import("worker_threads"), | ||
}; | ||
} | ||
return nodeModules; | ||
} | ||
|
||
export async function loadFile(path) { | ||
const modules = await loadNodeModules(); | ||
const readFile = modules.fs.readFile; | ||
const { join, dirname } = modules.path; | ||
const { fileURLToPath } = modules.url; | ||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
||
try { | ||
return await readFile(join(__dirname, path)); | ||
} catch (err) { | ||
if (err.code === "ENOENT") { | ||
return await readFile(join(__dirname, "../../../wasm", path.replace("./", ""))); | ||
} | ||
throw err; | ||
} | ||
} | ||
|
||
export async function createWorker() { | ||
const modules = await loadNodeModules(); | ||
const { Worker } = modules.worker; | ||
const { join, dirname } = modules.path; | ||
const { fileURLToPath } = modules.url; | ||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
const workerPath = join(__dirname, "worker.js"); | ||
return new Worker(workerPath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { readFile } from "fs/promises"; | ||
import { fileURLToPath } from "url"; | ||
import { dirname, resolve } from "path"; | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
export async function getWasmBinary() { | ||
return readFile(resolve(__dirname, "./d2.wasm")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,8 @@ if ! go version | grep -q '1.2[0-9]'; then | |
exit 1 | ||
fi | ||
|
||
if [ "${CI:-}" ]; then | ||
export FORCE_COLOR=1 | ||
npx [email protected] install --with-deps chromium | ||
fi | ||
# if [ "${CI:-}" ]; then | ||
# export FORCE_COLOR=1 | ||
# npx [email protected] install --with-deps chromium | ||
# fi | ||
_make "$@" |