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

docs: upgrading, tags, security and minor fixes #119

Merged
merged 3 commits into from
Oct 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
346 changes: 241 additions & 105 deletions API.md

Large diffs are not rendered by default.

36 changes: 0 additions & 36 deletions PUBLISHING.md

This file was deleted.

51 changes: 46 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

_CDK constructs for [esbuild](https:/evanw/esbuild), an extremely fast JavaScript bundler_

[Getting started](#getting-started) | [Upgrading from 1.x](#upgrading-from-1x) |
[Getting started](#getting-started) | [Migrating to v2](#migrating-to-v2) |
[Documentation](#documentation) | [API Reference](#api-reference) | [Versioning](#versioning)

## Why?
Expand All @@ -14,9 +14,9 @@ AWS CDK [supports _esbuild_ with Lambda Functions](https://docs.aws.amazon.com/c

This package is running _esbuild_ directly in Node.js and bypasses Docker which the AWS CDK implementation uses. The approach is quicker and easier to use for Node.js users, but incompatible with other languages.

**⚠️ Regarding stability**
**⚠️ A note on stability**

This package is generally stable. However _esbuild_ is still on major version zero, which you should consider. Please check their guide on [production readiness](https://esbuild.github.io/faq/#production-readiness).
This package is generally stable and ready to be used in production as many do. However _esbuild_ is still on major version zero, which you should consider. Please check their guide on [production readiness](https://esbuild.github.io/faq/#production-readiness).

Notably upgrades of the _esbuild_ version requirement will be introduced in **minor versions** of this package and will inherit breaking changes from _esbuild_.

Expand Down Expand Up @@ -135,16 +135,42 @@ Underlying classes power the other features. You normally won't have to use them

Auto-generated reference for classes and structs. This information is also available within the code completion of your IDE.

## Upgrading from 1.x
## Migrating to v2

tbd
The main changes in cdk-esbuild v2 are:

- The package is now `jsii` compliant and published in the [Construct Hub](https://constructs.dev/). This will also enable a possible feature release for other languages.
- Deprecated properties and classes have been removed, most notably the previous support for bundling via a Docker container. The implementation had a few issues that cannot be easily resolved. However more generic support for custom executables might arrive in later versions.
- `esbuild` is now installed as an optional dependency. It's not optional at all, but this is a ramification of using `jsii`. In practice this will have no impact on most people.
- Interfaces have been streamlined and the names and setup of internal types have been changed. In the unlikely case that someone was relying on these, upgrading will be straight forward.

### Upgrading

- Update the package dependency to v2: `npm install --save @mrgrain/cdk-esbuild@^2.0.0`
- `esbuild` is now installed as an optional dependency. If your setup does not automatically install optional dependencies, add it as an explicit dependency.
- Remove any use of `bundlerPriority`.
- Unstable construct `EsbuildBundling` has been renamed to `EsbuildBundler` and its interface has slightly changed. Like most other constructs, it now takes `entryPoints` as first parameter, with an optional `props` object as the second.

## Versioning

This package _mostly_ follows [Semantic Versioning](https://semver.org/), with the exception of upgrades to `esbuild`. These will be released as **minor versions** and often include breaking changes from `esbuild`.

Although great care is taken to avoid this, all features marked as `@unstable` may change with minor versions. Please note that the unstable flag is applied to all new or experimental features and internal classes.

### Npm Tags

Some users prefer to use tags over version ranges. The following stable tags are available for use:

- `cdk-v1`, `cdk-v2` are provided for the latest release compatible with each version of the AWS CDK.

- `latest` is the most recent stable release.

These tags also exist, but usage is strongly not recommended:

- `unstable`, `next` are used for pre-release of the current and next major version respectively.

- ~~`cdk-1.x.x`~~ tags have been deprecated in favour of `cdk-v1`. Use that one instead.

## Future releases

### AWS CDK v2
Expand All @@ -154,3 +180,18 @@ The monolithic version 2 of CDK (aka Mono-CDK) is on the horizon. A new major re
### Stable esbuild

Once `esbuild` has reached a stable version 1.0, a new major version will be released for _all_ breaking changes, including updates to minimum (peer) dependencies.

## Library authors

When developing a library consumed by other packages, you'll most likely have to set `buildOptions.absWorkingDir`. The easiest way to do this, is to resolve based on the directory name of the file, and traverse the tree upwards to the root of your library package (that's where `package.json` and `tsconfig.json` are):

```ts
// file: project/src/index.ts
const props = {
buildOptions: {
absWorkingDir: path.resolve(__dirname, ".."), // now: /user/project
},
};
```

This will dynamically resolve to the correct path, wherever the package is installed.
10 changes: 5 additions & 5 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

## Supported Versions

Use this section to tell people about which versions of your project are
currently being supported with security updates.
Only the latest release of each major version is supported.

| Plugin Version | CDK Version | Supported |
| -------------- | ----------- | ------------------------------------------------ |
| 1.x.x | 1.x.x | :white_check_mark: only the latest minor version |
| Plugin Version | CDK Version | Supported |
| -------------- | ----------- | --------------------------- |
| v1 | ^1.99.0 | until release of AWS CDK v2 |
| v2 | ^1.99.0 | :white_check_mark: |

## Reporting a Vulnerability

Expand Down
33 changes: 27 additions & 6 deletions src/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,41 @@ import { Asset as S3Asset } from '@aws-cdk/aws-s3-assets';
import { AssetHashType, Construct, ConstructNode } from '@aws-cdk/core';
import { EsbuildBundler, BundlerProps, EntryPoints } from './bundler';

/**
* @internal
*/
export interface AssetBaseProps extends BundlerProps {
/**
* A hash of this asset, which is available at construction time.
*
* As this is a plain string, it
* can be used in construct IDs in order to enforce creation of a new resource when the content
* hash has changed.
* As this is a plain string, it can be used in construct IDs in order to enforce creation of a new resource when the content hash has changed.
*
* Defaults to a hash of all files in the resulting bundle.
*
* @stability stable
*/
readonly assetHash?: string;
}

export interface AssetProps extends AssetBaseProps {
/**
* Relative paths to the entrypoints of your code, e.g. `src/index.ts`
* A relative path or list or map of relative paths to the entry points of your code from the root of the project. E.g. `src/index.ts`.
*
* @stability stable
*/
readonly entryPoints: EntryPoints;
}

type JavaScriptAssetProps = AssetProps;
type TypeScriptAssetProps = AssetProps;

/**
* @stability stable
*/
abstract class Asset<Props extends AssetProps> extends S3Asset {
/**
* @stability stable
*/
public constructor(
scope: Construct,
id: string,
Expand Down Expand Up @@ -72,11 +85,19 @@ abstract class Asset<Props extends AssetProps> extends S3Asset {
}

/**
* @experimental
* Bundles the entry points and creates a CDK asset which is uploaded to the bootstrapped CDK S3 bucket during deployment.
*
* The asset can be used by other constructs.
*
* @stability stable
*/
export class JavaScriptAsset extends Asset<JavaScriptAssetProps> {}

/**
* @experimental
* Bundles the entry points and creates a CDK asset which is uploaded to the bootstrapped CDK S3 bucket during deployment.
*
* The asset can be used by other constructs.
*
* @stability stable
*/
export class TypeScriptAsset extends Asset<TypeScriptAssetProps> {}
57 changes: 51 additions & 6 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,83 @@ import { buildSync } from './esbuild-wrapper';
import { printBuildMessages } from './formatMessages';

/**
* @experimental
* A relative path or list or map of relative paths to the entry points of your code from the root of the project. E.g. `src/index.ts`.
*
* @stability stable
*/
export type EntryPoints = string | string[] | Record<string, string>;

/**
* @experimental
* @stability stable
*/
export interface BundlerProps {
/**
* Options passed on to esbuild.
* Build options passed on to esbuild. Please refer to the esbuild Build API docs for details.
*
* - `buildOptions.outdir: string` \
* The actual path for the output directory is defined by CDK. However setting this option allows to write files into a subdirectory. \
* For example `{ outdir: 'js' }` will create an asset with a single directory called `js`, which contains all built files. This approach can be useful for static website deployments, where JavaScript code should be placed into a subdirectory. \
* *Cannot be used together with `outfile`*.
* - `buildOptions.outfile: string` \
* Relative path to a file inside the CDK asset output directory. \
* For example `{ outfile: 'js/index.js' }` will create an asset with a single directory called `js`, which contains a single file `index.js`. This can be useful to rename the entry point. \
* *Cannot be used with multiple entryPoints or together with `outdir`.*
* - `buildOptions.absWorkingDir: string` \
* Absolute path to the [esbuild working directory](https://esbuild.github.io/api/#working-directory) and defaults to the [current working directory](https://en.wikipedia.org/wiki/Working_directory). \
* If paths cannot be found, a good starting point is to look at the concatenation of `absWorkingDir + entryPoint`. It must always be a valid absolute path pointing to the entry point. When needed, the probably easiest way to set absWorkingDir is to use a combination of `resolve` and `__dirname` (see "Library authors" section in the documentation).
*
* @see https://esbuild.github.io/api/#build-api
* @stability stable
*/
readonly buildOptions?: BuildOptions;

/**
* Relative path to a directory copied to the output BEFORE esbuild is run (i.e esbuild will overwrite existing files).
* Copy additional files to the output directory, before the build runs.
* Files copied like this will be overwritten by esbuild if they share the same name as any of the outputs.
*
* @stability stable
*/
readonly copyDir?: string;
}

/**
* @experimental
* Low-level construct that can be used where `BundlingOptions` are required.
* This class directly interfaces with esbuild and provides almost no configuration safeguards.
*
* @stability experimental
*/
export class EsbuildBundler {
/**
* Implementation of `ILocalBundling` interface, responsible for calling esbuild functions.
*
* @stability experimental
*/
public readonly local: ILocalBundling;

/**
* @deprecated This value is ignored since the bundler is always using a locally installed version of esbuild. However the property is required to comply with the `BundlingOptions` interface.
*
* @stability deprecated
*/
public readonly image = DockerImage.fromRegistry('scratch');

/**
* @stability experimental
*/
public constructor(
/**
* Relative paths to the entrypoints of your code, e.g. `src/index.ts`
* A relative path or list or map of relative paths to the entry points of your code from the root of the project.
* E.g. `src/index.ts`.
*
* @stability experimental
*/
public readonly entryPoints: EntryPoints,

/**
* Props to change the behaviour of the bundler.
*
* @stability experimental
*/
public readonly props: BundlerProps,
) {
if (props?.buildOptions?.outfile && props?.buildOptions?.outdir) {
Expand Down
Loading