Skip to content

Commit

Permalink
feat(cordis): support esm loader below node 10.13
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jul 28, 2024
1 parent fa78ba8 commit 821d7b4
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/cordis/src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,29 @@ export interface Options extends Loader.Config {
daemon?: daemon.Config
}

const internalLoaders: ((require: NodeRequire) => any)[] = [
// Node 20.13 and above
(require) => require('internal/modules/esm/loader').getOrInitializeCascadedLoader(),
(require) => require('internal/process/esm_loader').esmLoader,
]

function getInternal() {
const require = createRequire(import.meta.url)
for (const loader of internalLoaders) {
try {
return loader(require)
} catch {}
}
}

export async function start(options: Options) {
const ctx = new Context()
ctx.plugin(Loader, {
...options,
filename: process.env.CORDIS_LOADER_ENTRY,
})
if (process.execArgv.includes('--expose-internals')) {
const require = createRequire(import.meta.url)
ctx.loader.internal = require('internal/modules/esm/loader').getOrInitializeCascadedLoader()
ctx.loader.internal = getInternal()
}
if (options.logger) ctx.plugin(logger, options.logger)
if (options.daemon) ctx.plugin(daemon, options.daemon)
Expand Down

0 comments on commit 821d7b4

Please sign in to comment.