Skip to content

Commit 5cdf61d

Browse files
committed
Bundle dependencies in transpile output
1 parent 39dcc0a commit 5cdf61d

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

cli/build/transpile/index.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import path from "node:path"
21
import fs from "node:fs"
2+
import { builtinModules } from "node:module"
3+
import path from "node:path"
34
import { rollup } from "rollup"
45
import typescript from "@rollup/plugin-typescript"
56
import resolve from "@rollup/plugin-node-resolve"
@@ -13,6 +14,19 @@ import {
1314
STATIC_ASSET_EXTENSIONS,
1415
} from "./static-asset-plugin"
1516

17+
const isNodeBuiltin = (id: string) => {
18+
const withoutNodePrefix = id.replace(/^node:/, "")
19+
20+
return (
21+
builtinModules.includes(id) ||
22+
builtinModules.includes(withoutNodePrefix) ||
23+
builtinModules.includes(`node:${withoutNodePrefix}`)
24+
)
25+
}
26+
27+
const isTscircuitDependency = (id: string) =>
28+
id === "tscircuit" || id.startsWith("tscircuit/")
29+
1630
const createExternalFunction =
1731
(projectDir: string, tsconfigPath?: string) =>
1832
(id: string): boolean => {
@@ -71,8 +85,18 @@ const createExternalFunction =
7185
return false // This is a local file, don't externalize
7286
}
7387

74-
// Everything else (npm packages like 'react', 'tscircuit', etc.) is external
75-
return true
88+
// Keep Node.js built-ins external
89+
if (isNodeBuiltin(id)) {
90+
return true
91+
}
92+
93+
// Always externalize tscircuit so it's not bundled with user output
94+
if (isTscircuitDependency(id)) {
95+
return true
96+
}
97+
98+
// Bundle all other dependencies into the output
99+
return false
76100
}
77101

78102
export const transpileFile = async ({
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { test, expect } from "bun:test"
2+
import { readFile, symlink, writeFile } from "node:fs/promises"
3+
import path from "node:path"
4+
import { getCliTestFixture } from "../../fixtures/get-cli-test-fixture"
5+
6+
test("transpile bundles dependencies except for tscircuit", async () => {
7+
const { tmpDir, runCommand } = await getCliTestFixture()
8+
const circuitPath = path.join(tmpDir, "with-deps.ts")
9+
10+
await writeFile(
11+
circuitPath,
12+
`import kleur from "kleur"
13+
import * as tscircuit from "tscircuit"
14+
15+
export const coloredText = kleur.red("ok")
16+
export const usesTscircuit = () =>
17+
typeof tscircuit === "object" ? "tscircuit-present" : "missing"
18+
`,
19+
)
20+
21+
await writeFile(path.join(tmpDir, "package.json"), "{}")
22+
23+
await symlink(
24+
path.resolve(process.cwd(), "node_modules"),
25+
path.join(tmpDir, "node_modules"),
26+
"dir",
27+
)
28+
29+
await runCommand(`tsci transpile ${circuitPath}`)
30+
31+
const esmPath = path.join(tmpDir, "dist", "index.js")
32+
const esmContent = await readFile(esmPath, "utf-8")
33+
34+
expect(esmContent).toMatch(/from ['\"]tscircuit['\"]/)
35+
expect(esmContent).not.toContain('from "kleur"')
36+
expect(esmContent).not.toContain('require("kleur")')
37+
expect(esmContent).toContain("tscircuit-present")
38+
}, 30_000)

0 commit comments

Comments
 (0)