-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(esbuild): implement first version of construct using esbuild
- Loading branch information
1 parent
6ff7588
commit fe8a425
Showing
3 changed files
with
114 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,42 +1,91 @@ | ||
import * as lambda from '@aws-cdk/aws-lambda'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import * as es from 'esbuild'; | ||
import * as path from 'path'; | ||
|
||
import { nodeMajorVersion } from './utils'; | ||
import { extractFileName, findProjectRoot, nodeMajorVersion } from './utils'; | ||
|
||
/** | ||
* Properties for a NodejsFunction | ||
*/ | ||
export interface NodejsFunctionProps extends lambda.FunctionOptions { | ||
/** | ||
* The name of the exported handler in the entry file. | ||
* The root of the lambda project. If you specify this prop, ensure that | ||
* this path includes `entry` and any module/dependencies used by your | ||
* function otherwise bundling will not be possible. | ||
* | ||
* @default "handler" | ||
* @default = the closest path containing a .git folder | ||
*/ | ||
readonly rootDir?: string; | ||
|
||
/** | ||
* The name of the method within your code that Lambda calls to execute your function. | ||
* | ||
* The format includes the file name and handler function. | ||
* For more information, see https://docs.aws.amazon.com/lambda/latest/dg/lambda-nodejs.html. | ||
* | ||
* @default = 'index.handler' | ||
*/ | ||
readonly handler?: string; | ||
|
||
/** | ||
* The runtime environment. Only runtimes of the Node.js family are | ||
* supported. | ||
* | ||
* @default - `NODEJS_12_X` if `process.versions.node` >= '12.0.0', | ||
* @default = `NODEJS_12_X` if `process.versions.node` >= '12.0.0', | ||
* `NODEJS_10_X` otherwise. | ||
*/ | ||
readonly runtime?: lambda.Runtime; | ||
|
||
/** | ||
* The esbuild bundler specific options. | ||
* | ||
* @default = { platform: 'node' } | ||
*/ | ||
readonly esbuildOptions?: es.BuildOptions; | ||
} | ||
|
||
const BUILD_FOLDER = '.build'; | ||
const DEFAULT_BUILD_OPTIONS: es.BuildOptions = { | ||
bundle: true, | ||
target: 'es2017', | ||
external: ['aws-sdk'], | ||
}; | ||
|
||
/** | ||
* A Node.js Lambda function bundled using `esbuild` | ||
*/ | ||
export class NodejsFunction extends lambda.Function { | ||
constructor(scope: cdk.Construct, id: string, props: NodejsFunctionProps = {}) { | ||
if (props.runtime && props.runtime.family !== lambda.RuntimeFamily.NODEJS) { | ||
throw new Error('Only `NODEJS` runtimes are supported.'); | ||
} | ||
|
||
const handler = props.handler ?? 'handler'; | ||
const projectRoot = findProjectRoot(props.rootDir); | ||
if (!projectRoot) { | ||
throw new Error('Cannot find root directory. Please specify it with `rootDir` option.'); | ||
} | ||
|
||
const handler = props.handler ?? 'index.handler'; | ||
const defaultRunTime = nodeMajorVersion() >= 12 | ||
? lambda.Runtime.NODEJS_12_X | ||
: lambda.Runtime.NODEJS_10_X; | ||
const runtime = props.runtime ?? defaultRunTime; | ||
const entry = extractFileName(projectRoot, handler); | ||
|
||
es.buildSync({ | ||
...DEFAULT_BUILD_OPTIONS, | ||
...props.esbuildOptions, | ||
entryPoints: [entry], | ||
outdir: path.join(projectRoot, BUILD_FOLDER, path.dirname(entry)), | ||
platform: 'node', | ||
}); | ||
|
||
super(scope, id, { | ||
...props, | ||
runtime, | ||
code: lambda.Code.fromInline('TODO'), | ||
handler: `index.${handler}`, | ||
code: lambda.Code.fromAsset(path.join(projectRoot, BUILD_FOLDER)), | ||
handler, | ||
}); | ||
} | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,14 +1,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"module": "CommonJS", | ||
"target": "es6", | ||
"outDir": "dist", | ||
"declaration": true, | ||
"skipLibCheck": true, | ||
"outDir": "dist" | ||
"esModuleInterop": true, | ||
"lib": ["ES2017"] | ||
}, | ||
"include": [ | ||
"src" | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
], | ||
"include": [ | ||
"src" | ||
] | ||
} |