- Notifications
You must be signed in to change notification settings - Fork126
Add ESM entry point for esbuild compatibility#296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
Copilot wants to merge4 commits intomainChoose a base branch fromcopilot/fix-esbuild-esm-bundle-issue
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+1,023 −0
Draft
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletionsREADME.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
241 changes: 241 additions & 0 deletions__tests__/esm-bundle.unit.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| 'use strict'; | ||
| const { execSync } = require('child_process'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const os = require('os'); | ||
| describe('ESM Bundle Tests', () => { | ||
| let tempDir; | ||
| let bundlePath; | ||
| let testFilePath; | ||
| beforeEach(() => { | ||
| // Create a temporary directory for test files | ||
| tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'lambda-api-esm-test-')); | ||
| bundlePath = path.join(tempDir, 'bundle.mjs'); | ||
| testFilePath = path.join(tempDir, 'test-entry.js'); | ||
| }); | ||
| afterEach(() => { | ||
| // Clean up temporary files | ||
| try { | ||
| if (fs.existsSync(tempDir)) { | ||
| fs.rmSync(tempDir, { recursive: true, force: true }); | ||
| } | ||
| } catch (e) { | ||
| // Ignore cleanup errors | ||
| } | ||
| }); | ||
| it('should bundle with esbuild for ESM without requiring banner', () => { | ||
| // Create a test entry file that imports from the .mjs entry point | ||
| const testCode = ` | ||
| import api from '${path.resolve(__dirname, '../index.mjs')}'; | ||
| const app = api(); | ||
| app.get('/test', (req, res) => { | ||
| res.json({ message: 'Hello from ESM bundle' }); | ||
| }); | ||
| export const handler = async (event, context) => { | ||
| return await app.run(event, context); | ||
| }; | ||
| `; | ||
| fs.writeFileSync(testFilePath, testCode); | ||
| // Bundle with esbuild (mark AWS SDK as external since they're peer dependencies) | ||
| try { | ||
| execSync( | ||
| `npx esbuild ${testFilePath} --bundle --platform=node --format=esm --outfile=${bundlePath} --external:@aws-sdk/client-s3 --external:@aws-sdk/s3-request-presigner`, | ||
| { cwd: path.resolve(__dirname, '..'), stdio: 'pipe' } | ||
| ); | ||
| } catch (e) { | ||
| throw new Error(`Bundling failed: ${e.message}`); | ||
| } | ||
| // Verify the bundle was created | ||
| expect(fs.existsSync(bundlePath)).toBe(true); | ||
| // Test that the bundle executes without errors | ||
| const testEvent = JSON.stringify({ | ||
| httpMethod: 'GET', | ||
| path: '/test', | ||
| headers: {}, | ||
| body: null, | ||
| isBase64Encoded: false, | ||
| }); | ||
| const testScript = ` | ||
| import { handler } from '${bundlePath}'; | ||
| const event = ${testEvent}; | ||
| const result = await handler(event, {}); | ||
| console.log(JSON.stringify(result)); | ||
| `; | ||
| const scriptPath = path.join(tempDir, 'test-run.mjs'); | ||
| fs.writeFileSync(scriptPath, testScript); | ||
| let output; | ||
| try { | ||
| output = execSync(`node ${scriptPath}`, { | ||
| encoding: 'utf-8', | ||
| cwd: tempDir, | ||
| }); | ||
| } catch (e) { | ||
| throw new Error(`Bundle execution failed: ${e.message}\n${e.stderr}`); | ||
| } | ||
| const result = JSON.parse(output.trim()); | ||
| // Verify the response | ||
| expect(result).toHaveProperty('statusCode', 200); | ||
| expect(result).toHaveProperty('headers'); | ||
| expect(result.headers).toHaveProperty('content-type', 'application/json'); | ||
| expect(result).toHaveProperty('body'); | ||
| const body = JSON.parse(result.body); | ||
| expect(body).toEqual({ message: 'Hello from ESM bundle' }); | ||
| }); | ||
| it('should work with CommonJS require (backward compatibility)', async () => { | ||
| const api = require('../index.js'); | ||
| expect(typeof api).toBe('function'); | ||
| const app = api(); | ||
| expect(app).toBeDefined(); | ||
| expect(typeof app.get).toBe('function'); | ||
| expect(typeof app.post).toBe('function'); | ||
| expect(typeof app.run).toBe('function'); | ||
| // Test full end-to-end functionality with CommonJS | ||
| app.get('/test-commonjs', (req, res) => { | ||
| res.json({ message: 'CommonJS works', method: req.method }); | ||
| }); | ||
| const event = { | ||
| httpMethod: 'GET', | ||
| path: '/test-commonjs', | ||
| headers: {}, | ||
| body: null, | ||
| isBase64Encoded: false, | ||
| }; | ||
| const result = await app.run(event, {}); | ||
| expect(result).toHaveProperty('statusCode', 200); | ||
| expect(result).toHaveProperty('headers'); | ||
| expect(result.headers).toHaveProperty('content-type', 'application/json'); | ||
| expect(result).toHaveProperty('body'); | ||
| const body = JSON.parse(result.body); | ||
| expect(body).toEqual({ message: 'CommonJS works', method: 'GET' }); | ||
| }); | ||
| it('should work with ESM import', async () => { | ||
| // Test that the .mjs file can be imported in Node.js | ||
| const testScript = ` | ||
| import api from '${path.resolve(__dirname, '../index.mjs')}'; | ||
| console.log(JSON.stringify({ | ||
| isFunction: typeof api === 'function', | ||
| hasDefault: api.default !== undefined | ||
| })); | ||
| `; | ||
| const scriptPath = path.join(tempDir, 'test-import.mjs'); | ||
| fs.writeFileSync(scriptPath, testScript); | ||
| let output; | ||
| try { | ||
| output = execSync(`node ${scriptPath}`, { | ||
| encoding: 'utf-8', | ||
| cwd: tempDir, | ||
| }); | ||
| } catch (e) { | ||
| throw new Error(`ESM import failed: ${e.message}\n${e.stderr}`); | ||
| } | ||
| const result = JSON.parse(output.trim()); | ||
| expect(result.isFunction).toBe(true); | ||
| }); | ||
| it('should bundle with esbuild for CommonJS without breaking (backward compatibility)', () => { | ||
| // Create a test entry file that requires from the CommonJS entry point | ||
| const testCode = ` | ||
| const api = require('${path.resolve(__dirname, '../index.js')}'); | ||
| const app = api(); | ||
| app.get('/test', (req, res) => { | ||
| res.json({ message: 'Hello from CommonJS bundle' }); | ||
| }); | ||
| module.exports.handler = async (event, context) => { | ||
| return await app.run(event, context); | ||
| }; | ||
| `; | ||
| fs.writeFileSync(testFilePath, testCode); | ||
| const cjsBundlePath = path.join(tempDir, 'bundle-cjs.js'); | ||
| // Bundle with esbuild using CommonJS format (mark AWS SDK as external since they're peer dependencies) | ||
| try { | ||
| execSync( | ||
| `npx esbuild ${testFilePath} --bundle --platform=node --format=cjs --outfile=${cjsBundlePath} --external:@aws-sdk/client-s3 --external:@aws-sdk/s3-request-presigner`, | ||
| { cwd: path.resolve(__dirname, '..'), stdio: 'pipe' } | ||
| ); | ||
| } catch (e) { | ||
| throw new Error(`CommonJS bundling failed: ${e.message}`); | ||
| } | ||
| // Verify the bundle was created | ||
| expect(fs.existsSync(cjsBundlePath)).toBe(true); | ||
| // Test that the bundle executes without errors | ||
| const testEvent = JSON.stringify({ | ||
| httpMethod: 'GET', | ||
| path: '/test', | ||
| headers: {}, | ||
| body: null, | ||
| isBase64Encoded: false, | ||
| }); | ||
| const testScript = ` | ||
| const { handler } = require('${cjsBundlePath}'); | ||
| const event = ${testEvent}; | ||
| handler(event, {}).then(result => { | ||
| console.log(JSON.stringify(result)); | ||
| }).catch(err => { | ||
| console.error(err.message); | ||
| process.exit(1); | ||
| }); | ||
| `; | ||
| const scriptPath = path.join(tempDir, 'test-run-cjs.js'); | ||
| fs.writeFileSync(scriptPath, testScript); | ||
| let output; | ||
| try { | ||
| output = execSync(`node ${scriptPath}`, { | ||
| encoding: 'utf-8', | ||
| cwd: tempDir, | ||
| }); | ||
| } catch (e) { | ||
| throw new Error(`CommonJS bundle execution failed: ${e.message}\n${e.stderr}`); | ||
| } | ||
| const result = JSON.parse(output.trim()); | ||
| // Verify the response | ||
| expect(result).toHaveProperty('statusCode', 200); | ||
| expect(result).toHaveProperty('headers'); | ||
| expect(result.headers).toHaveProperty('content-type', 'application/json'); | ||
| expect(result).toHaveProperty('body'); | ||
| const body = JSON.parse(result.body); | ||
| expect(body).toEqual({ message: 'Hello from CommonJS bundle' }); | ||
| }); | ||
| }); |
21 changes: 21 additions & 0 deletionsindex.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * ESM entry point for lambda-api | ||
| * | ||
| * This file provides ESM compatibility by ensuring require() is available | ||
| * for the CommonJS modules when bundled for ESM output. | ||
| * | ||
| * When bundlers like esbuild process this library for ESM output, this entry point | ||
| * ensures that the CommonJS code can still use require() for built-in Node.js modules. | ||
| */ | ||
| // Ensure require is available in ESM context (for esbuild bundles) | ||
| import { createRequire } from 'module'; | ||
| if (typeof globalThis.require === 'undefined') { | ||
| globalThis.require = createRequire(import.meta.url); | ||
| } | ||
| // Import the CommonJS module | ||
| // Note: This dynamic import ensures require is set up before loading CommonJS code | ||
| const { default: api } = await import('./index.js'); | ||
| export default api; |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.