@@ -46,10 +46,10 @@ export const handler = async (event, context) => {
4646
4747 fs . writeFileSync ( testFilePath , testCode ) ;
4848
49- // Bundle with esbuild
49+ // Bundle with esbuild (mark AWS SDK as external since they're peer dependencies)
5050 try {
5151 execSync (
52- `npx esbuild ${ testFilePath } --bundle --platform=node --format=esm --outfile=${ bundlePath } ` ,
52+ `npx esbuild ${ testFilePath } --bundle --platform=node --format=esm --outfile=${ bundlePath } --external:@aws-sdk/client-s3 --external:@aws-sdk/s3-request-presigner ` ,
5353 { cwd : path . resolve ( __dirname , '..' ) , stdio : 'pipe' }
5454 ) ;
5555 } catch ( e ) {
@@ -100,7 +100,7 @@ console.log(JSON.stringify(result));
100100 expect ( body ) . toEqual ( { message : 'Hello from ESM bundle' } ) ;
101101 } ) ;
102102
103- it ( 'should work with CommonJS require (backward compatibility)' , ( ) => {
103+ it ( 'should work with CommonJS require (backward compatibility)' , async ( ) => {
104104 const api = require ( '../index.js' ) ;
105105 expect ( typeof api ) . toBe ( 'function' ) ;
106106
@@ -109,6 +109,29 @@ console.log(JSON.stringify(result));
109109 expect ( typeof app . get ) . toBe ( 'function' ) ;
110110 expect ( typeof app . post ) . toBe ( 'function' ) ;
111111 expect ( typeof app . run ) . toBe ( 'function' ) ;
112+
113+ // Test full end-to-end functionality with CommonJS
114+ app . get ( '/test-commonjs' , ( req , res ) => {
115+ res . json ( { message : 'CommonJS works' , method : req . method } ) ;
116+ } ) ;
117+
118+ const event = {
119+ httpMethod : 'GET' ,
120+ path : '/test-commonjs' ,
121+ headers : { } ,
122+ body : null ,
123+ isBase64Encoded : false ,
124+ } ;
125+
126+ const result = await app . run ( event , { } ) ;
127+
128+ expect ( result ) . toHaveProperty ( 'statusCode' , 200 ) ;
129+ expect ( result ) . toHaveProperty ( 'headers' ) ;
130+ expect ( result . headers ) . toHaveProperty ( 'content-type' , 'application/json' ) ;
131+ expect ( result ) . toHaveProperty ( 'body' ) ;
132+
133+ const body = JSON . parse ( result . body ) ;
134+ expect ( body ) . toEqual ( { message : 'CommonJS works' , method : 'GET' } ) ;
112135 } ) ;
113136
114137 it ( 'should work with ESM import' , async ( ) => {
@@ -137,4 +160,82 @@ console.log(JSON.stringify({
137160 const result = JSON . parse ( output . trim ( ) ) ;
138161 expect ( result . isFunction ) . toBe ( true ) ;
139162 } ) ;
163+
164+ it ( 'should bundle with esbuild for CommonJS without breaking (backward compatibility)' , ( ) => {
165+ // Create a test entry file that requires from the CommonJS entry point
166+ const testCode = `
167+ const api = require('${ path . resolve ( __dirname , '../index.js' ) } ');
168+
169+ const app = api();
170+
171+ app.get('/test', (req, res) => {
172+ res.json({ message: 'Hello from CommonJS bundle' });
173+ });
174+
175+ module.exports.handler = async (event, context) => {
176+ return await app.run(event, context);
177+ };
178+ ` ;
179+
180+ fs . writeFileSync ( testFilePath , testCode ) ;
181+
182+ const cjsBundlePath = path . join ( tempDir , 'bundle-cjs.js' ) ;
183+
184+ // Bundle with esbuild using CommonJS format (mark AWS SDK as external since they're peer dependencies)
185+ try {
186+ execSync (
187+ `npx esbuild ${ testFilePath } --bundle --platform=node --format=cjs --outfile=${ cjsBundlePath } --external:@aws-sdk/client-s3 --external:@aws-sdk/s3-request-presigner` ,
188+ { cwd : path . resolve ( __dirname , '..' ) , stdio : 'pipe' }
189+ ) ;
190+ } catch ( e ) {
191+ throw new Error ( `CommonJS bundling failed: ${ e . message } ` ) ;
192+ }
193+
194+ // Verify the bundle was created
195+ expect ( fs . existsSync ( cjsBundlePath ) ) . toBe ( true ) ;
196+
197+ // Test that the bundle executes without errors
198+ const testEvent = JSON . stringify ( {
199+ httpMethod : 'GET' ,
200+ path : '/test' ,
201+ headers : { } ,
202+ body : null ,
203+ isBase64Encoded : false ,
204+ } ) ;
205+
206+ const testScript = `
207+ const { handler } = require('${ cjsBundlePath } ');
208+ const event = ${ testEvent } ;
209+ handler(event, {}).then(result => {
210+ console.log(JSON.stringify(result));
211+ }).catch(err => {
212+ console.error(err.message);
213+ process.exit(1);
214+ });
215+ ` ;
216+
217+ const scriptPath = path . join ( tempDir , 'test-run-cjs.js' ) ;
218+ fs . writeFileSync ( scriptPath , testScript ) ;
219+
220+ let output ;
221+ try {
222+ output = execSync ( `node ${ scriptPath } ` , {
223+ encoding : 'utf-8' ,
224+ cwd : tempDir ,
225+ } ) ;
226+ } catch ( e ) {
227+ throw new Error ( `CommonJS bundle execution failed: ${ e . message } \n${ e . stderr } ` ) ;
228+ }
229+
230+ const result = JSON . parse ( output . trim ( ) ) ;
231+
232+ // Verify the response
233+ expect ( result ) . toHaveProperty ( 'statusCode' , 200 ) ;
234+ expect ( result ) . toHaveProperty ( 'headers' ) ;
235+ expect ( result . headers ) . toHaveProperty ( 'content-type' , 'application/json' ) ;
236+ expect ( result ) . toHaveProperty ( 'body' ) ;
237+
238+ const body = JSON . parse ( result . body ) ;
239+ expect ( body ) . toEqual ( { message : 'Hello from CommonJS bundle' } ) ;
240+ } ) ;
140241} ) ;
0 commit comments