Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions cli/build/transpile/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from "node:path"
import fs from "node:fs"
import { builtinModules } from "node:module"
import path from "node:path"
import { rollup } from "rollup"
import typescript from "@rollup/plugin-typescript"
import resolve from "@rollup/plugin-node-resolve"
Expand All @@ -13,6 +14,19 @@ import {
STATIC_ASSET_EXTENSIONS,
} from "./static-asset-plugin"

const isNodeBuiltin = (id: string) => {
const withoutNodePrefix = id.replace(/^node:/, "")

return (
builtinModules.includes(id) ||
builtinModules.includes(withoutNodePrefix) ||
builtinModules.includes(`node:${withoutNodePrefix}`)
)
}

const isTscircuitDependency = (id: string) =>
id === "tscircuit" || id.startsWith("tscircuit/")

const createExternalFunction =
(projectDir: string, tsconfigPath?: string) =>
(id: string): boolean => {
Expand Down Expand Up @@ -71,8 +85,18 @@ const createExternalFunction =
return false // This is a local file, don't externalize
}

// Everything else (npm packages like 'react', 'tscircuit', etc.) is external
return true
// Keep Node.js built-ins external
if (isNodeBuiltin(id)) {
return true
}

// Always externalize tscircuit so it's not bundled with user output
if (isTscircuitDependency(id)) {
return true
}

// Bundle all other dependencies into the output
return false
}

export const transpileFile = async ({
Expand Down
38 changes: 38 additions & 0 deletions tests/cli/transpile/transpile-dependencies.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { test, expect } from "bun:test"
import { readFile, symlink, writeFile } from "node:fs/promises"
import path from "node:path"
import { getCliTestFixture } from "../../fixtures/get-cli-test-fixture"

test("transpile bundles dependencies except for tscircuit", async () => {
const { tmpDir, runCommand } = await getCliTestFixture()
const circuitPath = path.join(tmpDir, "with-deps.ts")

await writeFile(
circuitPath,
`import kleur from "kleur"
import * as tscircuit from "tscircuit"

export const coloredText = kleur.red("ok")
export const usesTscircuit = () =>
typeof tscircuit === "object" ? "tscircuit-present" : "missing"
`,
)

await writeFile(path.join(tmpDir, "package.json"), "{}")

await symlink(
path.resolve(process.cwd(), "node_modules"),
path.join(tmpDir, "node_modules"),
"dir",
)

await runCommand(`tsci transpile ${circuitPath}`)

const esmPath = path.join(tmpDir, "dist", "index.js")
const esmContent = await readFile(esmPath, "utf-8")

expect(esmContent).toMatch(/from ['\"]tscircuit['\"]/)
expect(esmContent).not.toContain('from "kleur"')
expect(esmContent).not.toContain('require("kleur")')
expect(esmContent).toContain("tscircuit-present")
}, 30_000)
Loading