wilk / microjob

A tiny wrapper for turning Node.js worker threads into easy-to-use routines for heavy CPU loads.
https://wilk.github.io/microjob/
MIT License
2.02k stars 47 forks source link

ReferenceError: immutable_1 is not defined when using 'immutable' library #62

Open mlsad3 opened 4 years ago

mlsad3 commented 4 years ago

Hi,

This library looks really cool. Unfortunately I hit a snag when I try to use 'immutable'. For example:

  await start();

  const res1 = await job(() => {
    let i = 0;
    for (i = 0; i < 1000000; i++) {}
    let config = OrderedMap(fromJS({}));
    return i;
  });

In the above example, it fails immediately with "immutable_1 is not defined". Is this a known limitation due to the way Immutable was written (using factories)? Thanks!

mlsad3 commented 4 years ago

Hmm, it seems to happen whenever I do anything inside the job (like create a new instance):

  const res1 = await job(() => {
    const configValidationManager = new ConfigValidationManager();
    let i = 0;
    for (i = 0; i < 1000000; i++) {}
    return i;
  });

This gives: Error: ConfigValidation_manager_1 is not defined Using Node 12.16.1, microjob 0.7.0

mlsad3 commented 4 years ago

Looking at the .js file that TypeScript generates, I see the following:

const immutable_1 = require("immutable");
const ConfigValidation_manager_1 = __importDefault(require("./ConfigValidation.manager"));
const microjob_1 = require("microjob");
const errors_1 = require("../controllers/errors");

So it looks like the 'import' commands I have in my file won't work as they are not in the same scope as the worker...and my worker calls dozens of functions which also use various libraries, so they would all have similar issues.

mlsad3 commented 4 years ago

I moved all of the processing code to another file, with a couple exports:

File: My.worker.ts

export { processRawConfigWorker };

Then in Main.ts:

try {
  // Start worker pool
  await start();

  // This function will be executed in another thread
  const res = await job(
    data => {
      const { processRawConfigWorker } = require('My.worker');
      return processRawConfigWorker(data.rawConfig);
    },
    { data: { rawConfig: rawConfig } }
  );
  return res;
} catch (err) {
  console.error(err);
}

But this creates the error: "Cannot find module 'My.worker'".

Does anyone have an example using TypeScript and calling a function in another file? I tried prefixing with __dirName, but that just added '.' to the name.