Skip to content

Commit 27b8a73

Browse files
committed
docs(docs): prepare documentation and examples
1 parent 3c7fa2f commit 27b8a73

18 files changed

+167
-5
lines changed

README.md

+20-4
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ Installation
3131
------------
3232

3333
```sh
34-
yarn add --dev aws-lambda-nodejs-esbuild
34+
yarn add --dev @aws-cdk/aws-lambda aws-lambda-nodejs-esbuild
3535
# or
36-
npm install -D aws-lambda-nodejs-esbuild
36+
npm install -D @aws-cdk/aws-lambda aws-lambda-nodejs-esbuild
3737
```
3838

3939

@@ -43,7 +43,21 @@ Configure
4343
By default, no configuration required, but you can change esbuild behavior:
4444

4545
```ts
46-
TODO
46+
import * as cdk from '@aws-cdk/core';
47+
import { NodejsFunction } from 'aws-lambda-nodejs-esbuild';
48+
49+
class NewStack extends cdk.Stack {
50+
constructor(scope, id, props) {
51+
super(scope, id, props);
52+
53+
new NodejsFunction(this, 'NewFunction', {
54+
esbuildOptions: {
55+
minify: false, // default
56+
target: 'ES2017', // default
57+
}
58+
});
59+
}
60+
}
4761
```
4862

4963
Check [esbuild](https://github.com/evanw/esbuild#command-line-usage) documentation for the full list of available options. Note that some options like `entryPoints` or `outdir` cannot be overwritten.
@@ -56,9 +70,11 @@ Usage
5670
The normal AWS CDK deploy procedure will automatically compile with `esbuild`:
5771

5872
- Create the AWS CDK project with `cdk init app --language=typescript`
59-
- Install aws-lambda-nodejs-esbuild plugin as above
73+
- Install `aws-lambda-nodejs-esbuild` as above
6074
- Deploy with `cdk deploy`
6175

76+
See examples: [minimal](examples/minimal/README.md) and [complete](examples/complete/README.md)
77+
6278

6379
Author
6480
------

examples/complete/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# CDK asset staging directory
2+
.cdk.staging
3+
cdk.out
4+
.build

examples/complete/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [aws-lambda-nodejs-esbuild](../../README.md) complete example
2+
3+
This example shows how to use the `aws-lambda-nodejs-esbuild` construct in the most common way.
4+
5+
Any package set as `external` in the `esbuildOptions` will not be bundled into the output file, but packed as a `node_modules` dependency.
6+
7+
If packing a package is not required, for instance if it exists in a layer, you may set it in the option `exclude`, so it will neither be packed nor bundled. `aws-sdk` is excluded by default.

examples/complete/cdk.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"app": "npx ts-node stack/app.ts",
3+
"versionReporting": false
4+
}

examples/complete/package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "complete",
3+
"version": "0.1.0",
4+
"devDependencies": {
5+
"@aws-cdk/aws-lambda": "^1.70.0",
6+
"@aws-cdk/core": "^1.70.0",
7+
"@types/node": "10.17.27",
8+
"aws-cdk": "1.70.0",
9+
"aws-lambda-nodejs-esbuild": "*",
10+
"ts-node": "^8.1.0",
11+
"typescript": "~3.9.7"
12+
},
13+
"dependencies": {
14+
"isin-validator": "^1.1.1"
15+
}
16+
}

examples/complete/src/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import validateIsin from 'isin-validator';
2+
3+
export function handler(event: any) {
4+
const isInvalid = validateIsin(event);
5+
6+
return {
7+
statusCode: 200,
8+
body: JSON.stringify({
9+
message: isInvalid ? 'ISIN is invalid!' : 'ISIN is fine!',
10+
input: event,
11+
}),
12+
};
13+
}

examples/complete/stack/app.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as cdk from '@aws-cdk/core';
2+
import { CompleteStack } from './stack';
3+
4+
const app = new cdk.App();
5+
new CompleteStack(app, 'CompleteStack');

