Skip to content

Commit

Permalink
update to use cjs bundle repo (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar authored Jan 16, 2025
1 parent 7fa08eb commit 8099cee
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 17 deletions.
1 change: 1 addition & 0 deletions lib/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { AnyCircuitElement } from "circuit-json"

export interface WebWorkerConfiguration {
snippetsApiBaseUrl: string
cjsRegistryUrl: string
webWorkerUrl?: URL | string
verbose?: boolean
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
},
"exclude": ["node_modules", "dist"]
}
21 changes: 5 additions & 16 deletions webworker/import-snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,19 @@ export async function importSnippet(
) {
const { preSuppliedImports } = ctx
const fullSnippetName = importName.replace("@tsci/", "").replace(".", "/")
const { snippet: importedSnippet, error } = await fetch(
`${ctx.snippetsApiBaseUrl}/snippets/get?name=${fullSnippetName}`,
)
.then((res) => res.json())
.catch((e) => ({ error: e }))

const { cjs, error } = await fetch(`${ctx.cjsRegistryUrl}/${fullSnippetName}`)
.then(async (res) => ({ cjs: await res.text(), error: null }))
.catch((e) => ({ error: e, cjs: null }))

if (error) {
console.error("Error fetching import", importName, error)
return
}

const { compiled_js, code } = importedSnippet

const importNames = getImportsFromCode(code!)

for (const importName of importNames) {
if (!preSuppliedImports[importName]) {
await importEvalPath(importName, ctx, depth + 1)
}
}

try {
preSuppliedImports[importName] = evalCompiledJs(
compiled_js,
cjs!,
preSuppliedImports,
).exports
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions webworker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let executionContext: ExecutionContext | null = null

const webWorkerConfiguration: WebWorkerConfiguration = {
snippetsApiBaseUrl: "https://registry-api.tscircuit.com",
cjsRegistryUrl: "https://cjs.tscircuit.com",
verbose: false,
}

Expand Down

0 comments on commit 8099cee

Please sign in to comment.