Skip to content

Instantly share code, notes, and snippets.

@steveruizok
Last active January 22, 2021 21:44
Show Gist options
  • Save steveruizok/403d7359ab09fcf510a93e56274d24db to your computer and use it in GitHub Desktop.
Save steveruizok/403d7359ab09fcf510a93e56274d24db to your computer and use it in GitHub Desktop.
Dream API for psuedo-bundled code.
async function getConcatenatedCodeFromWorker(...args: any[]) {
return "..."
}
async function getRunnableBundle<
Files extends Record<string, string>,
Entry extends keyof Files
>({
files,
entry,
scope,
}: {
files: Files
entry: Entry
scope: Record<string, any>
}) {
const concatenatedCode = await getConcatenatedCodeFromWorker(files, entry)
const args = Object.keys(scope)
const vargs = Object.values(scope)
return () => Function(...args)(...vargs, concatenatedCode)
}
const run = getRunnableBundle({
files: {
"name.ts": ` export const name = "Steve"`,
"index.ts": `import * as React from "reacy"
import { name } from "./
function App() {
return <div>Hello {name}!</div>
}
ReactDOM.render(<App/>, elm)`,
},
entry: "index.ts",
scope: {
React,
ReactDOM,
elm: document.getElementById("root")
},
})
run()
@nksaraf
Copy link

nksaraf commented Jan 22, 2021

import scopeEval from 'scope-eval';

// evaluates text (transpiled commonjs code) with scope, and dependencies

function runCodeAndReturnExports(text, scope, dependencies) {
  const require = (path) => {
    return dependencies[path];
  };

  const exports = {};
  const module = {
    exports,
  };

  scopeEval(text, { module, exports, require, ...scope });

  return { module, exports };
}


// module = { exports: {...all exports} }
// exports = { ...all exports }}
const { module, exports } = runCodeAndReturnExports(
  code,
  { monaco },
  {
    'monaco-editor-core': monaco,
    'monaco-editor': monaco,
    'use-monaco': monaco,
  }
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment