Skip to content

Commit

Permalink
fix: include source map for worker.js on cjs/esm builds
Browse files Browse the repository at this point in the history
  • Loading branch information
thucpn authored and chrisdickinson committed Sep 11, 2024
1 parent b4aba72 commit bd10799
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 10 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ _build out args='[]': prepare
const path = require("path");

const args = [{
sourcemap: true,
outdir: "dist/{{ out }}",
bundle: true,
minify: true,
Expand All @@ -60,6 +59,7 @@ _build out args='[]': prepare
alias: combValue('alias'),
polyfills: combValue('polyfills'),
define: combValue('define'),
sourcemap: lastValue('sourcemap'),
}

const resolved = args.reduce((acc, xs) => {
Expand Down Expand Up @@ -95,7 +95,7 @@ _build out args='[]': prepare

return { path: result, external: true }
})
}
}
}];


Expand Down Expand Up @@ -125,6 +125,7 @@ build_worker out args='[]':
#!/bin/bash
config="$(<<<'{{ args }}' jq -cM '
[{
"sourcemap": false,
"entryPoints": ["src/worker.ts"],
"bundle": true,
"minify": true,
Expand Down Expand Up @@ -162,6 +163,7 @@ build_worker_browser out='worker/browser' args='[]':
#!/bin/bash
config="$(<<<'{{ args }}' jq -cM '
[{
"sourcemap": true,
"format": "esm",
"alias": {
"node:worker_threads": "./src/polyfills/worker-node-worker_threads.ts"
Expand All @@ -179,7 +181,8 @@ build_node_cjs out='cjs' args='[]':
#!/bin/bash
config="$(<<<'{{ args }}' jq -cM '
[{
"entryPoints": ["src/mod.ts"],
"sourcemap": true,
"entryPoints": ["src/mod.ts", "src/worker.ts"],
"platform": "node",
"minify": false,
"polyfills": {
Expand Down Expand Up @@ -209,7 +212,8 @@ build_node_esm out='esm' args='[]':
#!/bin/bash
config="$(<<<'{{ args }}' jq -cM '
[{
"entryPoints": ["src/mod.ts"],
"sourcemap": true,
"entryPoints": ["src/mod.ts", "src/worker.ts"],
"platform": "node",
"format": "esm",
"minify": false,
Expand All @@ -229,6 +233,7 @@ build_bun out='bun' args='[]':
#!/bin/bash
config="$(<<<'{{ args }}' jq -cM '
[{
"sourcemap": true,
"entryPoints": ["src/mod.ts", "src/worker.ts"],
"platform": "node",
"format": "esm",
Expand All @@ -250,6 +255,7 @@ build_browser out='browser' args='[]':
#!/bin/bash
config="$(<<<'{{ args }}' jq -cM '
[{
"sourcemap": true,
"entryPoints": ["src/mod.ts"],
"platform": "browser",
"define": {"global": "globalThis"},
Expand Down
15 changes: 13 additions & 2 deletions src/worker-url.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
// This file is aliased by esbuild for commonjs, esm, and browser builds.
const relativeUrl = (await import.meta.resolve('./worker.ts')) as string
// NB(chris): we can't do the obvious thing here (`new URL('./worker.js', import.meta.url)`.)
// Why? This file is consumed by Deno, and in Deno JSR packages `import.meta.url`
// resolves to an `http(s):` protocol. However, `http(s):` protocol URLs are not supported
// by node:worker_threads.
//
// (And oof, in order to switch from node workers to web Workers,
// we'd have to polyfill the web Worker api on top of node. It was easier to go the other way
// around.)
//
// In Node, Bun, and browser environments, this entire file is *ignored*: the esbuild config
// replaces it with a prebuilt base64'd inline javascript URL. See `build_worker_node` in
// the `justfile`.
const relativeUrl = (await (import.meta.resolve as any)('./worker.ts')) as string
export const WORKER_URL = `data:text/javascript;base64,${btoa(`
export * from ${JSON.stringify(relativeUrl)};
`)}`

0 comments on commit bd10799

Please sign in to comment.