sebastianwessel / quickjs

A typescript package to execute JavaScript and TypeScript code in a webassembly quickjs sandbox
https://sebastianwessel.github.io/quickjs/
MIT License
609 stars 16 forks source link

How do I run code locally in Browser, except using cdn links? #38

Closed sRect closed 2 months ago

sRect commented 2 months ago

I use Vite + React in locally, But get some error, Unable to run successfully.

function App() { async function run() { // const variant = newVariant(RELEASE_SYNC, { // wasmLocation: new URL("/node_modules/@jitl/quickjs-wasmfile-release-sync/dist/emscripten-module.wasm", import.meta.url).href, // }); // const module = await newQuickJSWASMModuleFromVariant(Promise.resolve(variant)); // const vm = module.newContext(); // console.log("vm:", vm); const {createRuntime} = await quickJS('@jitl/quickjs-wasmfile-release-sync');

const { evalCode } = await createRuntime({
  allowFetch: true,
  allowFs: true, 
  // fetchAdapter: (...params) => fetch(...params),
  env: {
    MY_ENV_VAR: 'env var value',
    getJSON
  },
  mountFs: {
    src: {
      'custom.js': `export const relativeImportFunction = (n)=>'Hello from relative import function,' + n`,
    },
    'fileInRoot.txt': 'Some text content'
  },
})

const result = await evalCode(`
  import { readFileSync } from 'node:fs';
  import { relativeImportFunction } from './custom.js';

  async function getJSON2() {
    let url = 'https://xxx';
    try {
      let response = await fetch(url);
      return await response.json();
    } catch (error) {
      console.log('Request Failed', error);
    }
  }

  const r = readFileSync('./fileInRoot.txt');
  const r2 = relativeImportFunction(env.MY_ENV_VAR);
  const r3 = await getJSON2();

  export default {r, r2, r3};
`);

  console.log(result)

}

useEffect(() => { run(); }, []);

return (

hello

) }

export default App


+ browser concole:

Uncaught (in promise) TypeError: Failed to resolve module specifier '@jitl/quickjs-wasmfile-release-sync' at quickJS (@sebastianwessel_quickjs.js?v=1c57c2d9:16913:34) at run (App.tsx:70:35) at App.tsx:122:5 at commitHookEffectListMount (react-dom_client.js?v=f9f4815c:16915:34) at commitPassiveMountOnFiber (react-dom_client.js?v=f9f4815c:18156:19) at commitPassiveMountEffects_complete (react-dom_client.js?v=f9f4815c:18129:17) at commitPassiveMountEffects_begin (react-dom_client.js?v=f9f4815c:18119:15) at commitPassiveMountEffects (react-dom_client.js?v=f9f4815c:18109:11) at flushPassiveEffectsImpl (react-dom_client.js?v=f9f4815c:19490:11) at flushPassiveEffects (react-dom_client.js?v=f9f4815c:19447:22)

MarketingPip commented 2 months ago

@sRect - see here.

I suggest including this polyfill too - here.

sRect commented 2 months ago

@sRect - see here.

I suggest including this polyfill too - here.

I've added @hazae41/symbol-dispose-polyfill to the above code, But runs with other errors

sebastianwessel commented 2 months ago

Currently, there is a bigger refactoring in progress, which also should avoid the need for polyfill. But it will take some time

https://github.com/sebastianwessel/quickjs/issues/29

sebastianwessel commented 2 months ago

duplicate of #37