@nikovirtala/projen-bundle-lambda-function-code 0.0.16 → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +198 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1 +1,198 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nikovirtala/projen-bundle-lambda-function-code
|
|
2
|
+
|
|
3
|
+
A Projen component for automatically bundling AWS Lambda function code using esbuild.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Auto-discovery**: Automatically finds Lambda handler files with configurable extensions
|
|
8
|
+
- **ESBuild bundling**: Uses esbuild for fast, efficient bundling
|
|
9
|
+
- **CDK integration**: Generates CDK constructs with pre-bundled Lambda code
|
|
10
|
+
- **TypeScript support**: Full TypeScript support with configurable tsconfig
|
|
11
|
+
- **Flexible configuration**: Customizable bundling options per function
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @nikovirtala/projen-bundle-lambda-function-code
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### Basic Setup
|
|
22
|
+
|
|
23
|
+
Add the bundler to your Projen TypeScript project:
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { LambdaFunctionCodeBundler, Bundler } from '@nikovirtala/projen-bundle-lambda-function-code';
|
|
27
|
+
|
|
28
|
+
const project = new typescript.TypeScriptProject({
|
|
29
|
+
// ... your project config
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Add the bundler component
|
|
33
|
+
new Bundler(project);
|
|
34
|
+
|
|
35
|
+
// Auto-discover and bundle Lambda functions
|
|
36
|
+
new LambdaFunctionCodeBundler(project, {
|
|
37
|
+
extension: '.lambda.ts',
|
|
38
|
+
srcdir: 'src',
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Lambda Handler Files
|
|
43
|
+
|
|
44
|
+
Create Lambda handler files with the configured extension:
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
// src/hello-world.lambda.ts
|
|
48
|
+
export const handler = async (event: any) => {
|
|
49
|
+
return {
|
|
50
|
+
statusCode: 200,
|
|
51
|
+
body: JSON.stringify({ message: 'Hello World!' }),
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Generated CDK Constructs
|
|
57
|
+
|
|
58
|
+
The bundler automatically generates CDK constructs for each Lambda function:
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
// Generated: src/hello-world-code.ts
|
|
62
|
+
import * as path from 'path';
|
|
63
|
+
import { aws_lambda } from 'aws-cdk-lib';
|
|
64
|
+
|
|
65
|
+
export const HelloWorldFunctionCode = aws_lambda.Code.fromAsset(
|
|
66
|
+
path.join(__dirname, '../assets/hello-world'),
|
|
67
|
+
);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Use in your CDK stack:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { HelloWorldFunctionCode } from './hello-world-code';
|
|
74
|
+
|
|
75
|
+
new aws_lambda.Function(this, 'HelloWorldFunction', {
|
|
76
|
+
runtime: aws_lambda.Runtime.NODEJS_20_X,
|
|
77
|
+
handler: 'index.handler',
|
|
78
|
+
code: HelloWorldFunctionCode,
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## API Reference
|
|
83
|
+
|
|
84
|
+
### Bundler
|
|
85
|
+
|
|
86
|
+
Main component for esbuild bundling support.
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
new Bundler(project, {
|
|
90
|
+
esbuildVersion?: string; // esbuild version requirement
|
|
91
|
+
assetsDir?: string; // output directory (default: "assets")
|
|
92
|
+
addToPreCompile?: boolean; // add to pre-compile phase (default: true)
|
|
93
|
+
loaders?: Record<string, string>; // file extension loaders
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### LambdaFunctionCodeBundler
|
|
98
|
+
|
|
99
|
+
Auto-discovers and bundles Lambda functions.
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
new LambdaFunctionCodeBundler(project, {
|
|
103
|
+
extension: string; // file extension to discover (e.g., ".lambda.ts")
|
|
104
|
+
srcdir: string; // source directory to scan
|
|
105
|
+
bundleOptions?: { // default bundling options
|
|
106
|
+
externals?: string[];
|
|
107
|
+
sourcemap?: boolean;
|
|
108
|
+
target?: string;
|
|
109
|
+
platform?: string;
|
|
110
|
+
// ... more options
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### LambdaFunctionCodeBundle
|
|
116
|
+
|
|
117
|
+
Bundle individual Lambda functions.
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
new LambdaFunctionCodeBundle(project, {
|
|
121
|
+
entrypoint: string; // path to Lambda handler file
|
|
122
|
+
extension: string; // file extension
|
|
123
|
+
constructFile?: string; // generated construct file name
|
|
124
|
+
constructName?: string; // generated construct class name
|
|
125
|
+
bundlingOptions?: BundlingOptions;
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Bundling Options
|
|
130
|
+
|
|
131
|
+
Customize esbuild behavior:
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
interface BundlingOptions {
|
|
135
|
+
externals?: string[]; // external dependencies to exclude
|
|
136
|
+
sourcemap?: boolean; // include source maps (default: true)
|
|
137
|
+
target?: string; // esbuild target (default: "esnext")
|
|
138
|
+
platform?: string; // target platform (default: "node")
|
|
139
|
+
outfile?: string; // output filename (default: "index.mjs")
|
|
140
|
+
format?: string; // output format (esm, cjs, iife)
|
|
141
|
+
minify?: boolean; // minify output (default: true)
|
|
142
|
+
tsconfigPath?: string; // TypeScript config path
|
|
143
|
+
loaders?: Record<string, string>; // file loaders
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Examples
|
|
148
|
+
|
|
149
|
+
### Custom Bundling Options
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
new LambdaFunctionCodeBundler(project, {
|
|
153
|
+
extension: '.lambda.ts',
|
|
154
|
+
srcdir: 'src/functions',
|
|
155
|
+
bundleOptions: {
|
|
156
|
+
externals: ['aws-sdk'],
|
|
157
|
+
minify: false,
|
|
158
|
+
sourcemap: true,
|
|
159
|
+
target: 'node18',
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Individual Function Bundle
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
new LambdaFunctionCodeBundle(project, {
|
|
168
|
+
entrypoint: 'src/special-function.lambda.ts',
|
|
169
|
+
extension: '.lambda.ts',
|
|
170
|
+
constructName: 'SpecialFunction',
|
|
171
|
+
bundlingOptions: {
|
|
172
|
+
externals: ['@aws-sdk/client-s3'],
|
|
173
|
+
format: 'cjs',
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Development
|
|
179
|
+
|
|
180
|
+
This project uses Projen for configuration management.
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
# Install dependencies
|
|
184
|
+
pnpm install
|
|
185
|
+
|
|
186
|
+
# Build the project
|
|
187
|
+
pnpm build
|
|
188
|
+
|
|
189
|
+
# Run tests
|
|
190
|
+
pnpm test
|
|
191
|
+
|
|
192
|
+
# Update project configuration
|
|
193
|
+
npx projen
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## License
|
|
197
|
+
|
|
198
|
+
MIT
|
package/package.json
CHANGED