examples/complete/stack/stack.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as cdk from '@aws-cdk/core';
2+
import { NodejsFunction } from 'aws-lambda-nodejs-esbuild';
3+
4+
export class CompleteStack extends cdk.Stack {
5+
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
6+
super(scope, id, props);
7+
8+
new NodejsFunction(this, 'CompleteExampleFunction', {
9+
handler: 'src/index.handler',
10+
esbuildOptions: {
11+
external: ['isin-validator']
12+
}
13+
});
14+
}
15+
}

examples/complete/tsconfig.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2018",
4+
"module": "commonjs",
5+
"lib": ["es2018"],
6+
"declaration": true,
7+
"strict": true,
8+
"noImplicitAny": true,
9+
"strictNullChecks": true,
10+
"noImplicitThis": true,
11+
"alwaysStrict": true,
12+
"noUnusedLocals": false,
13+
"noUnusedParameters": false,
14+
"noImplicitReturns": true,
15+
"noFallthroughCasesInSwitch": false,
16+
"inlineSourceMap": true,
17+
"inlineSources": true,
18+
"experimentalDecorators": true,
19+
"strictPropertyInitialization": false,
20+
"typeRoots": ["./node_modules/@types"]
21+
},
22+
"exclude": ["cdk.out"]
23+
}

examples/minimal/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# CDK asset staging directory
2+
.cdk.staging
3+
cdk.out
4+
.build

examples/minimal/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [aws-lambda-nodejs-esbuild](../../README.md) minimal example
2+
3+
This example shows how to use the `aws-lambda-nodejs-esbuild` construct with default options.
4+
5+
If you do not provide a `handler` option it assumes that you define a lambda handler as `index.js` file in root folder.
6+
7+
By default it bundles all dependencies in a single file and transpiles to the `ES2017` target.

examples/minimal/cdk.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"app": "node stack/app.js",
3+
"versionReporting": false
4+
}

examples/minimal/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const validateIsin = require('isin-validator');
2+
3+
module.exports.handler = (event) => {
4+
const isInvalid = validateIsin(event);
5+
6+
return {
7+
statusCode: 200,
8+
body: JSON.stringify({
9+
message: isInvalid ? 'ISIN is invalid!' : 'ISIN is fine!',
10+
input: event,
11+
}),
12+
};
13+
};

examples/minimal/package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "minimal",
3+
"version": "0.1.0",
4+
"devDependencies": {
5+
"@aws-cdk/aws-lambda": "^1.70.0",
6+
"@aws-cdk/core": "1.70.0",
7+
"aws-cdk": "1.70.0",
8+
"aws-lambda-nodejs-esbuild": "*"
9+
},
10+
"dependencies": {
11+
"isin-validator": "^1.1.1"
12+
}
13+
}

examples/minimal/stack/app.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const cdk = require('@aws-cdk/core');
2+
const { MinimalStack } = require('./stack');
3+
4+
const app = new cdk.App();
5+
new MinimalStack(app, 'MinimalStack');

examples/minimal/stack/stack.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const cdk = require('@aws-cdk/core');
2+
const { NodejsFunction } = require('aws-lambda-nodejs-esbuild');
3+
4+
class MinimalStack extends cdk.Stack {
5+
constructor(scope, id, props) {
6+
super(scope, id, props);
7+
8+
new NodejsFunction(this, 'MinimalExampleFunction');
9+
}
10+
}
11+
12+
module.exports = { MinimalStack };

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"license": "MIT",
66
"author": "Victor Korzunin <[email protected]>",
77
"main": "dist/index.js",
8+
"types": "dist/index.d.ts",
89
"files": [
910
"dist",
1011
"package.json",

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export interface NodejsFunctionProps extends lambda.FunctionOptions {
4949
/**
5050
* The esbuild bundler specific options.
5151
*
52-
* @default = { platform: 'node' }
52+
* @default = { bundle: true, target: 'es2017' }
5353
*/
5454
readonly esbuildOptions?: es.BuildOptions;
5555
}

0 commit comments

Comments
 (0)