Skip to content

Commit 0818671

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 0818671

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

Makefile

Lines changed: 6 additions & 2 deletions
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)
@@ -128,16 +128,18 @@ help: ## Show this help message
128128
## Development
129129
ifeq ($(OS),Windows_NT)
130130
dev: node_modules/.installed build-main ## Start development server (Vite + nodemon watcher for Windows compatibility)
131-
@echo "Starting dev mode (2 watchers: nodemon for main process, vite for renderer)..."
131+
@echo "Starting dev mode (3 watchers: nodemon for main process, esbuild for api, vite for renderer)..."
132132
# On Windows, use npm run because bunx doesn't correctly pass arguments to concurrently
133133
# https://github.com/oven-sh/bun/issues/18275
134134
@NODE_OPTIONS="--max-old-space-size=4096" npm x concurrently -k --raw \
135135
"bun x nodemon --watch src --watch tsconfig.main.json --watch tsconfig.json --ext ts,tsx,json --ignore dist --ignore node_modules --exec node scripts/build-main-watch.js" \
136+
"npx esbuild src/cli/api.ts --bundle --format=esm --platform=node --outfile=dist/cli/api.mjs --external:zod --external:commander --external:@trpc/server --watch" \
136137
"vite"
137138
else
138139
dev: node_modules/.installed build-main build-preload ## Start development server (Vite + tsgo watcher for 10x faster type checking)
139140
@bun x concurrently -k \
140141
"bun x concurrently \"$(TSGO) -w -p tsconfig.main.json\" \"bun x tsc-alias -w -p tsconfig.main.json\"" \
142+
"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" \
141143
"vite"
142144
endif
143145

@@ -151,6 +153,7 @@ dev-server: node_modules/.installed build-main ## Start server mode with hot rel
151153
@# On Windows, use npm run because bunx doesn't correctly pass arguments
152154
@npmx concurrently -k \
153155
"npmx nodemon --watch src --watch tsconfig.main.json --watch tsconfig.json --ext ts,tsx,json --ignore dist --ignore node_modules --exec node scripts/build-main-watch.js" \
156+
"npx esbuild src/cli/api.ts --bundle --format=esm --platform=node --outfile=dist/cli/api.mjs --external:zod --external:commander --external:@trpc/server --watch" \
154157
"npmx nodemon --watch dist/cli/index.js --watch dist/cli/server.js --delay 500ms --exec \"node dist/cli/index.js server --host $(or $(BACKEND_HOST),localhost) --port $(or $(BACKEND_PORT),3000)\"" \
155158
"$(SHELL) -lc \"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\""
156159
else
@@ -162,6 +165,7 @@ dev-server: node_modules/.installed build-main ## Start server mode with hot rel
162165
@echo "For remote access: make dev-server VITE_HOST=0.0.0.0 BACKEND_HOST=0.0.0.0"
163166
@bun x concurrently -k \
164167
"bun x concurrently \"$(TSGO) -w -p tsconfig.main.json\" \"bun x tsc-alias -w -p tsconfig.main.json\"" \
168+
"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" \
165169
"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)'" \
166170
"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"
167171
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)