Skip to content

Commit f56f36d

Browse files
committed
fix(build): windows node pathconv + idempotent compat header
- convert bun build paths to Windows-native via cygpath so the bundle actually lands on disk when MSYS_NO_PATHCONV is set - make Step 3 compat-header injection idempotent (no re-duplicate on rebuild) - drop the duplicate createRequire import (reuse bundle's own) to fix 'Identifier createRequire has already been declared' on Node 25
1 parent aeeacf1 commit f56f36d

1 file changed

Lines changed: 33 additions & 20 deletions

File tree

browse/scripts/build-node-server.sh

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,31 @@ GSTACK_DIR="$(cd "$(dirname "$0")/../.." && pwd)"
1111
SRC_DIR="$GSTACK_DIR/browse/src"
1212
DIST_DIR="$GSTACK_DIR/browse/dist"
1313

14+
mkdir -p "$DIST_DIR"
15+
16+
# On MSYS2/git-bash, native binaries (bun) fail to write when given a POSIX
17+
# /c/Users/... path if MSYS_NO_PATHCONV=1 is set (bun reports "Bundled" but
18+
# writes nothing). Always hand bun a Windows-native path. MSYS tools
19+
# (perl/cat/cp) keep using the POSIX path, which resolves to the same file via
20+
# the /c -> C: mount, so the two never diverge.
21+
if command -v cygpath >/dev/null 2>&1; then
22+
SRC_W="$(cygpath -w -a "$SRC_DIR")\\server.ts"
23+
DIST_W="$(cygpath -w -a "$DIST_DIR")"
24+
RAW_W="$DIST_W\\server-node.raw.mjs"
25+
else
26+
SRC_W="$SRC_DIR/server.ts"
27+
DIST_W="$DIST_DIR"
28+
RAW_W="$DIST_DIR/server-node.raw.mjs"
29+
fi
30+
RAW="$DIST_DIR/server-node.raw.mjs"
31+
FINAL="$DIST_DIR/server-node.mjs"
32+
1433
echo "Building Node-compatible server bundle..."
1534

16-
# Step 1: Transpile server.ts to a single .mjs bundle (externalize runtime deps)
17-
#
18-
# Externalize packages with native addons, dynamic imports, or runtime resolution.
19-
# If you add a new dependency that uses `await import()` or has a .node addon,
20-
# add it here. Otherwise `bun build --outfile` will fail with
21-
# "cannot write multiple output files without an output directory".
22-
bun build "$SRC_DIR/server.ts" \
35+
# Step 1: bundle to a fresh raw file (no compat header yet)
36+
bun build "$SRC_W" \
2337
--target=node \
24-
--outfile "$DIST_DIR/server-node.mjs" \
38+
--outfile "$RAW_W" \
2539
--external playwright \
2640
--external playwright-core \
2741
--external diff \
@@ -30,27 +44,26 @@ bun build "$SRC_DIR/server.ts" \
3044
--external socks \
3145
--external sharp
3246

33-
# Step 2: Post-process
34-
# Replace import.meta.dir with a resolvable reference
35-
perl -pi -e 's/import\.meta\.dir/__browseNodeSrcDir/g' "$DIST_DIR/server-node.mjs"
36-
# Stub out bun:sqlite (macOS-only cookie import, not needed on Windows)
37-
perl -pi -e 's|import { Database } from "bun:sqlite";|const Database = null; // bun:sqlite stubbed on Node|g' "$DIST_DIR/server-node.mjs"
47+
# Step 2: post-process the raw bundle
48+
perl -pi -e 's/import\.meta\.dir/__browseNodeSrcDir/g' "$RAW"
49+
perl -pi -e 's|import { Database } from "bun:sqlite";|const Database = null; // bun:sqlite stubbed on Node|g' "$RAW"
3850

39-
# Step 3: Create the final file with polyfill header injected after the first line
51+
# Step 3: prepend the Windows compat header. Idempotent by construction: the
52+
# raw bundle is rebuilt from source every run (0 headers), so the header is
53+
# added exactly once — no duplicate-identifier errors on re-runs.
4054
{
41-
head -1 "$DIST_DIR/server-node.mjs"
4255
echo '// ── Windows Node.js compatibility (auto-generated) ──'
4356
echo 'import { fileURLToPath as _ftp } from "node:url";'
4457
echo 'import { dirname as _dn } from "node:path";'
4558
echo 'const __browseNodeSrcDir = _dn(_dn(_ftp(import.meta.url))) + "/src";'
4659
echo '{ const _r = createRequire(import.meta.url); _r("./bun-polyfill.cjs"); }'
4760
echo '// ── end compatibility ──'
48-
tail -n +2 "$DIST_DIR/server-node.mjs"
49-
} > "$DIST_DIR/server-node.tmp.mjs"
61+
cat "$RAW"
62+
} > "$FINAL"
5063

51-
mv "$DIST_DIR/server-node.tmp.mjs" "$DIST_DIR/server-node.mjs"
64+
rm -f "$RAW"
5265

53-
# Step 4: Copy polyfill to dist/
66+
# Step 4: copy polyfill to dist/
5467
cp "$SRC_DIR/bun-polyfill.cjs" "$DIST_DIR/bun-polyfill.cjs"
5568

56-
echo "Node server bundle ready: $DIST_DIR/server-node.mjs"
69+
echo "Node server bundle ready: $FINAL"

0 commit comments

Comments
 (0)