From ca475ed737aa61dc34bce87b8f5b16058459e11f Mon Sep 17 00:00:00 2001 From: seveibar Date: Mon, 14 Oct 2024 21:44:07 -0700 Subject: [PATCH 1/2] move import later to maybe fix the globalThis reference --- src/hooks/use-run-tsx/index.tsx | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/hooks/use-run-tsx/index.tsx b/src/hooks/use-run-tsx/index.tsx index ceacb341..cab59965 100644 --- a/src/hooks/use-run-tsx/index.tsx +++ b/src/hooks/use-run-tsx/index.tsx @@ -56,17 +56,6 @@ export const useRunTsx = ({ } if (!code) return async function run() { - const { success, compiledTsx: compiledJs, error } = safeCompileTsx(code!) - - if (!success) { - setTsxResult({ - compiledModule: null, - message: `Compile Error: ${error.message}`, - circuitJson: null, - isLoading: false, - }) - } - const userCodeTsciImports = getImportsFromCode(code!).filter((imp) => imp.startsWith("@tsci/"), ) @@ -132,10 +121,20 @@ export const useRunTsx = ({ return preSuppliedImports[name] } ;(globalThis as any).__tscircuit_require = __tscircuit_require + globalThis.React = React - try { - globalThis.React = React + const { success, compiledTsx: compiledJs, error } = safeCompileTsx(code!) + if (!success) { + setTsxResult({ + compiledModule: null, + message: `Compile Error: ${error.message}`, + circuitJson: null, + isLoading: false, + }) + } + + try { const module = evalCompiledJs(compiledJs!) const componentExportKeys = Object.keys(module.exports).filter( From 6b33f5ae6302e43fb4064c4d2034f33fb310b8c1 Mon Sep 17 00:00:00 2001 From: seveibar Date: Mon, 14 Oct 2024 22:16:35 -0700 Subject: [PATCH 2/2] fix require not being available for imports on first run --- src/hooks/use-run-tsx/index.tsx | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/hooks/use-run-tsx/index.tsx b/src/hooks/use-run-tsx/index.tsx index cab59965..9b25ab12 100644 --- a/src/hooks/use-run-tsx/index.tsx +++ b/src/hooks/use-run-tsx/index.tsx @@ -63,6 +63,20 @@ export const useRunTsx = ({ const preSuppliedImports: Record = preSuppliedImportsRef.current + const __tscircuit_require = (name: string) => { + if (!preSuppliedImports[name]) { + throw new Error( + `Import "${name}" not found (imports available: ${Object.keys(preSuppliedImports).join(",")})`, + ) + } + return preSuppliedImports[name] + } + ;(globalThis as any).__tscircuit_require = __tscircuit_require + preSuppliedImports["@tscircuit/core"] = tscircuitCore + preSuppliedImports["react"] = React + preSuppliedImports["jscad-fiber"] = jscadFiber + globalThis.React = React + async function addImport(importName: string, depth: number = 0) { if (!importName.startsWith("@tsci/")) return if (preSuppliedImports[importName]) return @@ -87,9 +101,7 @@ export const useRunTsx = ({ const { compiled_js, code } = importedSnippet - const importNames = getImportsFromCode(code!).filter( - (imp) => !preSuppliedImports[imp], - ) + const importNames = getImportsFromCode(code!) for (const importName of importNames) { if (!preSuppliedImports[importName]) { @@ -108,21 +120,6 @@ export const useRunTsx = ({ await addImport(userCodeTsciImport) } - preSuppliedImports["@tscircuit/core"] = tscircuitCore - preSuppliedImports["react"] = React - preSuppliedImports["jscad-fiber"] = jscadFiber - - const __tscircuit_require = (name: string) => { - if (!preSuppliedImports[name]) { - throw new Error( - `Import "${name}" not found (imports available: ${Object.keys(preSuppliedImports).join(",")})`, - ) - } - return preSuppliedImports[name] - } - ;(globalThis as any).__tscircuit_require = __tscircuit_require - globalThis.React = React - const { success, compiledTsx: compiledJs, error } = safeCompileTsx(code!) if (!success) {