@tscircuit/eval 0.0.329 → 0.0.331

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tscircuit/eval",
3
3
  "main": "dist/lib/index.js",
4
- "version": "0.0.329",
4
+ "version": "0.0.331",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "build": "bun run build:lib && bun run build:webworker && bun run build:blob-url && bun run build:runner && bun run build:worker-wrapper",
@@ -50,14 +50,13 @@
50
50
  }
51
51
  },
52
52
  "devDependencies": {
53
- "@babel/standalone": "^7.28.0",
54
53
  "@biomejs/biome": "^1.8.3",
55
54
  "@playwright/test": "^1.50.1",
56
55
  "@tscircuit/capacity-autorouter": "^0.0.107",
57
56
  "@tscircuit/checks": "^0.0.75",
58
57
  "@tscircuit/circuit-json-flex": "^0.0.3",
59
58
  "@tscircuit/circuit-json-util": "^0.0.67",
60
- "@tscircuit/core": "^0.0.738",
59
+ "@tscircuit/core": "^0.0.739",
61
60
  "@tscircuit/footprinter": "^0.0.236",
62
61
  "@tscircuit/import-snippet": "^0.0.4",
63
62
  "@tscircuit/infgrid-ijump-astar": "^0.0.33",
@@ -70,9 +69,9 @@
70
69
  "@tscircuit/props": "0.0.335",
71
70
  "@tscircuit/schematic-autolayout": "^0.0.6",
72
71
  "@tscircuit/schematic-match-adapt": "^0.0.16",
73
- "@tscircuit/schematic-trace-solver": "^0.0.37",
72
+ "@tscircuit/schematic-trace-solver": "^0.0.38",
74
73
  "@tscircuit/simple-3d-svg": "^0.0.41",
75
- "@types/babel__standalone": "^7.1.9",
74
+ "sucrase": "^3.35.0",
76
75
  "@types/bun": "^1.2.16",
77
76
  "@types/debug": "^4.1.12",
78
77
  "@types/react": "^19.1.8",
@@ -12,7 +12,7 @@ export default defineConfig({
12
12
  "@tscircuit/core",
13
13
  "circuit-json",
14
14
  "@tscircuit/parts-engine",
15
- "@babel/standalone",
15
+ "sucrase",
16
16
  "@tscircuit/math-utils",
17
17
  "zod",
18
18
  ],
@@ -1,6 +1,4 @@
1
- import { evalCompiledJs } from "./eval-compiled-js"
2
1
  import type { ExecutionContext } from "./execution-context"
3
- import * as Babel from "@babel/standalone"
4
2
  import { importLocalFile } from "./import-local-file"
5
3
  import { importSnippet } from "./import-snippet"
6
4
  import { resolveFilePath } from "lib/runner/resolveFilePath"
@@ -1,4 +1,3 @@
1
- import * as Babel from "@babel/standalone"
2
1
  import {
3
2
  resolveFilePath,
4
3
  resolveFilePathOrThrow,
@@ -10,6 +9,7 @@ import type { ExecutionContext } from "./execution-context"
10
9
  import { importEvalPath } from "./import-eval-path"
11
10
  import Debug from "debug"
12
11
  import { isStaticAssetPath } from "lib/shared/static-asset-extensions"
12
+ import { transformWithSucrase } from "./transform-with-sucrase"
13
13
 
14
14
  const debug = Debug("tsci:eval:import-local-file")
15
15
 
@@ -57,23 +57,14 @@ export const importLocalFile = async (
57
57
  }
58
58
  }
59
59
 
60
- const result = Babel.transform(fileContent, {
61
- presets: ["react", "typescript"],
62
- plugins: ["transform-modules-commonjs"],
63
- filename: "virtual.tsx",
64
- })
65
-
66
- if (!result || !result.code) {
67
- throw new Error("Failed to transform code")
68
- }
69
-
70
60
  try {
61
+ const transformedCode = transformWithSucrase(fileContent, fsPath)
71
62
  debug("evalCompiledJs called with:", {
72
- code: result.code?.slice(0, 100),
63
+ code: transformedCode.slice(0, 100),
73
64
  dirname: dirname(fsPath),
74
65
  })
75
66
  const importRunResult = evalCompiledJs(
76
- result.code,
67
+ transformedCode,
77
68
  preSuppliedImports,
78
69
  dirname(fsPath),
79
70
  )
@@ -89,18 +80,8 @@ export const importLocalFile = async (
89
80
  }
90
81
  } else if (fsPath.endsWith(".js")) {
91
82
  // For .js files, especially from node_modules, we need to transform them
92
- const result = Babel.transform(fileContent, {
93
- presets: ["env"],
94
- plugins: ["transform-modules-commonjs"],
95
- filename: fsPath,
96
- })
97
-
98
- if (!result || !result.code) {
99
- throw new Error("Failed to transform JS code")
100
- }
101
-
102
83
  preSuppliedImports[fsPath] = evalCompiledJs(
103
- result.code,
84
+ transformWithSucrase(fileContent, fsPath),
104
85
  preSuppliedImports,
105
86
  dirname(fsPath),
106
87
  ).exports
@@ -1,10 +1,10 @@
1
1
  import { evalCompiledJs } from "./eval-compiled-js"
2
2
  import type { ExecutionContext } from "./execution-context"
3
- import * as Babel from "@babel/standalone"
4
3
  import { dirname } from "lib/utils/dirname"
5
4
  import Debug from "debug"
6
5
  import { getImportsFromCode } from "lib/utils/get-imports-from-code"
7
6
  import { importEvalPath } from "./import-eval-path"
7
+ import { transformWithSucrase } from "./transform-with-sucrase"
8
8
 
9
9
  const debug = Debug("tsci:eval:import-npm-package")
10
10
 
@@ -58,18 +58,13 @@ export async function importNpmPackage(
58
58
  }
59
59
  }
60
60
 
61
- const transpiled = Babel.transform(content!, {
62
- presets: ["react", "env"],
63
- plugins: ["transform-modules-commonjs"],
64
- filename: importName,
65
- })
66
-
67
- if (!transpiled.code) {
68
- throw new Error(`Babel transpilation failed for ${importName}`)
69
- }
61
+ const transformedCode = transformWithSucrase(
62
+ content!,
63
+ finalImportName || importName,
64
+ )
70
65
  try {
71
66
  const exports = evalCompiledJs(
72
- transpiled.code!,
67
+ transformedCode,
73
68
  preSuppliedImports,
74
69
  cwd,
75
70
  ).exports
@@ -0,0 +1,68 @@
1
+ import { transform, type Transform as SucraseTransform } from "sucrase"
2
+
3
+ const TS_EXTENSIONS = new Set([".ts", ".tsx", ".mts", ".cts"])
4
+ const JSX_EXTENSIONS = new Set([".tsx", ".jsx"])
5
+
6
+ const stripQueryAndHash = (filePath: string) => {
7
+ const queryIndex = filePath.indexOf("?")
8
+ const hashIndex = filePath.indexOf("#")
9
+
10
+ let endIndex = filePath.length
11
+
12
+ if (queryIndex !== -1 && hashIndex !== -1) {
13
+ endIndex = Math.min(queryIndex, hashIndex)
14
+ } else if (queryIndex !== -1) {
15
+ endIndex = queryIndex
16
+ } else if (hashIndex !== -1) {
17
+ endIndex = hashIndex
18
+ }
19
+
20
+ return filePath.slice(0, endIndex)
21
+ }
22
+
23
+ const getExtension = (filePath: string) => {
24
+ const normalizedPath = stripQueryAndHash(filePath)
25
+ const lastDotIndex = normalizedPath.lastIndexOf(".")
26
+
27
+ if (lastDotIndex === -1) {
28
+ return ""
29
+ }
30
+
31
+ const lastSlashIndex = Math.max(
32
+ normalizedPath.lastIndexOf("/"),
33
+ normalizedPath.lastIndexOf("\\"),
34
+ )
35
+
36
+ if (lastSlashIndex > lastDotIndex) {
37
+ return ""
38
+ }
39
+
40
+ return normalizedPath.slice(lastDotIndex).toLowerCase()
41
+ }
42
+
43
+ const getTransformsForFilePath = (filePath: string) => {
44
+ const extension = getExtension(filePath)
45
+
46
+ const transforms: SucraseTransform[] = ["imports"]
47
+
48
+ if (TS_EXTENSIONS.has(extension)) {
49
+ transforms.unshift("typescript")
50
+ }
51
+
52
+ if (JSX_EXTENSIONS.has(extension)) {
53
+ transforms.push("jsx")
54
+ }
55
+
56
+ return transforms
57
+ }
58
+
59
+ export const transformWithSucrase = (code: string, filePath: string) => {
60
+ const transforms = getTransformsForFilePath(filePath)
61
+ const { code: transformedCode } = transform(code, {
62
+ filePath,
63
+ production: true,
64
+ transforms,
65
+ })
66
+
67
+ return transformedCode
68
+ }