Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

fix: detect alt version of remix handler #1657

Merged
merged 4 commits into from
Nov 20, 2023
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
12 changes: 8 additions & 4 deletions src/runtimes/node/parser/exports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {
Declaration,
ExportDefaultDeclaration,
ExportNamedDeclaration,
ExportSpecifier,
Expression,
Expand Down Expand Up @@ -167,9 +166,14 @@ const isNamedExport = (node: ExportNamedDeclaration['specifiers'][number], name:
)
}

// Returns whether a given node is a default export declaration.
const isESMDefaultExport = (node: Statement): node is ExportDefaultDeclaration =>
node.type === 'ExportDefaultDeclaration'
// Returns whether a given node is or contains a default export declaration.
const isESMDefaultExport = (node: Statement): boolean =>
node.type === 'ExportDefaultDeclaration' ||
(node.type === 'ExportNamedDeclaration' &&
node.specifiers.some(
(exportSpecifier) =>
exportSpecifier.exported.type === 'Identifier' && exportSpecifier.exported.name === 'default',
))

/**
* Finds a `config` named CJS export that maps to an object variable
Expand Down
26 changes: 25 additions & 1 deletion tests/unit/runtimes/node/in_source_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,30 @@ describe('V2 API', () => {
})
})

// Another version of the Remix handler
test('ESM file with handler generated by a function, exported in same expression as config, but handler is not called `handler`', () => {
const source = `
const server_default = createRequestHandler({ build: server_build_exports, mode: "production" })
export { server_default as default };
`

const isc = parseSource(source, options)
expect(isc.runtimeAPIVersion).toEqual(2)
})

// Example for the Astro handler
test('ESM file with named `default` export renamed from a local binding', () => {
const source = `
const _exports = adapter.createExports(_manifest, _args)
const _default = _exports['default']

export { _default as default };
`

const isc = parseSource(source, options)
expect(isc.runtimeAPIVersion).toEqual(2)
})

test('ESM file with default export wrapped in a literal from a function', () => {
const source = `
async function handler(){ return { statusCode: 200, body: "Hello" }}
Expand All @@ -252,7 +276,7 @@ describe('V2 API', () => {
export { foo as default };`

const isc = parseSource(source, options)
expect(isc).toEqual({ inputModuleFormat: 'esm', runtimeAPIVersion: 1 })
expect(isc).toEqual({ inputModuleFormat: 'esm', routes: [], runtimeAPIVersion: 2 })
})

test('TypeScript file with a default export and no `handler` export', () => {
Expand Down