andywer / threads.js

🧵 Make web workers & worker threads as simple as a function call.
https://threads.js.org/
MIT License
3.04k stars 161 forks source link

ReferenceError: importScripts is not defined when using await import #478

Closed linonetwo closed 1 year ago

linonetwo commented 1 year ago

I always get #461 for a worker, I think is is due to fail to import some deps, so I use dynamic import instead for a try

function runLLama(
  options: { conversationID: string; modelPath: string; prompt: string },
): Observable<ILanguageModelWorkerResponse> {
        const { LLM } = await import('llama-node');
        const { LLamaCpp } = await import('llama-node/dist/llm/llama-cpp');
        const llama = new LLM(LLamaCpp);
//...

const llmWorker = { runLLama };
export type LLMWorker = typeof llmWorker;
expose(llmWorker);

And I get importScripts is not defined

related compiled code

void 0!==__webpack_require__&&(__webpack_require__.ab=__dirname+"/native_modules/"),(()=>{var e={948:1};__webpack_require__.f.i=(r,t)=>{e[r]||importScripts(__webpack_require__.p+__webpack_require__.u(r))};var r=("undefined"!=typeof self?self:this).webpackChunktidgi=("undefined"!=typeof self?self:this).webpackChunktidgi||[],t=r.push.bind(r);r.push=r=>{var[n,o,i]=r;
linonetwo commented 1 year ago

Get this after externalize it by

  new ExternalsPlugin({
    type: 'commonjs',
    // use regex works.
    include: /@tiddlygit\+tiddlywiki@(.+)|llama-node(.+)|@llama-node(.+)/,
    // when using npm, we can use this. But with pnpm, this won't work ↓
    // include: path.join(__dirname, 'node_modules', '.pnpm', '@tiddlygit', 'tiddlywiki'),
  }),
  new ThreadsPlugin({
    target: 'electron-node-worker',
    plugins: ['ExternalsPlugin'],
  }),
require() of ES Module xx/Resources/node_modules/llama-node/dist/llm/llama-cpp.js from xx/Resources/app.asar.unpacked/.webpack/main/948.index.worker.js not supported.

Instead change the require of llama-cpp.js in xx/Resources/app.asar.unpacked/.webpack/main/948.index.worker.js to a dynamic import() which is available in all CommonJS modules. Error [ERR_REQUIRE_ESM]: require() of ES Module xx/Resources/node_modules/llama-node/dist/llm/llama-cpp.js from xx/Resources/app.asar.unpacked/.webpack/main/948.index.worker.js not supported.

Instead change the require of llama-cpp.js in xx/Resources/app.asar.unpacked/.webpack/main/948.index.worker.js to a dynamic import() which is available in all CommonJS modules.
linonetwo commented 1 year ago

This works

        const { LLM } = await import('llama-node');
        const { LLamaCpp } = await import('llama-node/dist/llm/llama-cpp.cjs');

with

declare module 'llama-node/dist/llm/llama-cpp.cjs' {
  export { LLamaCpp } from 'llama-node/dist/llm/llama-cpp';
}