Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda-nodejs): esbuild bundling #11289

Merged
merged 48 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
6a16cf2
feat(lambda-nodejs): esbuild bundling
jogold Nov 4, 2020
1c8639b
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 4, 2020
36e29f8
minify and sourcemap
jogold Nov 4, 2020
ba7c830
README and target
jogold Nov 4, 2020
7d03889
README
jogold Nov 4, 2020
4f27d7a
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 4, 2020
2195f60
env
jogold Nov 4, 2020
2d6b25c
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 4, 2020
087d07f
Merge branch 'lambda-nodejs-esbuild' of github.com:jogold/aws-cdk int…
jogold Nov 4, 2020
703efd5
packagejsonmanager
jogold Nov 4, 2020
893af67
Update packages/@aws-cdk/aws-lambda-nodejs/README.md
jogold Nov 5, 2020
a087a89
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 8, 2020
39e22bd
clean up / refactor
jogold Nov 8, 2020
6176343
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 8, 2020
815ce59
EsBuildBundler
jogold Nov 8, 2020
de6ad71
do not docker build during tests
jogold Nov 9, 2020
3720e2f
win32 support
jogold Nov 9, 2020
f31e054
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 9, 2020
3ee5915
get target from runtime
jogold Nov 9, 2020
a61dbf1
JSDoc
jogold Nov 9, 2020
c634f26
shouldBuildImage
jogold Nov 9, 2020
712801e
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 9, 2020
8e94fc9
branch coverage to 70
jogold Nov 9, 2020
b2f4d86
refactor and loaders
jogold Nov 10, 2020
a19fb3a
fix npx on Windows
jogold Nov 10, 2020
8f1fc7a
fix json writing on Windows
jogold Nov 10, 2020
d6d81b6
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 10, 2020
3b47df1
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 10, 2020
0843335
Remove nohoist on parcel-bundler
jogold Nov 11, 2020
4646273
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 11, 2020
72da21f
remove parcel special case from yarn-upgrade workflow
jogold Nov 11, 2020
ef20456
esbuild -> Parcel in JSDoc
jogold Nov 11, 2020
d10d281
README
jogold Nov 11, 2020
c4e46f7
change loader interface
jogold Nov 11, 2020
27fa4af
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 11, 2020
5d47fce
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 12, 2020
5367675
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 13, 2020
a4c00bb
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 15, 2020
f366982
Merge branch 'master' into lambda-nodejs-esbuild
Nov 17, 2020
be89560
Update packages/@aws-cdk/aws-lambda-nodejs/lib/util.ts
jogold Nov 17, 2020
68c4d64
depsLockFilePath
jogold Nov 17, 2020
8a7dd2a
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 17, 2020
451d3f4
startsWith/endsWith
jogold Nov 17, 2020
1ccb052
latest esbuild
jogold Nov 17, 2020
5623bd5
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 17, 2020
ad3a010
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 17, 2020
c3eb7f6
Merge branch 'master' into lambda-nodejs-esbuild
jogold Nov 18, 2020
1a91fc2
README and Error
jogold Nov 18, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/workflows/yarn-upgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,18 @@ jobs:
- name: Run "ncu -u"
# We special-case @types/node because we want to stay on the current major (minimum supported node release)
# We special-case @types/fs-extra because the current major (9.x) is broken with @types/node >= 10
# We special-case parcel because we are currently on a pre-release and don't want to move to nightlies
# We special-case aws-sdk because of breaking changes with TS interface exports in recent minor versions - https://github.com/aws/aws-sdk-js/issues/3453
# We special-case typescript because it's not semantically versionned
# We special-case constructs because we want to stay in control of the minimum compatible version
run: |-
# Upgrade dependencies at repository root
ncu --upgrade --filter=@types/node,@types/fs-extra --target=minor
ncu --upgrade --filter=typescript --target=patch
ncu --upgrade --reject=@types/node,@types/fs-extra,constructs,parcel,typescript --target=minor
ncu --upgrade --reject=@types/node,@types/fs-extra,constructs,typescript --target=minor
# Upgrade all the packages
lerna exec --parallel ncu -- --upgrade --filter=@types/node,@types/fs-extra --target=minor
lerna exec --parallel ncu -- --upgrade --filter=typescript --target=patch
lerna exec --parallel ncu -- --upgrade --reject='@types/node,@types/fs-extra,constructs,parcel,typescript,aws-sdk,${{ steps.list-packages.outputs.list }}' --target=minor
lerna exec --parallel ncu -- --upgrade --reject='@types/node,@types/fs-extra,constructs,typescript,aws-sdk,${{ steps.list-packages.outputs.list }}' --target=minor
# This will create a brand new `yarn.lock` file (this is more efficient than `yarn install && yarn upgrade`)
- name: Run "yarn install --force"
run: yarn install --force
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@
"@aws-cdk/aws-ecr-assets/minimatch/**",
"@aws-cdk/aws-eks/yaml",
"@aws-cdk/aws-eks/yaml/**",
"@aws-cdk/aws-lambda-nodejs/parcel-bundler",
"@aws-cdk/aws-lambda-nodejs/parcel-bundler/**",
"@aws-cdk/cloud-assembly-schema/jsonschema",
"@aws-cdk/cloud-assembly-schema/jsonschema/**",
"@aws-cdk/cloud-assembly-schema/semver",
Expand Down
63 changes: 25 additions & 38 deletions packages/@aws-cdk/aws-lambda-nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ up the entry file:
├── stack.my-handler.ts # exports a function named 'handler'
```

This file is used as "entry" for [Parcel](https://parceljs.org/). This means that your code is
automatically transpiled and bundled whether it's written in JavaScript or TypeScript.
This file is used as "entry" for [esbuild](https://esbuild.github.io/). This means that your code is automatically transpiled and bundled whether it's written in JavaScript or TypeScript.

Alternatively, an entry file and handler can be specified:

Expand All @@ -45,11 +44,11 @@ All other properties of `lambda.Function` are supported, see also the [AWS Lambd
The `NodejsFunction` construct automatically [reuses existing connections](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/node-reusing-connections.html)
when working with the AWS SDK for JavaScript. Set the `awsSdkConnectionReuse` prop to `false` to disable it.

Use the `parcelEnvironment` prop to define environments variables when Parcel runs:
Use the `bundlingEnvironment` prop to define environments variables when esbuild runs:

```ts
new lambda.NodejsFunction(this, 'my-handler', {
parcelEnvironment: {
bundlingEnvironment: {
NODE_ENV: 'production',
},
});
Expand All @@ -73,37 +72,25 @@ new lambda.NodejsFunction(this, 'my-handler', {
});
```

This image should have Parcel installed at `/`. If you plan to use `nodeModules` it
This image should have esbuild installed globally. If you plan to use `nodeModules` it
should also have `npm` or `yarn` depending on the lock file you're using.

Use the [default image provided by `@aws-cdk/aws-lambda-nodejs`](https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-lambda-nodejs/parcel/Dockerfile)
Use the [default image provided by `@aws-cdk/aws-lambda-nodejs`](https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-lambda-nodejs/lib/Dockerfile)
as a source of inspiration.

### Project root
The `NodejsFunction` tries to automatically determine your project root, that is
the root of your node project. This is usually where the top level `node_modules`
folder of your project is located. When bundling in a Docker container, the
project root is used as the source (`/asset-input`) for the volume mounted in
the container.
### Lock file
The `NodejsFunction` requires a dependencies lock file (`yarn.lock` or
`package-lock.json`). When bundling in a Docker container, the path containing this
lock file is used as the source (`/asset-input`) for the volume mounted in the
container.

The following folders are considered by walking up parent folders starting from
the current working directory (order matters):
* the folder containing your `.git` folder
* the folder containing a `yarn.lock` file
* the folder containing a `package-lock.json` file
* the folder containing a `package.json` file
By default, it will try to automatically determine your project lock file.
Alternatively, you can specify the `depsLockFilePath` prop manually. In this
case you need to ensure that this path includes `entry` and any module/dependencies
used by your function. Otherwise bundling will fail.

Alternatively, you can specify the `projectRoot` prop manually. In this case you
need to ensure that this path includes `entry` and any module/dependencies used
by your function. Otherwise bundling will fail.

### Configuring Parcel
The `NodejsFunction` construct exposes some [Parcel](https://parceljs.org/) options via properties: `minify`, `sourceMaps` and `cacheDir`.

Parcel transpiles your code (every internal module) with [@babel/preset-env](https://babeljs.io/docs/en/babel-preset-env) and uses the
runtime version of your Lambda function as target.

Configuring Babel with Parcel is possible via a `.babelrc` or a `babel` config in `package.json`.
### Configuring esbuild
The `NodejsFunction` construct exposes some [esbuild](https://esbuild.github.io/) options via properties: `minify`, `sourceMaps` and `target`.

### Working with modules

Expand All @@ -121,10 +108,10 @@ new lambda.NodejsFunction(this, 'my-handler', {
```

#### Install modules
By default, all node modules referenced in your Lambda code will be bundled by Parcel.
By default, all node modules referenced in your Lambda code will be bundled by esbuild.
Use the `nodeModules` prop to specify a list of modules that should not be bundled
but instead included in the `node_modules` folder of the Lambda package. This is useful
when working with native dependencies or when Parcel fails to bundle a module.
when working with native dependencies or when esbuild fails to bundle a module.

```ts
new lambda.NodejsFunction(this, 'my-handler', {
Expand All @@ -133,25 +120,25 @@ new lambda.NodejsFunction(this, 'my-handler', {
```

The modules listed in `nodeModules` must be present in the `package.json`'s dependencies. The
same version will be used for installation. If a lock file is detected (`package-lock.json` or
`yarn.lock`) it will be used along with the right installer (`npm` or `yarn`).
same version will be used for installation. The lock file (`yarn.lock` or `package-lock.json`)
will be used along with the right installer (`yarn` or `npm`).

### Local bundling
If Parcel v2.0.0-beta.1 is available it will be used to bundle your code in your environment. Otherwise,
If esbuild is available it will be used to bundle your code in your environment. Otherwise,
bundling will happen in a [Lambda compatible Docker container](https://hub.docker.com/r/amazon/aws-sam-cli-build-image-nodejs12.x).

For macOS the recommendend approach is to install Parcel as Docker volume performance is really poor.
For macOS the recommendend approach is to install esbuild as Docker volume performance is really poor.

Parcel v2.0.0-beta.1 can be installed with:
esbuild can be installed with:

```bash
$ npm install --save-dev --save-exact [email protected]
$ npm install --save-dev esbuild@0
```

OR

```bash
$ yarn add --dev --exact [email protected]
$ yarn add --dev esbuild@0
```

To force bundling in a Docker container, set the `forceDockerBundling` prop to `true`. This
Expand Down
10 changes: 1 addition & 9 deletions packages/@aws-cdk/aws-lambda-nodejs/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
const baseConfig = require('cdk-build-tools/config/jest.config');
module.exports = {
...baseConfig,
coverageThreshold: {
global: {
...baseConfig.coverageThreshold.global,
branches: 50,
},
},
};
module.exports = baseConfig;
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ FROM $IMAGE
# Install yarn
RUN npm install --global [email protected]

# Install parcel 2 (fix the version since it's still in beta)
# install at "/" so that node_modules will be in the path for /asset-input
ARG PARCEL_VERSION=2.0.0-beta.1
RUN cd / && npm install parcel@$PARCEL_VERSION --no-package-lock
# Install esbuild
# (unsafe-perm because esbuild has a postinstall script)
ARG ESBUILD_VERSION=0
RUN npm install --global --unsafe-perm=true esbuild@$ESBUILD_VERSION

# Ensure all users can write to npm cache
RUN mkdir /tmp/npm-cache && \
Expand All @@ -22,4 +22,4 @@ RUN npm config --global set update-notifier false
# create non root user and change allow execute command for non root user
RUN /sbin/useradd -u 1000 user && chmod 711 /

CMD [ "parcel" ]
CMD [ "esbuild" ]
193 changes: 0 additions & 193 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/bundlers.ts

This file was deleted.

Loading