breejs / bree

Bree is a Node.js and JavaScript job task scheduler with worker threads, cron, Date, and human syntax. Built for @ladjs, @forwardemail, @spamscanner, @cabinjs.
https://jobscheduler.net
MIT License
3.01k stars 78 forks source link

[fix] Not working with build from esbuild #142

Closed stefangeneralao closed 2 years ago

stefangeneralao commented 2 years ago

Describe the bug

Code works fine with TS-Node using Bree.extend(require('@breejs/ts-worker'));. However, when executing build from esbuild it fails.

Actual behavior

Console from executing build from esbuild:

Worker for job "job1" online undefined
Worker for job "job1" had an error {
  err: Error: Cannot find module '/absolute-path-to-project/shelly/build/worker.js'
      at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
      at Function.Module._load (node:internal/modules/cjs/loader:778:27)
      at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
      at MessagePort.<anonymous> (node:internal/main/worker_thread:187:24)
      at MessagePort.[nodejs.internal.kHybridDispatch] (node:internal/event_target:460:20)
      at MessagePort.exports.emitMessage (node:internal/per_context/messageport:23:28) {
    code: 'MODULE_NOT_FOUND',
    requireStack: []
  }
}
Worker for job "job1" exited with code 1 undefined

...

Expected behavior

Console from executing source code with ts-node:

Worker for job "job1" online undefined
worker started
Worker for job "job1" exited with code 0 undefined

...

Code to reproduce

Source code is compiled with:

esbuild ./src --bundle --platform=node --outdir=build

Minimalistic source code from documentation to replicate bug:

import Bree from 'bree';

Bree.extend(require('@breejs/ts-worker'));

const bree = new Bree({
  defaultExtension: 'ts',
  jobs: ['job1'],
});

bree.start();

jobs/job1.ts is an empty file.

Checklist

shadowgate15 commented 2 years ago

https://esbuild.github.io/api/#non-analyzable-imports

Take a look at the above. I believe that this will cause issues since we rely on runtime defined paths.

shadowgate15 commented 2 years ago

Also note, that jobs is never compiled so bree will always throw an error because there is no jobs folder.

stefangeneralao commented 2 years ago

... jobs is never compiled ...

How come? Is this by design? That doesn't make much sense to me. What am I missing?

shadowgate15 commented 2 years ago

It is just part of how esbuild works. I would look through their docs on how to configure it to bundle correctly.

stefangeneralao commented 2 years ago

Esbuild compiles everything, probably including the jobs folder. Is the solution to simply exclude the jobs folder from the compilation?

(Disclaimer: I'm a complete newbie to Bree)

shadowgate15 commented 2 years ago

So esbuild bundles everything into one file. Which means that when code, such as Bree, attempts to create paths at runtime and require those paths, they will fail because the files aren't there.

stefangeneralao commented 2 years ago

Alright. Wouldn't it work if Bree was excluded from the build then, .e.g., using --external:bree and then calling the uncompiled jobs folder from the build?

shadowgate15 commented 2 years ago

Basically. I don't know that much about esbuild but if I had to guess there are configs you can set to move files into the build folder instead of just having it bundle down to one file.

spence-s commented 2 years ago

@stefangeneralao - ts-worker only works as a ts-node workflow helper - if you are bundling with esbuild then you need to make your file looks like this:

src/index.ts

import * as path from 'node:path';
import Bree from 'bree';

// Bree.extend(require('@breejs/ts-worker'));

const bree = new Bree({
  root: path.resolve(__dirname, 'jobs'),
  jobs: ['job1']
});

bree.start();

your worker should be in: ./src/jobs/job1.ts

run esbuild ./src --bundle --platform=node --outdir=build run esbuild ./src/jobs/**/*.ts --platform=node --outdir=build/jobs

now you can run node build/src.js and it will run successfully.

stefangeneralao commented 2 years ago

That did it, thanks!