Skip to content

Commit

Permalink
fix recursive importing
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Oct 14, 2024
1 parent 9a0d524 commit 4adb983
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
10 changes: 5 additions & 5 deletions fake-snippets-api/lib/db/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,19 @@ exports.A555Timer = A555Timer;
// Add a snippet that outputs a square waveform using the a555timer

db.addSnippet({
name: "seveibar/a555timer-square-wave",
name: "testuser/a555timer-square-wave",
unscoped_name: "a555timer-square-wave",
owner_name: "seveibar",
owner_name: "testuser",
code: `
import { A555Timer } from "@tsci/seveibar.a555timer"
export default () => (
export const SquareWaveModule = () => (
<A555Timer name="U1" />
)
`.trim(),
dts: "export declare const MyChip: (props: {\n name: string;\n}) => any;\n",
dts: 'export declare const SquareWaveModule: () => import("react/jsx-runtime").JSX.Element;\n',
compiled_js:
'"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n value: true\n});\nexports.MyChip = void 0;\nvar _seveibar = require("@tsci/seveibar.a555timer");\nconst MyChip = props => /*#__PURE__*/React.createElement(_seveibar.A555Timer, {\n name: "U1"\n});\nexports.MyChip = MyChip;',
'"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n value: true\n});\nexports.SquareWaveModule = void 0;\nvar _seveibar = require("@tsci/seveibar.a555timer");\nconst SquareWaveModule = () => /*#__PURE__*/React.createElement(_seveibar.A555Timer, {\n name: "U1"\n});\nexports.SquareWaveModule = SquareWaveModule;',
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
snippet_type: "package",
Expand Down
15 changes: 10 additions & 5 deletions src/hooks/use-run-tsx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,16 @@ export const useRunTsx = ({
const fullSnippetName = importName
.replace("@tsci/", "")
.replace(".", "/")
const { snippet: importedSnippet } = await fetch(
const { snippet: importedSnippet, error } = await fetch(
`${apiBaseUrl}/snippets/get?name=${fullSnippetName}`,
).then((res) => res.json())
)
.then((res) => res.json())
.catch((e) => ({ error: e }))

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

const { compiled_js, code } = importedSnippet

Expand All @@ -98,9 +105,7 @@ export const useRunTsx = ({
}

try {
preSuppliedImports[importName] = evalCompiledJs(
importedSnippet.compiled_js,
).exports
preSuppliedImports[importName] = evalCompiledJs(compiled_js).exports
} catch (e) {
console.error("Error importing snippet", e)
}
Expand Down

0 comments on commit 4adb983

Please sign in to comment.