@mrgrain/cdk-esbuild 4.0.0-alpha.8 → 4.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,97 @@
1
+
2
+ ## [4.0.0-beta.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.13.3...v4.0.0-beta.0) (2023-01-03)
3
+
4
+ ## [3.13.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.11.6...v3.13.0) (2022-12-18)
5
+
6
+
7
+ ### Features
8
+
9
+ * upgrade esbuild to 0.16.0 ([#279](https://github.com/mrgrain/cdk-esbuild/issues/279)) ([20bdefa](https://github.com/mrgrain/cdk-esbuild/commit/20bdefac870d13285e4ec4e2765b493f316e2320))
10
+
11
+ ### [3.11.6](https://github.com/mrgrain/cdk-esbuild/compare/v3.11.5...v3.11.6) (2022-12-17)
12
+
13
+ ### [3.11.5](https://github.com/mrgrain/cdk-esbuild/compare/v3.11.4...v3.11.5) (2022-12-03)
14
+
15
+ ### [3.11.4](https://github.com/mrgrain/cdk-esbuild/compare/v3.11.3...v3.11.4) (2022-11-05)
16
+
17
+ ### [3.11.3](https://github.com/mrgrain/cdk-esbuild/compare/v3.11.2...v3.11.3) (2022-10-29)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * bundler silently fails in case of an error ([#239](https://github.com/mrgrain/cdk-esbuild/issues/239)) ([3a63486](https://github.com/mrgrain/cdk-esbuild/commit/3a63486414f055e5a0b6b71bc443a649069fd034)), closes [#237](https://github.com/mrgrain/cdk-esbuild/issues/237)
23
+
24
+ ### [3.11.2](https://github.com/mrgrain/cdk-esbuild/compare/v3.11.1...v3.11.2) (2022-08-24)
25
+
26
+
27
+ ### Bug Fixes
28
+
29
+ * esbuild discovery can't find module installed in project ([#234](https://github.com/mrgrain/cdk-esbuild/issues/234)) ([58d7604](https://github.com/mrgrain/cdk-esbuild/commit/58d7604096fb4f7f638a873a90a714565a078d69)), closes [#233](https://github.com/mrgrain/cdk-esbuild/issues/233)
30
+
31
+ ### [3.11.1](https://github.com/mrgrain/cdk-esbuild/compare/v3.11.0...v3.11.1) (2022-08-22)
32
+
33
+
34
+ ### Bug Fixes
35
+
36
+ * `TransformOptions.tsconfigRaw` cannot receive an object ([#230](https://github.com/mrgrain/cdk-esbuild/issues/230)) ([1584ece](https://github.com/mrgrain/cdk-esbuild/commit/1584ecedaff5251f183f08aa512151010a54df16))
37
+
38
+ ## [3.11.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.10.0...v3.11.0) (2022-08-20)
39
+
40
+
41
+ ### Features
42
+
43
+ * auto install esbuild on jsii platforms ([#226](https://github.com/mrgrain/cdk-esbuild/issues/226)) ([d97688a](https://github.com/mrgrain/cdk-esbuild/commit/d97688a32cf62212b2756b91297c27248363619c))
44
+
45
+ ## [3.10.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.9.0...v3.10.0) (2022-08-14)
46
+
47
+
48
+ ### Features
49
+
50
+ * support custom esbuild module path ([#220](https://github.com/mrgrain/cdk-esbuild/issues/220)) ([9cc071e](https://github.com/mrgrain/cdk-esbuild/commit/9cc071edbb8f173288ec4b68162b1bbd1f0e18c2))
51
+
52
+ ## [3.9.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.8.1...v3.9.0) (2022-08-14)
53
+
54
+
55
+ ### Features
56
+
57
+ * lazy evaluate `InlineCode` and show message before transforming ([#217](https://github.com/mrgrain/cdk-esbuild/issues/217)) ([98f7d1d](https://github.com/mrgrain/cdk-esbuild/commit/98f7d1d23fd51967aa27c4fad62fafcb6a8cf802))
58
+ * support NO_COLOR ([#213](https://github.com/mrgrain/cdk-esbuild/issues/213)) ([1abd267](https://github.com/mrgrain/cdk-esbuild/commit/1abd2676f9aed67f31df70dc99671bafeb9929d5)), closes [#211](https://github.com/mrgrain/cdk-esbuild/issues/211)
59
+ * upgrade esbuild to ^0.15.0 ([#207](https://github.com/mrgrain/cdk-esbuild/issues/207)) ([7d74485](https://github.com/mrgrain/cdk-esbuild/commit/7d7448533a58a7422a2e5edb3858a74c232026d2)), closes [#204](https://github.com/mrgrain/cdk-esbuild/issues/204)
60
+
61
+ ### [3.8.1](https://github.com/mrgrain/cdk-esbuild/compare/v3.8.0...v3.8.1) (2022-08-13)
62
+
63
+
64
+ ### Bug Fixes
65
+
66
+ * remove console object overwrites from InlineCode ([#214](https://github.com/mrgrain/cdk-esbuild/issues/214)) ([dde3fdf](https://github.com/mrgrain/cdk-esbuild/commit/dde3fdf23780c77e13e7454fb42eb2a9fda84d7e))
67
+
68
+ ## [3.8.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.7.2...v3.8.0) (2022-08-13)
69
+
70
+
71
+ ### Features
72
+
73
+ * support new esbuild options: `platform`, `jsxDev`, `jsxImportSource` ([#209](https://github.com/mrgrain/cdk-esbuild/issues/209)) ([a7d14e7](https://github.com/mrgrain/cdk-esbuild/commit/a7d14e7bbf1655850951c3f5d041b045309b0ced))
74
+
75
+
76
+ ### Bug Fixes
77
+
78
+ * `esbuildBinaryPath` not working with `Code`, not available for `InlineCode` ([#210](https://github.com/mrgrain/cdk-esbuild/issues/210)) ([dc2609b](https://github.com/mrgrain/cdk-esbuild/commit/dc2609bf48956e3bc05a1d5fdb4851672cec3883)), closes [#203](https://github.com/mrgrain/cdk-esbuild/issues/203)
79
+ * esbuild messages printed out twice ([#212](https://github.com/mrgrain/cdk-esbuild/issues/212)) ([2596368](https://github.com/mrgrain/cdk-esbuild/commit/25963686f9f41e18af0f928e465263d1c87db611))
80
+
81
+ ### [3.7.2](https://github.com/mrgrain/cdk-esbuild/compare/v3.7.0...v3.7.2) (2022-07-15)
82
+
83
+
84
+ ### Bug Fixes
85
+
86
+ * only reset esbuild bin path if it was overwritten ([#190](https://github.com/mrgrain/cdk-esbuild/issues/190)) ([59c3c80](https://github.com/mrgrain/cdk-esbuild/commit/59c3c80b893c2b24180f7a61757de774157779dd))
87
+
88
+ ## [3.7.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.6.0...v3.7.0) (2022-07-13)
89
+
90
+
91
+ ### Features
92
+
93
+ * support absolute entry points, as long as they are within the working directory ([0e56b44](https://github.com/mrgrain/cdk-esbuild/commit/0e56b442a9b5c1874ee853721986f7f24d2ed455))
94
+
1
95
  ## [3.6.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.5.0...v3.6.0) (2022-06-23)
2
96
 
3
97
 
package/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2022 Moritz Kornher
1
+ Copyright (c) 2023 Moritz Kornher
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,55 +1,57 @@
1
- # cdk-esbuild
1
+ <picture>
2
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/mrgrain/cdk-esbuild/main/images/wordmark-dark.svg">
3
+ <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/mrgrain/cdk-esbuild/main/images/wordmark-light.svg">
4
+ <img src="https://raw.githubusercontent.com/mrgrain/cdk-esbuild/main/images/wordmark-dynamic.svg" alt="cdk-esbuild">
5
+ </picture>
2
6
 
3
7
  _CDK constructs for [esbuild](https://github.com/evanw/esbuild), an extremely fast JavaScript bundler_
4
8
 
5
- [Getting started](#getting-started)
6
- [Documentation](#documentation) | [API Reference](#api-reference) | [Versioning](#versioning) | [Upgrading from AWS CDK v1](#upgrading-from-aws-cdk-v1)
9
+ [Getting started](#getting-started) |
10
+ [Documentation](#documentation) | [API Reference](#api-reference) | [Python & .NET](#python-and-net) | [FAQ](#faq)
7
11
 
8
12
  [![View on Construct Hub](https://constructs.dev/badge?package=%40mrgrain%2Fcdk-esbuild)](https://constructs.dev/packages/@mrgrain/cdk-esbuild)
9
13
 
10
14
  ## Why?
11
15
 
12
- _esbuild_ is an extremely fast bundler and minifier for Typescript and JavaScript.
13
- This package makes _esbuild_ available to deploy lambda functions, static websites or to publish build artefacts (assets) for further use.
16
+ _esbuild_ is an extremely fast bundler and minifier for TypeScript and JavaScript.
17
+ This package makes _esbuild_ available to deploy AWS Lambda Functions, static websites or publish assets for further usage.
14
18
 
15
- AWS CDK [supports _esbuild_ with Lambda Functions](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-lambda-nodejs-readme.html). However the implementation cannot be used with any other Constructs and doesn't expose all of _esbuild_'s build interface.
16
-
17
- 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.
18
-
19
- **Production readiness**
20
-
21
- This package is stable and ready to be used in production, as many do. However _esbuild_ has not yet released a version 1.0.0 and its API is still in active development. Please read the guide on [esbuild's production readiness](https://esbuild.github.io/faq/#production-readiness).
22
-
23
- Notably upgrades of the _esbuild_ minimum version requirement will be introduced in **minor versions** of this package and will inherit breaking changes from _esbuild_.
19
+ AWS CDK [supports _esbuild_ for AWS Lambda Functions](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-lambda-nodejs-readme.html), but the implementation cannot be used with other Constructs and doesn't expose all of _esbuild_'s API.
24
20
 
25
21
  ## Getting started
26
22
 
27
- Install `cdk-esbuild`:
23
+ Install `cdk-esbuild` for Node.js using your favorite package manager:
28
24
 
29
- ```
30
- npm install @mrgrain/cdk-esbuild@3
25
+ ```sh
26
+ # npm
27
+ npm install @mrgrain/cdk-esbuild@4
28
+ # Yarn
29
+ yarn add @mrgrain/cdk-esbuild@4
30
+ # pnpm
31
+ pnpm add @mrgrain/cdk-esbuild@4
31
32
  ```
32
33
 
33
- If _peer_ or _optional dependencies_ are not installed automatically (e.g. when using npm v4-6), please use this command to install all of them:
34
+ For Python and .NET, use these commands:
34
35
 
35
- ```
36
- npm install @mrgrain/cdk-esbuild@3 esbuild
36
+ ```sh
37
+ # Python
38
+ pip install mrgrain.cdk-esbuild
39
+
40
+ # .NET
41
+ dotnet add package Mrgrain.CdkEsbuild
37
42
  ```
38
43
 
39
44
  ### AWS Lambda: Serverless function
40
45
 
41
- > 💡 See [Lambda Function](examples/lambda) for a complete working example of a how to deploy a lambda function.
46
+ > 💡 See [Lambda (TypeScript)](examples/typescript/lambda) and [Lambda (Python)](examples/typescript/lambda) for complete working examples of a how to deploy an AWS Lambda Function.
42
47
 
43
48
  Use `TypeScriptCode` as the `code` of a [lambda function](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Function.html#code):
44
49
 
45
50
  ```ts
46
- import * as lambda from "aws-cdk-lib/aws-lambda";
47
- import { TypeScriptCode } from "@mrgrain/cdk-esbuild";
48
-
49
- const bundledCode = new TypeScriptCode("src/index.ts");
51
+ const bundledCode = new TypeScriptCode("src/handler.ts");
50
52
 
51
53
  const fn = new lambda.Function(stack, "MyFunction", {
52
- runtime: lambda.Runtime.NODEJS_16_X,
54
+ runtime: lambda.Runtime.NODEJS_18_X,
53
55
  handler: "index.handler",
54
56
  code: bundledCode,
55
57
  });
@@ -57,21 +59,17 @@ const fn = new lambda.Function(stack, "MyFunction", {
57
59
 
58
60
  ### AWS S3: Static Website
59
61
 
60
- > 💡 See [Static Website with React](examples/website) for a complete working example of a how to deploy a React app to S3.
62
+ > 💡 See [React App (TypeScript)](examples/typescript/website) for a complete working example of a how to deploy a React app to S3.
61
63
 
62
64
  Use `TypeScriptSource` as one of the `sources` of a [static website deployment](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-s3-deployment-readme.html#roadmap):
63
65
 
64
66
  ```ts
65
- import * as s3 from "aws-cdk-lib/aws-s3";
66
- import * as s3deploy from "aws-cdk-lib/aws-s3-deployment";
67
- import { TypeScriptSource } from "@mrgrain/cdk-esbuild";
68
-
69
67
  const websiteBundle = new TypeScriptSource("src/index.tsx");
70
68
 
71
69
  const websiteBucket = new s3.Bucket(stack, "WebsiteBucket", {
72
70
  autoDeleteObjects: true,
73
71
  publicReadAccess: true,
74
- removalPolicy: RemovalPolicy.DESTROY,
72
+ removalPolicy: cdk.RemovalPolicy.DESTROY,
75
73
  websiteIndexDocument: "index.html",
76
74
  });
77
75
 
@@ -83,23 +81,18 @@ new s3deploy.BucketDeployment(stack, "DeployWebsite", {
83
81
 
84
82
  ### Amazon CloudWatch Synthetics: Canary monitoring
85
83
 
86
- > 💡 See [Monitored Website](examples/website) for a complete working example of a deployed and monitored website.
84
+ > 💡 See [Monitored Website (TypeScript)](examples/typescript/website) for a complete working example of a deployed and monitored website.
87
85
 
88
86
  Synthetics runs a canary to produce traffic to an application for monitoring purposes. Use `TypeScriptCode` as the `code` of a Canary test:
89
87
 
90
- > ℹ️ This feature depends on the `@aws-cdk/aws-synthetics-alpha` package which is a developer preview. You may need to update your source code when upgrading to a newer version of this package.
91
- >
92
- > ```
93
- > npm i @aws-cdk/aws-synthetics-alpha
94
- > ```
88
+ > ℹ️ This feature depends on `@aws-cdk/aws-synthetics-alpha` which is in developer preview.
89
+ > Please install the package using the respective tool of your language.
90
+ > You may need to update your source code when upgrading to a newer version of this alpha package.
95
91
 
96
92
  ```ts
97
- import * as synthetics from "@aws-cdk/aws-synthetics-alpha";
98
- import { TypeScriptCode } from "@mrgrain/cdk-esbuild";
99
-
100
- const bundledCode = new TypeScriptCode("src/index.ts", {
93
+ const bundledCode = new TypeScriptCode("src/canary.ts", {
101
94
  buildOptions: {
102
- outdir: "nodejs/node_modules", // This is required by Synthetics
95
+ outdir: "nodejs/node_modules", // This is required by AWS Synthetics
103
96
  },
104
97
  });
105
98
 
@@ -108,13 +101,13 @@ const canary = new synthetics.Canary(stack, "MyCanary", {
108
101
  test: synthetics.Test.custom({
109
102
  code: bundledCode,
110
103
  handler: "index.handler",
111
- });
104
+ }),
112
105
  });
113
106
  ```
114
107
 
115
108
  ## Documentation
116
109
 
117
- The package exports various different constructs for use with existing CDK features. A major guiding design principal for this package is to _extend, don't replace_. Expect constructs that you can provide as props, not complete replacements.
110
+ The package exports various different constructs for use with AWS CDK features. The guiding design principal for this package is _"extend, don't replace"_. Expect constructs that you can provide as props, not complete replacements.
118
111
 
119
112
  For use in **Lambda Functions** and **Synthetic Canaries**, the following classes implement `lambda.Code` ([reference](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Code.html)) and `synthetics.Code` ([reference](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-synthetics-alpha.Code.html)):
120
113
 
@@ -123,7 +116,6 @@ For use in **Lambda Functions** and **Synthetic Canaries**, the following classe
123
116
  Inline code is only supported by **Lambda**:
124
117
 
125
118
  - `InlineTypeScriptCode` & `InlineJavaScriptCode`
126
- - `InlineTsxCode` & `InlineJsxCode`
127
119
 
128
120
  For use with **S3 bucket deployments**, classes implementing `s3deploy.ISource` ([reference](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-s3-deployment-readme.html)):
129
121
 
@@ -139,129 +131,274 @@ The following classes power the other features. You normally won't have to use t
139
131
  - `EsbuildBundler` implements `core.BundlingOptions` ([reference](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.BundlingOptions.html)) \
140
132
  provides an interface for a _esbuild_ bundler wherever needed
141
133
 
134
+ - `EsbuildProvider` implements `IBuildProvider` and `ITransformProvider` \
135
+ provides the default _esbuild_ API implementation and can be replaced with a custom implementation
136
+
142
137
  ### [API Reference](API.md)
143
138
 
144
- Auto-generated reference for classes and structs. This information is also available as part of your IDE's code completion.
139
+ Auto-generated reference for Constructs, Classes and Structs.
140
+ This information is also available as part of your IDE's code completion.
145
141
 
146
- ### Escape hatches
142
+ ### Python and .NET
147
143
 
148
- It's possible that you want to use an implementation of esbuild that's different to the default one. Common reasons are:
144
+ _Esbuild_ requires a platform and architecture specific binary and currently has to be installed with a Node.js package manager like npm.
149
145
 
150
- - The current version constraints for esbuild are not suitable
151
- - To use a version of esbuild that is installed by any other means than `npm`, including Docker
152
- - Plugin support is needed for building
146
+ When using `cdk-esbuild` with Python or .NET, the package will automatically detect a local or global installation of the _esbuild_ npm package.If none can be found, it will fall back to dynamically installing a copy into a temporary location.
147
+ The exact algorithm of this mechanism must be treated as an implementation detail and should not be relied on.
148
+ It can however be configured to a certain extent.
149
+ See the examples below for more details.
153
150
 
154
- For these situations, this package offers an escape hatch to bypass regular the implementation and provide a custom build and transform function.
151
+ This "best effort" approach makes it easy to get started.
152
+ But is not always desirable, for example in environments with limited network access or when guaranteed repeatability of builds is a concern.
153
+ You have several options to opt-out of this behavior.
155
154
 
156
- #### Esbuild binary path
155
+ #### Provide a controlled installation of _esbuild_
156
+
157
+ The first step is to install a version of _esbuild_ that is controlled by you.
158
+
159
+ I **strongly recommend** to install _esbuild_ as a local package.
160
+ The easiest approach is to manage an additional Node.js project at the root of your AWS CDK project.
161
+ _esbuild_ can then be added to the `package.json` file and it is your responsibility to ensure required setup steps are taken in all environments like development machines and CI/CD systems.
157
162
 
158
- It is possible to override the binary used by esbuild. The usual way to do this is to set the `ESBUILD_BINARY_PATH` environment variable. For convenience this package allows to set the binary path as a prop:
163
+ Instead of installing the _esbuild_ package in a local project, it can also be **installed globally** with `npm install -g esbuild` or a similar command.
164
+ This approach might be preferred if a build container is prepared ahead of time, thus avoiding repeated package installation.
165
+
166
+ #### Change the automatic package detection
167
+
168
+ The second step is to make `cdk-esbuild` aware of your chosen install location.
169
+ This step is optional, but it's a good idea to have the location or at least the method explicitly defined.
170
+
171
+ To do this, you can set the `esbuildModulePath` prop on a `EsbuildProvider`.
172
+ Either pass a known, absolute or relative path as value, or use the `EsbuildSource` helper to set the detection method.
173
+ Please refer to the [`EsbuildSource`](API.md#esbuildsource) reference for a complete list of available methods.
159
174
 
160
175
  ```ts
161
- new TypeScriptCode("fixtures/handlers/ts-handler.ts", {
162
- esbuildBinaryPath: "path/to/esbuild/binary"
176
+ // Use the standard Node.js algorithm to detect a locally installed package
177
+ new EsbuildProvider({
178
+ esbuildModulePath: EsbuildSource.nodeJs(),
179
+ });
180
+
181
+ // Provide an explicit path
182
+ new EsbuildProvider({
183
+ esbuildModulePath: '/home/user/node_modules/esbuild/lib/main.js',
163
184
  });
164
185
  ```
165
186
 
166
- #### Custom build function
187
+ As a no-code approach, the `CDK_ESBUILD_MODULE_PATH` environment variable can be set in the same way.
188
+ An advantage of this is that the path can easily be changed for different systems.
189
+ Setting the env variable can be used with any installation approach, but is typically paired with a global installation of the _esbuild_ package.
190
+ Note that `esbuildModulePath` takes precedence.
167
191
 
168
- > 💡 See [Using esbuild with plugins](examples/esbuild-with-plugins) for a complete working example of a custom build function using this escape hatch.
192
+ #### Override the default detection method
169
193
 
170
- Constructs that result in starting a build, take a `buildFn` as optional prop. While the defined type for this function is `any`, it must implement the same signature as esbuild's `buildSync` function.
194
+ For an AWS CDK app with many instances of `TypeScriptCode` etc. it would be annoying to change the above for every single one of them.
195
+ Luckily, the default can be changed for all usages per app:
171
196
 
172
197
  ```ts
173
- new TypeScriptCode("fixtures/handlers/ts-handler.ts", {
174
- buildFn: (options: BuildOptions): BuildResult => {
175
- try {
176
- // custom implementation returning BuildResult
177
- } catch (error) {
178
- // throw BuildFailure exception here
179
- }
180
- },
198
+ const customModule = new EsbuildProvider({
199
+ esbuildModulePath: EsbuildSource.globalPaths(),
181
200
  });
201
+ EsbuildProvider.overrideDefaultProvider(customModule);
182
202
  ```
183
203
 
184
- Instead of esbuild, the provided function will be invoked with the calculated build options. The custom build function can amend, change or discard any of these. However integration with CDK relies heavily on the values `outdir`/`outfile` are set to and it's usually required to use them unchanged.
204
+ ### Customizing the Esbuild API
185
205
 
186
- Failures have to cause a `BuildFailure` exception in order to be fully handled.
206
+ This package uses the _esbuild_ JavaScript API.
207
+ In most situations the default API configuration will be suitable.
208
+ But sometimes it is required to configure _esbuild_ differently or even provide a custom implementation.
209
+ Common reasons for this are:
187
210
 
188
- #### Custom transform function
211
+ - Using a pre-installed version of _esbuild_ with Python or .NET
212
+ - If features not supported by the synchronous API are required, e.g. support for plugins
213
+ - If the default version constraints for _esbuild_ are not suitable
214
+ - To use a version of esbuild that is installed by any other means than `npm`, including Docker
189
215
 
190
- Constructs that result in starting a transformation, take a `transformFn` as optional prop. While the defined type for this function is `any`, it must implement the same signature as esbuild's `transformSync` function.
216
+ For these scenarios, this package offers customization options and an interface to provide a custom implementation:
191
217
 
192
218
  ```ts
219
+ declare const myCustomBuildProvider: IBuildProvider;
220
+
221
+ new TypeScriptCode("src/handler.ts", {
222
+ buildProvider: myCustomBuildProvider,
223
+ });
224
+
225
+
226
+ declare const myCustomTransformProvider: ITransformProvider;
227
+
193
228
  new InlineTypeScriptCode("let x: number = 1", {
194
- transformFn: (options: TransformOptions): TransformResult => {
195
- try {
196
- // custom implementation returning TransformResult
197
- } catch (error) {
198
- // throw TransformFailure exception here
199
- }
200
- },,
229
+ transformProvider: myCustomTransformProvider,
201
230
  });
202
231
  ```
203
232
 
204
- Instead of esbuild, the provided function will be invoked with the calculated transform options. The custom transform function can amend, change or discard any of these.
233
+ #### Esbuild binary path
205
234
 
206
- Failures have to cause a `TransformFailure` exception in order to be fully handled.
235
+ It is possible to override the binary used by _esbuild_ by setting a property on `EsbuildProvider`.
236
+ This is the same as setting the `ESBUILD_BINARY_PATH` environment variable.
237
+ Defining the `esbuildBinaryPath` prop takes precedence.
207
238
 
208
- ### Upgrading from AWS CDK v1
239
+ ```ts
240
+ const buildProvider = new EsbuildProvider({
241
+ esbuildBinaryPath: "path/to/esbuild/binary",
242
+ });
209
243
 
210
- This version is compatible with AWS CDK v2. For the previous, AWS CDK v1 compatible release, see [cdk-esbuild@v2](https://github.com/mrgrain/cdk-esbuild/tree/v2).
244
+ // This will use a different esbuild binary
245
+ new TypeScriptCode("src/handler.ts", { buildProvider });
246
+ ```
211
247
 
212
- To upgrade from AWS CDK v1 and cdk-esbuild@v2, please follow these steps:
248
+ #### Esbuild module path
213
249
 
214
- - This version requires AWS CDK v2. Follow the [official migration guide](https://docs.aws.amazon.com/cdk/latest/guide/work-with-cdk-v2.html) to upgrade.
215
- - Update the package dependency to v3: `npm install --save @mrgrain/cdk-esbuild@^3.0.0`
216
- - `esbuild` is installed as an optional dependency. If your setup does not automatically install optional dependencies, make sure to add it as an explicit dependency.
217
- - Any use of `InlineCode` variations has to be updated. Previously the second parameter was either of type `TransformerProps` or `TransformOptions`. Now it must be `TransformerProps`.\
218
- If the passed value is of type `TransformOptions`, turn it into the correct type like this:
250
+ The Node.js module discovery algorithm will normally be used to find the _esbuild_ package.
251
+ It can be useful to use specify a different module path, for example if a globally installed package should be used instead of a local version.
219
252
 
220
- ```ts
221
- const oldOptions: TransformOptions = {...}
253
+ ```ts
254
+ const buildProvider = new EsbuildProvider({
255
+ esbuildModulePath: "/home/user/node_modules/esbuild/lib/main.js",
256
+ });
222
257
 
223
- new InlineTypeScriptCode('// inline code', {
224
- transformOptions: oldOptions
225
- });
226
- ```
258
+ // This will use a different esbuild module
259
+ new TypeScriptCode("src/handler.ts", { buildProvider });
260
+ ```
227
261
 
228
- ## Versioning
262
+ Alternatively supported by setting the `CDK_ESBUILD_MODULE_PATH` environment variable, which will apply to all uses.
263
+ Defining the `esbuildModulePath` prop takes precedence.
229
264
 
230
- This package 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`.
265
+ If you are a Python or .NET users, see the language specific guide for a more detailed expose on this feature.
231
266
 
232
- ### Npm Tags
267
+ #### Custom Build and Transform API implementations
233
268
 
234
- Some users prefer to use tags over version ranges. The following stable tags are available for use:
269
+ > 💡 See [esbuild plugins w/ TypeScript](examples/typescript/esbuild-with-plugins) for a complete working example of a custom Build API implementation.
235
270
 
236
- - `cdk-v1`, `cdk-v2` are provided for the latest release compatible with each version of the AWS CDK.
271
+ A custom implementation can be provided by implementing `IBuildProvider` or `ITransformProvider`:
237
272
 
238
- - `latest` is the most recent stable release.
273
+ ```ts
274
+ class CustomEsbuild implements IBuildProvider, ITransformProvider {
275
+ buildSync(options: BuildOptions): void {
276
+ // custom implementation goes here
277
+ }
278
+
279
+ transformSync(code: string, options?: TransformOptions): string {
280
+ // custom implementation goes here, return transformed code
281
+ return 'transformed code';
282
+ }
283
+ }
239
284
 
240
- These tags also exist, but usage is strongly not recommended:
285
+ // These will use the custom implementation
286
+ new TypeScriptCode("src/handler.ts", {
287
+ buildProvider: new CustomEsbuild(),
288
+ });
289
+ new InlineTypeScriptCode("let x: number = 1", {
290
+ transformProvider: new CustomEsbuild(),
291
+ });
292
+ ```
293
+
294
+ Instead of _esbuild_, the custom methods will be invoked with all computed options.
295
+ Custom implementations can amend, change or discard any of the options.
241
296
 
242
- - `unstable`, `next` are used for pre-release of the current and next major version respectively.
297
+ The `IBuildProvider` integration with CDK relies on the `outdir`/`outfile` values and it's usually required to use them unchanged.
243
298
 
244
- - ~~`cdk-1.x.x`~~ tags have been deprecated in favour of `cdk-v1`. Use that one instead.
299
+ `ITransformProvider` must return the transformed code as a string.
245
300
 
246
- ## Roadmap & Contributions
301
+ Failures and warnings should be printed to stderr and thrown as the respective _esbuild_ error.
247
302
 
248
- [The project's roadmap is available on GitHub.](https://github.com/mrgrain/cdk-esbuild/projects/1) Please submit any feature requests as issues to the repository.
303
+ #### Overriding the default implementation providers
249
304
 
305
+ The default implementation can also be set for all usages of `TypeScriptCode` etc. in an AWS CDK app.
306
+ You can change the default for both APIs at once or set a different implementation for each of them.
307
+
308
+ ```ts
309
+ const myCustomEsbuildProvider = new MyCustomEsbuildProvider();
310
+
311
+ EsbuildProvider.overrideDefaultProvider(myCustomEsbuildProvider);
312
+ EsbuildProvider.overrideDefaultBuildProvider(myCustomEsbuildProvider);
313
+ EsbuildProvider.overrideDefaultTransformationProvider(myCustomEsbuildProvider);
314
+
315
+ // This will use the custom provider without the need to define it as a prop
316
+ new TypeScriptCode("src/handler.ts");
317
+ ```
318
+
319
+ ### Roadmap & Contributions
320
+
321
+ [The project's roadmap is available on GitHub.](https://github.com/users/mrgrain/projects/1/views/7)
322
+
323
+ Please submit feature requests as issues to the repository.
250
324
  All contributions are welcome, no matter if they are for already planned or completely new features.
251
325
 
252
- ## Library authors
326
+ ## FAQ
327
+
328
+ ### Should I use this package in production?
329
+
330
+ This package is stable and ready to be used in production, as many do.
331
+ However _esbuild_ has not yet released a version 1.0.0 and its API is still in active development.
332
+ Please read the guide on [esbuild's production readiness](https://esbuild.github.io/faq/#production-readiness).
333
+
334
+ Note that _esbuild_ minor version upgrades are also introduced in **minor versions** of this package.
335
+ Since _esbuild_ is pre v1, these versions typically introduce breaking changes and this package will inherit them.
336
+ To avoid this behavior, add the desired _esbuild_ version as a dependency to your package.
337
+
338
+ ### How do I upgrade from `cdk-esbuild` v3?
339
+
340
+ Please refer to the [v4 release notes](https://github.com/mrgrain/cdk-esbuild/releases/tag/v4.0.0) for a list of backwards incompatible changes and respective upgrade instructions.
341
+
342
+ ### [TS/JS] Why am I getting the error `Cannot find module 'esbuild'`?
253
343
 
254
- Building a library consumed by other packages that relies on `cdk-esbuild` might require you to set `buildOptions.absWorkingDir`. The easiest way to do this, is to resolve based on the directory name of the calling file, and traverse the tree upwards to the root of your library package (that's where `package.json` and `tsconfig.json` are):
344
+ This package depends on _esbuild_ as an optional dependencies. If optional dependencies are not installed automatically on your system (e.g. when using npm v4-6), install _esbuild_ explicitly:
345
+
346
+ ```console
347
+ npm install esbuild
348
+ ```
349
+
350
+ ### [TS/JS] How can I use a different version of _esbuild_?
351
+
352
+ Use the [override](https://docs.npmjs.com/cli/v9/configuring-npm/package-json?v=true#overrides) instructions for your package manager to force a specific version, for example:
353
+
354
+ ```json
355
+ {
356
+ "overrides": {
357
+ "esbuild": "0.14.7"
358
+ }
359
+ }
360
+ ```
361
+
362
+ Build and Transform interfaces are relatively stable across _esbuild_ versions.
363
+ However if any incompatibilities occur, `buildOptions` / `transformOptions` can be cast to `any`:
255
364
 
256
365
  ```ts
257
- // file: project/src/index.ts
366
+ const bundledCode = new TypeScriptCode("src/handler.ts", {
367
+ buildOptions: {
368
+ unsupportedOption: "value"
369
+ } as any,
370
+ });
371
+ ```
372
+
373
+ ### [Python/.NET] How can I use a different version of _esbuild_?
374
+
375
+ Install the desired version of _esbuild_ locally or globally [as described in the documentation above](#python-and-dotnet).
376
+
377
+ Build and Transform interfaces are relatively stable across _esbuild_ versions.
378
+ However if any incompatibilities occur, use the appropriate language features to cast any incompatible `buildOptions` / `transformOptions` to the correct types.
379
+
380
+ ### Can I use this package in my published AWS CDK Construct?
381
+
382
+ It is possible to use `cdk-esbuild` in a published AWS CDK Construct library, but not recommended.
383
+ Always prefer to ship a compiled `.js` file or even bundle a zip archive in your package.
384
+ For AWS Lambda Functions, [projen provides an excellent solution](https://projen.io/awscdk.html#aws-lambda-functions).
385
+
386
+ If you do end up consuming `cdk-esbuild`, you will have to set `buildOptions.absWorkingDir`. The easiest way to do this, is to resolve the path based on the directory name of the calling file:
387
+
388
+ ```js
389
+ // file: node_modules/construct-library/src/index.ts
258
390
  const props = {
259
391
  buildOptions: {
260
- absWorkingDir: path.resolve(__dirname, ".."), // now: /user/project
392
+ absWorkingDir: path.resolve(__dirname, ".."),
393
+ // now: /user/local-app/node_modules/construct-library
261
394
  },
262
395
  };
263
396
  ```
264
397
 
265
- This will dynamically resolve to the correct path, wherever the package is installed.
398
+ This will dynamically resolve to the correct path, wherever the package is installed. Please open an issue if you encounter any difficulties.
399
+
400
+ ### Can I use this package with AWS CDK v1?
401
+
402
+ Yes, the `2.x.x` line of `cdk-esbuild` is compatible with AWS CDK v1. You can find the [documentation for it on the v2 branch](https://github.com/mrgrain/cdk-esbuild/tree/v2).
266
403
 
267
- Please open an issue if you encounter any difficulties.
404
+ However, in line with AWS CDK v2, this version release now only receives security updates and critical bug fixes and support will fully end on June 1, 2023.
package/lib/asset.d.ts CHANGED
@@ -18,7 +18,18 @@ export interface AssetBaseProps extends BundlerProps {
18
18
  }
19
19
  export interface AssetProps extends AssetBaseProps {
20
20
  /**
21
- * 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`.
21
+ * A path or list or map of paths to the entry points of your code.
22
+ *
23
+ * Relative paths are by default resolved from the current working directory.
24
+ * To change the working directory, see `buildOptions.absWorkingDir`.
25
+ *
26
+ * Absolute paths can be used if files are part of the working directory.
27
+ *
28
+ * Examples:
29
+ * - `'src/index.ts'`
30
+ * - `require.resolve('./lambda')`
31
+ * - `['src/index.ts', 'src/util.ts']`
32
+ * - `{one: 'src/two.ts', two: 'src/one.ts'}`
22
33
  *
23
34
  * @stability stable
24
35
  */