Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const fs = require("node:fs")
const process = require("node:process")

const { require: tsxRequire } = require(`tsx/cjs/api`)
const { tsImport } = require(`tsx/esm/api`)
const {
HandlerNotFound,
MalformedHandlerName,
Expand Down Expand Up @@ -136,10 +137,9 @@ function _hasFolderPackageJsonTypeModule(folder) {
}

function _hasPackageJsonTypeModule(file) {
// File must have a .js extension
const jsPath = `${file}.js`
return fs.existsSync(jsPath)
? _hasFolderPackageJsonTypeModule(path.resolve(path.dirname(jsPath)))
const dir = path.dirname(file)
return fs.existsSync(dir)
? _hasFolderPackageJsonTypeModule(path.resolve(dir))
: false
}

Expand Down Expand Up @@ -175,13 +175,15 @@ async function _tryRequire(appRoot, moduleRoot, module) {
}

// If still not loaded, try .js, .mjs, .cjs and .ts in that order.
// Files ending with .js are loaded as ES modules when the nearest parent package.json
// Files ending with .js and .ts are loaded as ES modules when the nearest parent package.json
// file contains a top-level field "type" with a value of "module".
// https://nodejs.org/api/packages.html#packages_type
const loaded =
(pjHasModule && (await _tryAwaitImport(lambdaStylePath, ".js"))) ||
(await _tryAwaitImport(lambdaStylePath, ".mjs")) ||
_tryRequireFile(lambdaStylePath, ".cjs") ||
(pjHasModule &&
(await tsImport(`${lambdaStylePath}.ts`, `${lambdaStylePath}.ts`))) ||
tsxRequire(`${lambdaStylePath}.ts`, `${lambdaStylePath}.ts`)
if (loaded) {
return loaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ describe("handler module format: commonjs", function desc() {
path: "/dev/package-type",
status: 200,
},

{
description: "should return 'foo' for ts-extension endpoint",
expected: "foo",
path: "/dev/ts-extension",
status: 200,
},
].forEach(({ description, expected, path, status }) => {
it(description, async () => {
const url = new URL(path, BASE_URL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ describe("handler module format: module", function desc() {
path: "/dev/top-level-await",
status: 200,
},

{
description: "should return 'bar' for ts-extension-load-esm endpoint",
expected: "bar",
path: "/dev/ts-extension-load-esm",
status: 200,
},
].forEach(({ description, expected, path, status }) => {
it(description, async () => {
const url = new URL(path, BASE_URL)
Expand Down
7 changes: 7 additions & 0 deletions tests/handler-module-formats/module/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,10 @@ functions:
method: get
path: top-level-await
handler: top-level-await/handler.bar

ts-extension-load-esm:
events:
- http:
method: get
path: ts-extension-load-esm
handler: ts-extension-load-esm/handler.foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!node_modules/
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { bar } from "testpackage"

const { stringify } = JSON

export async function foo() {
return {
body: stringify(bar()),
statusCode: 200,
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
Loading