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

Commit 46c96e2

Browse files
fix: detect alt version of remix handler (#1657)
* fix: detect alt version of remix handler * chore: add another test for astro-style config Test case taken from #1658. Co-Authored-By: @lilnasy * Update tests/unit/runtimes/node/in_source_config.test.ts Co-authored-by: Eduardo Bouças <[email protected]> --------- Co-authored-by: Eduardo Bouças <[email protected]>
1 parent 5ca965f commit 46c96e2

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/runtimes/node/parser/exports.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type {
22
Declaration,
3-
ExportDefaultDeclaration,
43
ExportNamedDeclaration,
54
ExportSpecifier,
65
Expression,
@@ -167,9 +166,14 @@ const isNamedExport = (node: ExportNamedDeclaration['specifiers'][number], name:
167166
)
168167
}
169168

170-
// Returns whether a given node is a default export declaration.
171-
const isESMDefaultExport = (node: Statement): node is ExportDefaultDeclaration =>
172-
node.type === 'ExportDefaultDeclaration'
169+
// Returns whether a given node is or contains a default export declaration.
170+
const isESMDefaultExport = (node: Statement): boolean =>
171+
node.type === 'ExportDefaultDeclaration' ||
172+
(node.type === 'ExportNamedDeclaration' &&
173+
node.specifiers.some(
174+
(exportSpecifier) =>
175+
exportSpecifier.exported.type === 'Identifier' && exportSpecifier.exported.name === 'default',
176+
))
173177

174178
/**
175179
* Finds a `config` named CJS export that maps to an object variable

tests/unit/runtimes/node/in_source_config.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,30 @@ describe('V2 API', () => {
237237
})
238238
})
239239

240+
// Another version of the Remix handler
241+
test('ESM file with handler generated by a function, exported in same expression as config, but handler is not called `handler`', () => {
242+
const source = `
243+
const server_default = createRequestHandler({ build: server_build_exports, mode: "production" })
244+
export { server_default as default };
245+
`
246+
247+
const isc = parseSource(source, options)
248+
expect(isc.runtimeAPIVersion).toEqual(2)
249+
})
250+
251+
// Example for the Astro handler
252+
test('ESM file with named `default` export renamed from a local binding', () => {
253+
const source = `
254+
const _exports = adapter.createExports(_manifest, _args)
255+
const _default = _exports['default']
256+
257+
export { _default as default };
258+
`
259+
260+
const isc = parseSource(source, options)
261+
expect(isc.runtimeAPIVersion).toEqual(2)
262+
})
263+
240264
test('ESM file with default export wrapped in a literal from a function', () => {
241265
const source = `
242266
async function handler(){ return { statusCode: 200, body: "Hello" }}
@@ -252,7 +276,7 @@ describe('V2 API', () => {
252276
export { foo as default };`
253277

254278
const isc = parseSource(source, options)
255-
expect(isc).toEqual({ inputModuleFormat: 'esm', runtimeAPIVersion: 1 })
279+
expect(isc).toEqual({ inputModuleFormat: 'esm', routes: [], runtimeAPIVersion: 2 })
256280
})
257281

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

0 commit comments

Comments
 (0)