@tscircuit/eval 0.0.362 → 0.0.364

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.
Files changed (108) hide show
  1. package/dist/blob-url.js +1 -1
  2. package/dist/webworker/entrypoint.js +120 -120
  3. package/package.json +7 -4
  4. package/.github/workflows/bun-formatcheck.yml +0 -27
  5. package/.github/workflows/bun-pver-release.yml +0 -70
  6. package/.github/workflows/bun-test.yml +0 -87
  7. package/.github/workflows/bun-typecheck.yml +0 -27
  8. package/.github/workflows/playwright.yml +0 -40
  9. package/.github/workflows/update-deps.yml +0 -47
  10. package/.github/workflows/update-tscircuit-core.yml +0 -59
  11. package/biome.json +0 -50
  12. package/browser-tests/browser.test.ts +0 -15
  13. package/browser-tests/browsertest.html +0 -15
  14. package/browser-tests/browsertest.ts +0 -53
  15. package/bunfig.toml +0 -6
  16. package/lib/getPlatformConfig.ts +0 -41
  17. package/lib/getPossibleEntrypointComponentPaths.ts +0 -42
  18. package/lib/index.ts +0 -5
  19. package/lib/runner/CircuitRunner.ts +0 -201
  20. package/lib/runner/index.ts +0 -4
  21. package/lib/runner/normalizeFsMap.ts +0 -20
  22. package/lib/runner/resolveFilePath.ts +0 -90
  23. package/lib/runner/runTscircuitCode.ts +0 -30
  24. package/lib/runner/runTscircuitModule.ts +0 -37
  25. package/lib/runner/setupDefaultEntrypointIfNeeded.ts +0 -81
  26. package/lib/shared/static-asset-extensions.ts +0 -10
  27. package/lib/shared/types.ts +0 -79
  28. package/lib/utils/dirname.ts +0 -21
  29. package/lib/utils/get-imports-from-code.ts +0 -23
  30. package/lib/utils/index.ts +0 -1
  31. package/lib/utils/resolve-node-module.ts +0 -193
  32. package/lib/worker.ts +0 -246
  33. package/playwright.config.ts +0 -23
  34. package/scripts/build-worker-blob-url.ts +0 -27
  35. package/scripts/copy-core-versions.ts +0 -81
  36. package/scripts/validate-test-matrix.js +0 -148
  37. package/tests/circuit-runner/circuitrunner1-readme-example.test.tsx +0 -29
  38. package/tests/custom-component-with-fsmap/should-reject-invalid-main-component-path.test.ts +0 -26
  39. package/tests/custom-component-with-fsmap/should-render-multiple-components-from-fsmap.test.ts +0 -46
  40. package/tests/custom-component-with-fsmap/should-render-single-component-from-fsmap.test.ts +0 -38
  41. package/tests/examples/__snapshots__/example18-kicad-footprint-server.snap.svg +0 -1
  42. package/tests/examples/example01-readme-example.test.tsx +0 -30
  43. package/tests/examples/example02-multiple-files.test.tsx +0 -45
  44. package/tests/examples/example03-encoded-url.test.tsx +0 -28
  45. package/tests/examples/example04-root-child-issue.test.tsx +0 -37
  46. package/tests/examples/example05-event-recording.test.tsx +0 -44
  47. package/tests/examples/example06-dynamic-load-blob-url.test.tsx +0 -29
  48. package/tests/examples/example07-import-default-and-namespace.test.tsx +0 -74
  49. package/tests/examples/example08-footprinter-to220.test.tsx +0 -30
  50. package/tests/examples/example09-not-defined-component.test.tsx +0 -24
  51. package/tests/examples/example10-run-tscircuit-code.test.tsx +0 -14
  52. package/tests/examples/example11-flexible-import-extensions.test.tsx +0 -20
  53. package/tests/examples/example12-import-from-subdirectory.test.tsx +0 -26
  54. package/tests/examples/example13-webworker-without-entrypoint.test.tsx +0 -32
  55. package/tests/examples/example14-run-tscircuit-module.test.tsx +0 -20
  56. package/tests/examples/example15-run-tscircuit-module-with-props.test.tsx +0 -24
  57. package/tests/examples/example16-parts-engine.test.tsx +0 -118
  58. package/tests/examples/example17-parse-tscircuit-config.test.tsx +0 -49
  59. package/tests/examples/example18-kicad-footprint-server.test.tsx +0 -39
  60. package/tests/examples/example19-support-backwards-compat.test.tsx +0 -31
  61. package/tests/examples/example20-kicad-import.test.tsx +0 -48
  62. package/tests/features/circuit-event-forwarding.test.tsx +0 -45
  63. package/tests/features/enable-debug.test.ts +0 -26
  64. package/tests/features/execute-component-runner.test.tsx +0 -47
  65. package/tests/features/execute-component-worker.test.tsx +0 -25
  66. package/tests/features/fetch-proxy/fetch-override.test.ts +0 -46
  67. package/tests/features/fetch-proxy/fetch-proxy-validation.test.ts +0 -95
  68. package/tests/features/kill.test.ts +0 -24
  69. package/tests/features/manual-edits.test.tsx +0 -118
  70. package/tests/features/npm-import.test.tsx +0 -47
  71. package/tests/features/parent-directory-import.test.tsx +0 -26
  72. package/tests/features/platform-config.test.tsx +0 -23
  73. package/tests/features/prioritize-default-export.test.tsx +0 -27
  74. package/tests/features/project-config.test.tsx +0 -25
  75. package/tests/features/static-file-imports/static-file-import.test.ts +0 -50
  76. package/tests/fixtures/preload.ts +0 -10
  77. package/tests/fixtures/resourcePaths.ts +0 -3
  78. package/tests/node-resolution/node-module-resolution-1.test.tsx +0 -32
  79. package/tests/node-resolution/node-module-resolution-10.test.tsx +0 -30
  80. package/tests/node-resolution/node-module-resolution-11.test.tsx +0 -41
  81. package/tests/node-resolution/node-module-resolution-2.test.tsx +0 -33
  82. package/tests/node-resolution/node-module-resolution-3.test.tsx +0 -38
  83. package/tests/node-resolution/node-module-resolution-4.test.tsx +0 -29
  84. package/tests/node-resolution/node-module-resolution-5.test.tsx +0 -38
  85. package/tests/node-resolution/node-module-resolution-6.test.tsx +0 -40
  86. package/tests/node-resolution/node-module-resolution-7.test.tsx +0 -43
  87. package/tests/node-resolution/node-module-resolution-8.test.tsx +0 -32
  88. package/tests/node-resolution/node-module-resolution-9.test.tsx +0 -29
  89. package/tests/node-resolution/node-modules-resolution-12.test.tsx +0 -30
  90. package/tests/repros/group-wrapper.test.tsx +0 -18
  91. package/tests/repros/nine-keyboard-default-export.test.tsx +0 -26
  92. package/tests/util-fns/get-imports-from-code.test.tsx +0 -93
  93. package/tests/util-fns/getPossibleEntrypointComponentPaths.test.ts +0 -35
  94. package/tsconfig.json +0 -34
  95. package/tsup-lib.config.ts +0 -11
  96. package/tsup-runner.config.ts +0 -12
  97. package/tsup-webworker.config.ts +0 -21
  98. package/webworker/entrypoint.ts +0 -227
  99. package/webworker/eval-compiled-js.ts +0 -61
  100. package/webworker/execution-context.ts +0 -64
  101. package/webworker/fetchProxy.ts +0 -83
  102. package/webworker/import-eval-path.ts +0 -78
  103. package/webworker/import-local-file.ts +0 -93
  104. package/webworker/import-node-module.ts +0 -54
  105. package/webworker/import-npm-package.ts +0 -77
  106. package/webworker/import-snippet.ts +0 -30
  107. package/webworker/index.ts +0 -1
  108. package/webworker/transform-with-sucrase.ts +0 -68
