So actually writing out this post helped me think outside the box and dig deeper into how the babelRequireHook works for the redwood code base. In the end, I realised the imports inside the exec script was causing the issues, and fixed it by adding dynamic imports inside the script.
const { getPaths } = require('@redwoodjs/internal')
const faktory = require('faktory-worker')
import path from 'path'
import babelRequireHook from '@babel/register'
babelRequireHook({
extends: path.join(getPaths().api.base, '.babelrc.js'),
extensions: ['.js', '.ts'],
plugins: [
[
'babel-plugin-module-resolver',
{
alias: {
src: getPaths().api.src,
},
},
],
],
ignore: ['node_modules'],
cache: false,
})
const runTask = async (taskName, taskArgs) => {
const [module, fn] = taskName.split('.')
const taskPath = path.join(getPaths().api.src, 'tasks', `${module}.js`)
const script = await import(taskPath)
await script[fn](taskArgs)
}
The above lines of code allow runTask to import any files within the api/tasks folder which is where I keep all my async stuff.