Skip to content

Commit d9cb599

Browse files
committed
feat: load app with sourcemap at request command
1 parent b941d6e commit d9cb599

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

src/commands/request/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ describe('requestCommand', () => {
8484
expect(mockBuildAndImportApp).toHaveBeenCalledWith(expectedPath, {
8585
external: ['@hono/node-server'],
8686
watch: false,
87+
sourcemap: true,
8788
})
8889

8990
expect(consoleLogSpy).toHaveBeenCalledWith(
@@ -114,6 +115,7 @@ describe('requestCommand', () => {
114115
expect(mockBuildAndImportApp).toHaveBeenCalledWith(expectedPath, {
115116
external: ['@hono/node-server'],
116117
watch: true,
118+
sourcemap: true,
117119
})
118120

119121
expect(consoleLogSpy).toHaveBeenCalledWith(

src/commands/request/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export function getBuildIterator(
7070
return buildAndImportApp(appFilePath, {
7171
external: ['@hono/node-server'],
7272
watch,
73+
sourcemap: true,
7374
})
7475
}
7576

src/utils/build.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ describe('buildAndImportApp', () => {
6363
entryPoints: [filePath],
6464
bundle: true,
6565
write: false,
66+
sourcemap: false,
67+
sourcesContent: false,
68+
sourceRoot: process.cwd(),
6669
format: 'esm',
6770
target: 'node20',
6871
jsx: 'automatic',
@@ -97,6 +100,9 @@ describe('buildAndImportApp', () => {
97100
entryPoints: [filePath],
98101
bundle: true,
99102
write: false,
103+
sourcemap: false,
104+
sourcesContent: false,
105+
sourceRoot: process.cwd(),
100106
format: 'esm',
101107
target: 'node20',
102108
jsx: 'automatic',
@@ -134,6 +140,9 @@ describe('buildAndImportApp', () => {
134140
entryPoints: [filePath],
135141
bundle: true,
136142
write: false,
143+
sourcemap: false,
144+
sourcesContent: false,
145+
sourceRoot: process.cwd(),
137146
format: 'esm',
138147
target: 'node20',
139148
jsx: 'automatic',
@@ -174,6 +183,33 @@ describe('buildAndImportApp', () => {
174183
expect(result).toBe(mockApp)
175184
})
176185

186+
it('should build and import file with sourcemap', async () => {
187+
const mockApp = new Hono()
188+
const filePath = '/path/to/app.ts'
189+
const bundledCode = 'export default app;'
190+
191+
setupBundledCode(bundledCode)
192+
193+
// Mock dynamic import
194+
const dataUrl = `data:text/javascript;base64,${Buffer.from(`${bundledCode}\n//# sourceURL=file://${process.cwd()}/__hono_cli_bundle__.js`).toString('base64')}`
195+
vi.doMock(dataUrl, () => ({ default: mockApp }))
196+
197+
const buildIterator = buildAndImportApp(filePath, { sourcemap: true })
198+
const result = (await buildIterator.next()).value
199+
200+
expect(mockEsbuild).toHaveBeenCalledWith(
201+
expect.objectContaining({
202+
entryPoints: [filePath],
203+
jsx: 'automatic',
204+
jsxImportSource: 'hono/jsx',
205+
sourcemap: true,
206+
sourcesContent: false,
207+
sourceRoot: process.cwd(),
208+
})
209+
)
210+
expect(result).toBe(mockApp)
211+
})
212+
177213
it('should handle esbuild errors', async () => {
178214
const filePath = '/path/to/app.ts'
179215
const buildError = new Error('Build failed')
@@ -202,6 +238,9 @@ describe('buildAndImportApp', () => {
202238
entryPoints: [filePath],
203239
bundle: true,
204240
write: false,
241+
sourcemap: false,
242+
sourcesContent: false,
243+
sourceRoot: process.cwd(),
205244
format: 'esm',
206245
target: 'node20',
207246
jsx: 'automatic',

src/utils/build.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Hono } from 'hono'
44
export interface BuildOptions {
55
external?: string[]
66
watch?: boolean
7+
sourcemap?: boolean
78
}
89

910
/**
@@ -25,6 +26,9 @@ export async function* buildAndImportApp(
2526

2627
const context = await esbuild.context({
2728
entryPoints: [filePath],
29+
sourcemap: options.sourcemap ?? false,
30+
sourcesContent: false,
31+
sourceRoot: process.cwd(),
2832
bundle: true,
2933
write: false,
3034
format: 'esm',
@@ -40,7 +44,10 @@ export async function* buildAndImportApp(
4044
build.onEnd(async (result) => {
4145
try {
4246
// Execute the bundled code using data URL
43-
const code = result.outputFiles?.[0]?.text || ''
47+
let code = result.outputFiles?.[0]?.text || ''
48+
if (options.sourcemap) {
49+
code += `\n//# sourceURL=file://${process.cwd()}/__hono_cli_bundle__.js`
50+
}
4451
const dataUrl = `data:text/javascript;base64,${Buffer.from(code).toString('base64')}`
4552
const module = await import(dataUrl)
4653
const app = module.default

0 commit comments

Comments
 (0)