@@ -1,227 +0,0 @@
1
- import type { AnyCircuitElement } from "circuit-json"
2
- import * as Comlink from "comlink"
3
- import type {
4
- InternalWebWorkerApi,
5
- WebWorkerConfiguration,
6
- } from "lib/shared/types"
7
- import * as React from "react"
8
- import type { PlatformConfig } from "@tscircuit/props"
9
- import {
10
- createExecutionContext,
11
- type ExecutionContext,
12
- } from "./execution-context"
13
- import { importEvalPath } from "./import-eval-path"
14
- import { normalizeFsMap } from "lib/runner/normalizeFsMap"
15
- import type { RootCircuit } from "@tscircuit/core"
16
- import { setupDefaultEntrypointIfNeeded } from "lib/runner/setupDefaultEntrypointIfNeeded"
17
- import { setupFetchProxy } from "./fetchProxy"
18
-
19
- globalThis.React = React
20
- setupFetchProxy()
21
-
22
- let executionContext: ExecutionContext | null = null
23
- let debugNamespace: string | undefined
24
-
25
- const circuitRunnerConfiguration: WebWorkerConfiguration = {
26
- snippetsApiBaseUrl: "https://registry-api.tscircuit.com",
27
- cjsRegistryUrl: "https://cjs.tscircuit.com",
28
- verbose: false,
29
- platform: undefined,
30
- projectConfig: undefined,
31
- }
32
-
33
- const eventListeners: Record<string, ((...args: any[]) => void)[]> = {}
34
-
35
- // Helper to deserialize React elements from cross-worker communication
36
- function deserializeReactElement(serialized: any): any {
37
- if (!serialized || typeof serialized !== "object") {
38
- return serialized
39
- }
40
-
41
- if (serialized.__isSerializedReactElement) {
42
- const props = deserializeProps(serialized.props)
43
- return React.createElement(serialized.type, props)
44
- }
45
-
46
- return serialized
47
- }
48
-
49
- function deserializeProps(props: any): any {
50
- if (!props || typeof props !== "object") {
51
- return props
52
- }
53
-
54
- const deserialized: any = {}
55
- for (const [key, value] of Object.entries(props)) {
56
- if (key === "children") {
57
- if (Array.isArray(value)) {
58
- deserialized.children = value.map(deserializeReactElement)
59
- } else {
60
- deserialized.children = deserializeReactElement(value)
61
- }
62
- } else {
63
- deserialized[key] = value
64
- }
65
- }
66
- return deserialized
67
- }
68
-
69
- function bindEventListeners(circuit: RootCircuit) {
70
- for (const event in eventListeners) {
71
- for (const listener of eventListeners[event]) {
72
- circuit.on(event as any, listener as any)
73
- }
74
- }
75
- }
76
-
77
- const webWorkerApi = {
78
- setSnippetsApiBaseUrl: async (baseUrl: string) => {
79
- circuitRunnerConfiguration.snippetsApiBaseUrl = baseUrl
80
- },
81
- setPlatformConfig: async (platform: PlatformConfig) => {
82
- circuitRunnerConfiguration.platform = platform
83
- },
84
- setProjectConfig: async (project: Partial<PlatformConfig>) => {
85
- circuitRunnerConfiguration.projectConfig = project
86
- },
87
-
88
- enableDebug: async (namespace: string) => {
89
- debugNamespace = namespace
90
- if (executionContext) {
91
- const circuit = executionContext.circuit as any
92
- circuit.enableDebug?.(namespace)
93
- }
94
- },
95
-
96
- version: async () => {
97
- return "0.0.0"
98
- },
99
-
100
- async executeWithFsMap(opts: {
101
- entrypoint?: string
102
- fsMap: Record<string, string>
103
- name?: string
104
- }): Promise<void> {
105
- if (circuitRunnerConfiguration.verbose) {
106
- console.log("[Worker] executeWithFsMap called with:", {
107
- entrypoint: opts.entrypoint,
108
- fsMapKeys: Object.keys(opts.fsMap),
109
- name: opts.name,
110
- })
111
- }
112
-
113
- setupDefaultEntrypointIfNeeded(opts)
114
-
115
- let entrypoint = opts.entrypoint!
116
-
117
- executionContext = createExecutionContext(circuitRunnerConfiguration, {
118
- name: opts.name,
119
- platform: circuitRunnerConfiguration.platform,
120
- projectConfig: circuitRunnerConfiguration.projectConfig,
121
- debugNamespace,
122
- })
123
- bindEventListeners(executionContext.circuit)
124
- executionContext.fsMap = normalizeFsMap(opts.fsMap)
125
- if (!executionContext.fsMap[entrypoint]) {
126
- throw new Error(`Entrypoint "${opts.entrypoint}" not found`)
127
- }
128
- ;(globalThis as any).__tscircuit_circuit = executionContext.circuit
129
-
130
- if (!entrypoint.startsWith("./")) {
131
- entrypoint = `./${entrypoint}`
132
- }
133
-
134
- await importEvalPath(entrypoint, executionContext)
135
- },
136
-
137
- async execute(code: string, opts: { name?: string } = {}) {
138
- if (circuitRunnerConfiguration.verbose) {
139
- console.log("[Worker] execute called with code length:", code.length)
140
- }
141
- executionContext = createExecutionContext(circuitRunnerConfiguration, {
142
- ...opts,
143
- platform: circuitRunnerConfiguration.platform,
144
- projectConfig: circuitRunnerConfiguration.projectConfig,
145
- debugNamespace,
146
- })
147
- bindEventListeners(executionContext.circuit)
148
- executionContext.fsMap["entrypoint.tsx"] = code
149
- ;(globalThis as any).__tscircuit_circuit = executionContext.circuit
150
-
151
- await importEvalPath("./entrypoint.tsx", executionContext)
152
- },
153
-
154
- async executeComponent(component: any, opts: { name?: string } = {}) {
155
- if (circuitRunnerConfiguration.verbose) {
156
- console.log("[Worker] executeComponent called")
157
- }
158
- executionContext = createExecutionContext(circuitRunnerConfiguration, {
159
- ...opts,
160
- platform: circuitRunnerConfiguration.platform,
161
- projectConfig: circuitRunnerConfiguration.projectConfig,
162
- debugNamespace,
163
- })
164
- bindEventListeners(executionContext.circuit)
165
- ;(globalThis as any).__tscircuit_circuit = executionContext.circuit
166
-
167
- let element: any
168
- if (typeof component === "function") {
169
- element = component()
170
- } else if (component && component.__isSerializedReactElement) {
171
- element = deserializeReactElement(component)
172
- } else {
173
- element = component
174
- }
175
- executionContext.circuit.add(element as any)
176
- },
177
-
178
- on: (event: string, callback: (...args: any[]) => void) => {
179
- eventListeners[event] ??= []
180
- eventListeners[event].push(callback)
181
- executionContext?.circuit.on(event as any, callback)
182
- },
183
-
184
- renderUntilSettled: async (): Promise<void> => {
185
- if (!executionContext) {
186
- throw new Error("No circuit has been created")
187
- }
188
- await executionContext.circuit.renderUntilSettled()
189
- },
190
-
191
- getCircuitJson: async (): Promise<AnyCircuitElement[]> => {
192
- if (!executionContext) {
193
- throw new Error("No circuit has been created")
194
- }
195
- return executionContext.circuit.getCircuitJson()
196
- },
197
-
198
- clearEventListeners: () => {
199
- // If there's an active circuit, try to unbind all listeners
200
- if (executionContext?.circuit) {
201
- for (const event in eventListeners) {
202
- for (const listener of eventListeners[event]) {
203
- const circuit = executionContext.circuit as unknown as {
204
- removeListener?: (
205
- event: string,
206
- listener: (...args: any[]) => void,
207
- ) => void
208
- }
209
- if (circuit.removeListener) {
210
- circuit.removeListener(event, listener)
211
- }
212
- }
213
- }
214
- }
215
-
216
- // Clear all stored event listeners
217
- for (const event in eventListeners) {
218
- delete eventListeners[event]
219
- }
220
- },
221
-
222
- // Required by InternalWebWorkerApi interface but never called
223
- // Worker termination is handled by the main thread
224
- kill: async () => {},
225
- } satisfies InternalWebWorkerApi
226
-
227
- Comlink.expose(webWorkerApi)
@@ -1,61 +0,0 @@
1
- import { resolveFilePath } from "lib/runner/resolveFilePath"
2
-
3
- export function evalCompiledJs(
4
- compiledCode: string,
5
- preSuppliedImports: Record<string, any>,
6
- cwd?: string,
7
- ) {
8
- ;(globalThis as any).__tscircuit_require = (name: string) => {
9
- const resolvedFilePath = resolveFilePath(name, preSuppliedImports, cwd)
10
-
11
- const hasResolvedFilePath =
12
- resolvedFilePath && preSuppliedImports[resolvedFilePath]
13
-
14
- if (!preSuppliedImports[name] && !hasResolvedFilePath) {
15
- throw new Error(`Import "${name}" not found ${cwd ? `in "${cwd}"` : ""}`)
16
- }
17
-
18
- const mod =
19
- preSuppliedImports[name] || preSuppliedImports[resolvedFilePath!]
20
- return new Proxy(mod, {
21
- get(target, prop) {
22
- if (!(prop in target)) {
23
- if (prop === "default") {
24
- if (target.default !== undefined) {
25
- return target.default
26
- }
27
-
28
- if (target.__esModule) {
29
- return undefined
30
- }
31
-
32
- if (typeof target === "function" || typeof target === "object") {
33
- return target
34
- }
35
-
36
- return undefined
37
- }
38
-
39
- if (prop === "__esModule") {
40
- return true
41
- }
42
-
43
- throw new Error(
44
- `Component "${String(prop)}" is not exported by "${name}"`,
45
- )
46
- }
47
-
48
- return target[prop as keyof typeof target]
49
- },
50
- })
51
- }
52
-
53
- const functionBody = `
54
- var exports = {};
55
- var require = globalThis.__tscircuit_require;
56
- var module = { exports };
57
- var circuit = globalThis.__tscircuit_circuit;
58
- ${compiledCode};
59
- return module;`.trim()
60
- return Function(functionBody).call(globalThis)
61
- }
@@ -1,64 +0,0 @@
1
- import { RootCircuit } from "@tscircuit/core"
2
- import type { WebWorkerConfiguration } from "lib/shared/types"
3
- import * as tscircuitCore from "@tscircuit/core"
4
- import * as React from "react"
5
- import * as tscircuitMathUtils from "@tscircuit/math-utils"
6
- import type { PlatformConfig } from "@tscircuit/props"
7
- import { getPlatformConfig } from "lib/getPlatformConfig"
8
- import Debug from "debug"
9
-
10
- const debug = Debug("tsci:eval:execution-context")
11
-
12
- export interface ExecutionContext extends WebWorkerConfiguration {
13
- fsMap: Record<string, string>
14
- entrypoint: string
15
- preSuppliedImports: Record<string, any>
16
- circuit: RootCircuit
17
- }
18
-
19
- export function createExecutionContext(
20
- webWorkerConfiguration: WebWorkerConfiguration,
21
- opts: {
22
- name?: string
23
- platform?: PlatformConfig
24
- projectConfig?: Partial<PlatformConfig>
25
- debugNamespace?: string
26
- } = {},
27
- ): ExecutionContext {
28
- globalThis.React = React
29
-
30
- const basePlatform = opts.platform || getPlatformConfig()
31
- const platform = opts.projectConfig
32
- ? { ...basePlatform, ...opts.projectConfig }
33
- : basePlatform
34
-
35
- const circuit = new RootCircuit({
36
- platform,
37
- })
38
-
39
- if (opts.name) {
40
- circuit.name = opts.name
41
- }
42
-
43
- if (opts.debugNamespace) {
44
- circuit.enableDebug(opts.debugNamespace)
45
- }
46
-
47
- return {
48
- fsMap: {},
49
- entrypoint: "",
50
- preSuppliedImports: {
51
- "@tscircuit/core": tscircuitCore,
52
- tscircuit: tscircuitCore,
53
- "@tscircuit/math-utils": tscircuitMathUtils,
54
- react: React,
55
- debug: Debug,
56
-
57
- // This is usually used as a type import, we can remove the shim when we
58
- // ignore type imports in getImportsFromCode
59
- "@tscircuit/props": {},
60
- },
61
- circuit,
62
- ...webWorkerConfiguration,
63
- }
64
- }
@@ -1,83 +0,0 @@
1
- export const setupFetchProxy = () => {
2
- const pendingRequests = new Map<
3
- number,
4
- { resolve: (value: Response) => void; reject: (reason: any) => void }
5
- >()
6
- let requestCounter = 0
7
-
8
- function fetchProxy(
9
- input: RequestInfo | URL,
10
- init?: RequestInit,
11
- ): Promise<Response> {
12
- const requestId = ++requestCounter
13
- return new Promise((resolve, reject) => {
14
- pendingRequests.set(requestId, { resolve, reject })
15
- let url: string
16
- let requestInit: any = init ? { ...init } : {}
17
-
18
- if (typeof input === "string" || input instanceof URL) {
19
- url = input.toString()
20
- } else {
21
- url = input.url
22
- requestInit = {
23
- ...requestInit,
24
- method: input.method,
25
- headers: (() => {
26
- const obj: Record<string, string> = {}
27
- input.headers.forEach((value, key) => {
28
- obj[key] = value
29
- })
30
- return obj
31
- })(),
32
- body: input.bodyUsed ? undefined : (input as any).body,
33
- }
34
- }
35
-
36
- if (requestInit.headers instanceof Headers) {
37
- const obj: Record<string, string> = {}
38
- requestInit.headers.forEach((value: string, key: string) => {
39
- obj[key] = value
40
- })
41
- requestInit.headers = obj
42
- }
43
- ;(globalThis as any).postMessage({
44
- type: "worker_fetch",
45
- requestId,
46
- input: url,
47
- init: requestInit,
48
- })
49
- })
50
- }
51
-
52
- function handleMessage(event: MessageEvent) {
53
- const data = event.data
54
- if (!data) return
55
-
56
- if (data.type === "override_global_fetch") {
57
- ;(globalThis as any).fetch = fetchProxy
58
- return
59
- }
60
-
61
- if (data.type === "worker_fetch_result") {
62
- const handlers = pendingRequests.get(data.requestId)
63
- if (!handlers) return
64
- pendingRequests.delete(data.requestId)
65
-
66
- if (data.success) {
67
- const resp = new Response(data.response.body, {
68
- status: data.response.status,
69
- statusText: data.response.statusText,
70
- headers: data.response.headers,
71
- })
72
- handlers.resolve(resp)
73
- } else {
74
- const err = new Error(data.error.message)
75
- if (data.error.name) err.name = data.error.name
76
- if (data.error.stack) err.stack = data.error.stack
77
- handlers.reject(err)
78
- }
79
- }
80
- }
81
-
82
- globalThis.addEventListener("message", handleMessage)
83
- }
@@ -1,78 +0,0 @@
1
- import type { ExecutionContext } from "./execution-context"
2
- import { importLocalFile } from "./import-local-file"
3
- import { importSnippet } from "./import-snippet"
4
- import { resolveFilePath } from "lib/runner/resolveFilePath"
5
- import { resolveNodeModule } from "lib/utils/resolve-node-module"
6
- import { importNodeModule } from "./import-node-module"
7
- import { importNpmPackage } from "./import-npm-package"
8
- import Debug from "debug"
9
-
10
- const debug = Debug("tsci:eval:import-eval-path")
11
-
12
- export async function importEvalPath(
13
- importName: string,
14
- ctx: ExecutionContext,
15
- depth = 0,
16
- opts: {
17
- cwd?: string
18
- } = {},
19
- ) {
20
- debug("importEvalPath called with:", {
21
- importName,
22
- depth,
23
- opts,
24
- })
25
-
26
- debug(`${" ".repeat(depth)}➡️`, importName)
27
- const { preSuppliedImports } = ctx
28
-
29
- if (preSuppliedImports[importName]) return
30
- if (importName.startsWith("./") && preSuppliedImports[importName.slice(2)])
31
- return
32
-
33
- if (depth > 5) {
34
- console.log("Max depth for imports reached")
35
- return
36
- }
37
-
38
- if (importName.startsWith("/npm/")) {
39
- const pkgName = importName.replace(/^\/npm\//, "").replace(/\/\+esm$/, "")
40
- await importNpmPackage(pkgName, ctx, depth)
41
- const pkg = preSuppliedImports[pkgName]
42
- if (pkg) {
43
- preSuppliedImports[importName] = pkg
44
- }
45
- return
46
- }
47
-
48
- const resolvedLocalImportPath = resolveFilePath(
49
- importName,
50
- ctx.fsMap,
51
- opts.cwd,
52
- )
53
- if (resolvedLocalImportPath) {
54
- return importLocalFile(resolvedLocalImportPath, ctx, depth)
55
- }
56
-
57
- // Try to resolve from node_modules
58
- const resolvedNodeModulePath = resolveNodeModule(
59
- importName,
60
- ctx.fsMap,
61
- opts.cwd || "",
62
- )
63
- if (resolvedNodeModulePath) {
64
- return importNodeModule(importName, ctx, depth)
65
- }
66
-
67
- if (importName.startsWith("@tsci/")) {
68
- return importSnippet(importName, ctx, depth)
69
- }
70
-
71
- if (!importName.startsWith(".") && !importName.startsWith("/")) {
72
- return importNpmPackage(importName, ctx, depth)
73
- }
74
-
75
- throw new Error(
76
- `Unresolved import "${importName}" ${opts.cwd ? `from directory "${opts.cwd}"` : ""}`,
77
- )
78
- }
@@ -1,93 +0,0 @@
1
- import {
2
- resolveFilePath,
3
- resolveFilePathOrThrow,
4
- } from "lib/runner/resolveFilePath"
5
- import { dirname } from "lib/utils/dirname"
6
- import { getImportsFromCode } from "lib/utils/get-imports-from-code"
7
- import { evalCompiledJs } from "./eval-compiled-js"
8
- import type { ExecutionContext } from "./execution-context"
9
- import { importEvalPath } from "./import-eval-path"
10
- import Debug from "debug"
11
- import { isStaticAssetPath } from "lib/shared/static-asset-extensions"
12
- import { transformWithSucrase } from "./transform-with-sucrase"
13
-
14
- const debug = Debug("tsci:eval:import-local-file")
15
-
16
- export const importLocalFile = async (
17
- importName: string,
18
- ctx: ExecutionContext,
19
- depth = 0,
20
- ) => {
21
- debug("importLocalFile called with:", {
22
- importName,
23
- })
24
-
25
- const { fsMap, preSuppliedImports } = ctx
26
-
27
- const fsPath = resolveFilePathOrThrow(importName, fsMap)
28
- debug("fsPath:", fsPath)
29
- if (!ctx.fsMap[fsPath]) {
30
- debug("fsPath not found in fsMap:", fsPath)
31
- throw new Error(`File "${fsPath}" not found`)
32
- }
33
- const fileContent = fsMap[fsPath]
34
- debug("fileContent:", fileContent?.slice(0, 100))
35
- if (fsPath.endsWith(".json")) {
36
- const jsonData = JSON.parse(fileContent)
37
- preSuppliedImports[fsPath] = {
38
- __esModule: true,
39
- default: jsonData,
40
- }
41
- } else if (isStaticAssetPath(fsPath)) {
42
- const platformConfig = ctx.circuit.platform
43
- // Use projectBaseUrl for static file imports
44
- const staticUrl = `${platformConfig?.projectBaseUrl ?? ""}/${fsPath.startsWith("./") ? fsPath.slice(2) : fsPath}`
45
- preSuppliedImports[fsPath] = {
46
- __esModule: true,
47
- default: staticUrl,
48
- }
49
- } else if (fsPath.endsWith(".tsx") || fsPath.endsWith(".ts")) {
50
- const importNames = getImportsFromCode(fileContent)
51
-
52
- for (const importName of importNames) {
53
- if (!preSuppliedImports[importName]) {
54
- await importEvalPath(importName, ctx, depth + 1, {
55
- cwd: dirname(fsPath),
56
- })
57
- }
58
- }
59
-
60
- try {
61
- const transformedCode = transformWithSucrase(fileContent, fsPath)
62
- debug("evalCompiledJs called with:", {
63
- code: transformedCode.slice(0, 100),
64
- dirname: dirname(fsPath),
65
- })
66
- const importRunResult = evalCompiledJs(
67
- transformedCode,
68
- preSuppliedImports,
69
- dirname(fsPath),
70
- )
71
- debug("importRunResult:", {
72
- fsPath,
73
- importRunResult,
74
- })
75
- preSuppliedImports[fsPath] = importRunResult.exports
76
- } catch (error: any) {
77
- throw new Error(
78
- `Eval compiled js error for "${importName}": ${error.message}`,
79
- )
80
- }
81
- } else if (fsPath.endsWith(".js")) {
82
- // For .js files, especially from node_modules, we need to transform them
83
- preSuppliedImports[fsPath] = evalCompiledJs(
84
- transformWithSucrase(fileContent, fsPath),
85
- preSuppliedImports,
86
- dirname(fsPath),
87
- ).exports
88
- } else {
89
- throw new Error(
90
- `Unsupported file extension "${fsPath.split(".").pop()}" for "${fsPath}"`,
91
- )
92
- }
93
- }
@@ -1,54 +0,0 @@
1
- import { resolveNodeModule } from "lib/utils/resolve-node-module"
2
- import type { ExecutionContext } from "./execution-context"
3
- import { importLocalFile } from "./import-local-file"
4
-
5
- export const importNodeModule = async (
6
- importName: string,
7
- ctx: ExecutionContext,
8
- depth = 0,
9
- ) => {
10
- const { preSuppliedImports } = ctx
11
-
12
- if (preSuppliedImports[importName]) {
13
- return
14
- }
15
-
16
- const resolvedNodeModulePath = resolveNodeModule(importName, ctx.fsMap, "")
17
-
18
- if (!resolvedNodeModulePath) {
19
- throw new Error(`Node module "${importName}" not found`)
20
- }
21
-
22
- // Use importLocalFile to handle the node module
23
- await importLocalFile(resolvedNodeModulePath, ctx, depth)
24
-
25
- // Map the original import name to the resolved module's exports
26
- preSuppliedImports[importName] = preSuppliedImports[resolvedNodeModulePath]
27
-
28
- // Map without node_modules prefix for direct imports
29
- const unprefixedPath = resolvedNodeModulePath.replace(/^node_modules\//, "")
30
- preSuppliedImports[unprefixedPath] =
31
- preSuppliedImports[resolvedNodeModulePath]
32
-
33
- // Handle index files specially
34
- if (
35
- resolvedNodeModulePath.endsWith("/index.tsx") ||
36
- resolvedNodeModulePath.endsWith("/index.ts") ||
37
- resolvedNodeModulePath.endsWith("/index.js")
38
- ) {
39
- const dirPath = resolvedNodeModulePath.replace(/\/index\.(tsx?|js)$/, "")
40
- const unprefixedDirPath = dirPath.replace(/^node_modules\//, "")
41
- preSuppliedImports[unprefixedDirPath] =
42
- preSuppliedImports[resolvedNodeModulePath]
43
-
44
- // Handle scoped packages
45
- if (unprefixedDirPath.startsWith("@")) {
46
- const scopeParts = unprefixedDirPath.split("/")
47
- if (scopeParts.length >= 2) {
48
- const scopedName = `${scopeParts[0]}/${scopeParts[1]}`
49
- preSuppliedImports[scopedName] =
50
- preSuppliedImports[resolvedNodeModulePath]
51
- }
52
- }
53
- }
54
- }