Skip to content

Commit

Permalink
fix: use .default of worker module if it exists (#21)
Browse files Browse the repository at this point in the history
Dynamic import resolves with `{ default: { default: [Function] } }` for CJS modules
  • Loading branch information
aleclarson authored Jan 4, 2022
1 parent 3586860 commit 113b621
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ async function getHandler(
try {
// With our current set of TypeScript options, this is transpiled to
// `require(filename)`.
handler = await import(filename)
const handlerModule = await import(filename)

// Check if the default export is an object, because dynamic import
// resolves with `{ default: { default: [Function] } }` for CJS modules.
handler =
(typeof handlerModule.default !== 'function' && handlerModule.default) ||
handlerModule

if (typeof handler !== 'function') {
handler = await (handler as any)[name]
}
Expand Down

0 comments on commit 113b621

Please sign in to comment.