Skip to content

Commit 886d917

Browse files
committed
fix: build api CLI as ESM to support trpc-cli
trpc-cli requires ESM with top-level await, which can't be loaded via CommonJS require(). Bundle api.ts separately using esbuild as ESM (.mjs) and use native import() via Function constructor to bypass TypeScript's conversion to require(). Also adds `make mux` target for convenient CLI invocation.
1 parent 1dc4f78 commit 886d917

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ rebuild-native: node_modules/.installed ## Rebuild native modules (node-pty) for
109109

110110
# Run compiled CLI with trailing arguments (builds only if missing)
111111
mux: ## Run the compiled mux CLI (e.g., make mux server --port 3000)
112-
@test -f dist/cli/index.js || $(MAKE) build-main
112+
@test -f dist/cli/index.js -a -f dist/cli/api.mjs || $(MAKE) build-main
113113
@node dist/cli/index.js $(filter-out $@,$(MAKECMDGOALS))
114114

115115
# Catch unknown targets passed to mux (prevents "No rule to make target" errors)
@@ -138,6 +138,7 @@ else
138138
dev: node_modules/.installed build-main build-preload ## Start development server (Vite + tsgo watcher for 10x faster type checking)
139139
@bun x concurrently -k \
140140
"bun x concurrently \"$(TSGO) -w -p tsconfig.main.json\" \"bun x tsc-alias -w -p tsconfig.main.json\"" \
141+
"bun x esbuild src/cli/api.ts --bundle --format=esm --platform=node --outfile=dist/cli/api.mjs --external:zod --external:commander --external:@trpc/server --watch" \
141142
"vite"
142143
endif
143144

@@ -162,6 +163,7 @@ dev-server: node_modules/.installed build-main ## Start server mode with hot rel
162163
@echo "For remote access: make dev-server VITE_HOST=0.0.0.0 BACKEND_HOST=0.0.0.0"
163164
@bun x concurrently -k \
164165
"bun x concurrently \"$(TSGO) -w -p tsconfig.main.json\" \"bun x tsc-alias -w -p tsconfig.main.json\"" \
166+
"bun x esbuild src/cli/api.ts --bundle --format=esm --platform=node --outfile=dist/cli/api.mjs --external:zod --external:commander --external:@trpc/server --watch" \
165167
"bun x nodemon --watch dist/cli/index.js --watch dist/cli/server.js --delay 500ms --exec 'NODE_ENV=development node dist/cli/index.js server --host $(or $(BACKEND_HOST),localhost) --port $(or $(BACKEND_PORT),3000)'" \
166168
"MUX_VITE_HOST=$(or $(VITE_HOST),127.0.0.1) MUX_VITE_PORT=$(or $(VITE_PORT),5173) VITE_BACKEND_URL=http://$(or $(BACKEND_HOST),localhost):$(or $(BACKEND_PORT),3000) vite"
167169
endif

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@
196196
},
197197
"files": [
198198
"dist/**/*.js",
199+
"dist/**/*.mjs",
199200
"dist/**/*.js.map",
200201
"dist/**/*.wasm",
201202
"dist/**/*.html",

scripts/smoke-test.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ fi
108108

109109
log_info "✅ mux binary found"
110110

111+
# Test that mux api subcommand works (requires ESM bundle api.mjs)
112+
log_info "Testing mux api subcommand (ESM bundle)..."
113+
if ! node_modules/.bin/mux api --help >/dev/null 2>&1; then
114+
log_error "mux api --help failed - ESM bundle (api.mjs) may be missing from package"
115+
exit 1
116+
fi
117+
118+
log_info "✅ mux api subcommand works"
119+
111120
# Start the server in background
112121
log_info "Starting mux server on $SERVER_HOST:$SERVER_PORT..."
113122
node_modules/.bin/mux server --host "$SERVER_HOST" --port "$SERVER_PORT" >server.log 2>&1 &

0 commit comments

Comments
 (0)