Skip to content

Commit 97bb362

Browse files
rj-auTobbe
authored andcommitted
fix(api-server): lambdaLoader: improve handler import logic... (#12064)
1 parent 7a28197 commit 97bb362

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

packages/api-server/src/plugins/lambdaLoader.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,23 @@ export const setLambdaFunctions = async (foundFunctions: string[]) => {
2828
const ts = Date.now()
2929
const routeName = path.basename(fnPath).replace('.js', '')
3030

31-
const { handler } = await import(`file://${fnPath}`)
31+
const fnImport = await import(`file://${fnPath}`)
32+
const handler: Handler = (() => {
33+
if ('handler' in fnImport) {
34+
// ESModule export of handler - when using `export const handler = ...` - most common case
35+
return fnImport.handler
36+
}
37+
if ('default' in fnImport) {
38+
if ('handler' in fnImport.default) {
39+
// CommonJS export of handler - when using `module.exports.handler = ...` or `export default { handler: ... }`
40+
// This is less common, but required for bundling tools that export a default object, like esbuild or rollup
41+
return fnImport.default.handler
42+
}
43+
// Default export is not expected, so skip it
44+
}
45+
// If no handler is found, return undefined - we do not want to throw an error
46+
})()
47+
3248
LAMBDA_FUNCTIONS[routeName] = handler
3349
if (!handler) {
3450
console.warn(

0 commit comments

Comments
 (0)