Skip to content

Commit

Permalink
docs(docs): prepare documentation and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
floydspace committed Oct 27, 2020
1 parent 3c7fa2f commit 27b8a73
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 5 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ Installation
------------

```sh
yarn add --dev aws-lambda-nodejs-esbuild
yarn add --dev @aws-cdk/aws-lambda aws-lambda-nodejs-esbuild
# or
npm install -D aws-lambda-nodejs-esbuild
npm install -D @aws-cdk/aws-lambda aws-lambda-nodejs-esbuild
```


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

```ts
TODO
import * as cdk from '@aws-cdk/core';
import { NodejsFunction } from 'aws-lambda-nodejs-esbuild';

class NewStack extends cdk.Stack {
constructor(scope, id, props) {
super(scope, id, props);

new NodejsFunction(this, 'NewFunction', {
esbuildOptions: {
minify: false, // default
target: 'ES2017', // default
}
});
}
}
```

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.
Expand All @@ -56,9 +70,11 @@ Usage
The normal AWS CDK deploy procedure will automatically compile with `esbuild`:

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

See examples: [minimal](examples/minimal/README.md) and [complete](examples/complete/README.md)


Author
------
Expand Down
4 changes: 4 additions & 0 deletions examples/complete/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# CDK asset staging directory
.cdk.staging
cdk.out
.build
7 changes: 7 additions & 0 deletions examples/complete/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# [aws-lambda-nodejs-esbuild](../../README.md) complete example

This example shows how to use the `aws-lambda-nodejs-esbuild` construct in the most common way.

Any package set as `external` in the `esbuildOptions` will not be bundled into the output file, but packed as a `node_modules` dependency.

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.
4 changes: 4 additions & 0 deletions examples/complete/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"app": "npx ts-node stack/app.ts",
"versionReporting": false
}
16 changes: 16 additions & 0 deletions examples/complete/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "complete",
"version": "0.1.0",
"devDependencies": {
"@aws-cdk/aws-lambda": "^1.70.0",
"@aws-cdk/core": "^1.70.0",
"@types/node": "10.17.27",
"aws-cdk": "1.70.0",
"aws-lambda-nodejs-esbuild": "*",
"ts-node": "^8.1.0",
"typescript": "~3.9.7"
},
"dependencies": {
"isin-validator": "^1.1.1"
}
}
13 changes: 13 additions & 0 deletions examples/complete/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import validateIsin from 'isin-validator';

export function handler(event: any) {
const isInvalid = validateIsin(event);

return {
statusCode: 200,
body: JSON.stringify({
message: isInvalid ? 'ISIN is invalid!' : 'ISIN is fine!',
input: event,
}),
};
}
5 changes: 5 additions & 0 deletions examples/complete/stack/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as cdk from '@aws-cdk/core';
import { CompleteStack } from './stack';

const app = new cdk.App();
new CompleteStack(app, 'CompleteStack');
15 changes: 15 additions & 0 deletions examples/complete/stack/stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as cdk from '@aws-cdk/core';
import { NodejsFunction } from 'aws-lambda-nodejs-esbuild';

export class CompleteStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

new NodejsFunction(this, 'CompleteExampleFunction', {
handler: 'src/index.handler',
esbuildOptions: {
external: ['isin-validator']
}
});
}
}
23 changes: 23 additions & 0 deletions examples/complete/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"lib": ["es2018"],
"declaration": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"typeRoots": ["./node_modules/@types"]
},
"exclude": ["cdk.out"]
}
4 changes: 4 additions & 0 deletions examples/minimal/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# CDK asset staging directory
.cdk.staging
cdk.out
.build
7 changes: 7 additions & 0 deletions examples/minimal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# [aws-lambda-nodejs-esbuild](../../README.md) minimal example

This example shows how to use the `aws-lambda-nodejs-esbuild` construct with default options.

If you do not provide a `handler` option it assumes that you define a lambda handler as `index.js` file in root folder.

By default it bundles all dependencies in a single file and transpiles to the `ES2017` target.
4 changes: 4 additions & 0 deletions examples/minimal/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"app": "node stack/app.js",
"versionReporting": false
}
13 changes: 13 additions & 0 deletions examples/minimal/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const validateIsin = require('isin-validator');

module.exports.handler = (event) => {
const isInvalid = validateIsin(event);

return {
statusCode: 200,
body: JSON.stringify({
message: isInvalid ? 'ISIN is invalid!' : 'ISIN is fine!',
input: event,
}),
};
};
13 changes: 13 additions & 0 deletions examples/minimal/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "minimal",
"version": "0.1.0",
"devDependencies": {
"@aws-cdk/aws-lambda": "^1.70.0",
"@aws-cdk/core": "1.70.0",
"aws-cdk": "1.70.0",
"aws-lambda-nodejs-esbuild": "*"
},
"dependencies": {
"isin-validator": "^1.1.1"
}
}
5 changes: 5 additions & 0 deletions examples/minimal/stack/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const cdk = require('@aws-cdk/core');
const { MinimalStack } = require('./stack');

const app = new cdk.App();
new MinimalStack(app, 'MinimalStack');
12 changes: 12 additions & 0 deletions examples/minimal/stack/stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const cdk = require('@aws-cdk/core');
const { NodejsFunction } = require('aws-lambda-nodejs-esbuild');

class MinimalStack extends cdk.Stack {
constructor(scope, id, props) {
super(scope, id, props);

new NodejsFunction(this, 'MinimalExampleFunction');
}
}

module.exports = { MinimalStack };
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"license": "MIT",
"author": "Victor Korzunin <[email protected]>",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist",
"package.json",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface NodejsFunctionProps extends lambda.FunctionOptions {
/**
* The esbuild bundler specific options.
*
* @default = { platform: 'node' }
* @default = { bundle: true, target: 'es2017' }
*/
readonly esbuildOptions?: es.BuildOptions;
}
Expand Down

0 comments on commit 27b8a73

Please sign in to comment.