@mrgrain/cdk-esbuild 4.0.0-alpha.7 → 4.0.0-beta.0

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.
Files changed (51) hide show
  1. package/.jsii +1573 -588
  2. package/CHANGELOG.md +104 -0
  3. package/LICENSE +1 -1
  4. package/README.md +223 -102
  5. package/lib/asset.d.ts +20 -5
  6. package/lib/asset.js +31 -19
  7. package/lib/bundler.d.ts +51 -28
  8. package/lib/bundler.js +28 -17
  9. package/lib/code.d.ts +82 -16
  10. package/lib/code.js +71 -25
  11. package/lib/esbuild-types.d.ts +49 -18
  12. package/lib/esbuild-types.js +1 -1
  13. package/lib/index.d.ts +5 -4
  14. package/lib/index.js +8 -7
  15. package/lib/inline-code.d.ts +29 -77
  16. package/lib/inline-code.js +53 -104
  17. package/lib/private/dynamic-package.d.ts +41 -0
  18. package/lib/private/dynamic-package.js +115 -0
  19. package/lib/private/esbuild-source.d.ts +33 -0
  20. package/lib/private/esbuild-source.js +59 -0
  21. package/lib/private/utils.d.ts +7 -0
  22. package/lib/private/utils.js +23 -0
  23. package/lib/provider.d.ts +129 -0
  24. package/lib/provider.js +115 -0
  25. package/lib/source.d.ts +81 -5
  26. package/lib/source.js +84 -8
  27. package/package.json +33 -25
  28. package/rosetta/default.ts-fixture +32 -0
  29. package/.gitattributes +0 -24
  30. package/.projenrc.ts +0 -208
  31. package/API.md +0 -2477
  32. package/SECURITY.md +0 -19
  33. package/lib/esbuild-polyfill.js +0 -2302
  34. package/lib/esbuild-wrapper.d.ts +0 -3
  35. package/lib/esbuild-wrapper.js +0 -31
  36. package/lib/formatMessages.d.ts +0 -18
  37. package/lib/formatMessages.js +0 -34
  38. package/node_modules/isexe/.npmignore +0 -2
  39. package/node_modules/isexe/LICENSE +0 -15
  40. package/node_modules/isexe/README.md +0 -51
  41. package/node_modules/isexe/index.js +0 -57
  42. package/node_modules/isexe/mode.js +0 -41
  43. package/node_modules/isexe/package.json +0 -35
  44. package/node_modules/isexe/test/basic.js +0 -221
  45. package/node_modules/isexe/windows.js +0 -42
  46. package/node_modules/which/CHANGELOG.md +0 -166
  47. package/node_modules/which/LICENSE +0 -15
  48. package/node_modules/which/README.md +0 -54
  49. package/node_modules/which/bin/node-which +0 -52
  50. package/node_modules/which/package.json +0 -47
  51. package/node_modules/which/which.js +0 -125
package/CHANGELOG.md CHANGED
@@ -1,4 +1,108 @@
1
1
 
2
+ ## [3.13.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.11.6...v3.13.0) (2022-12-18)
3
+
4
+
5
+ ### Features
6
+
7
+ * upgrade esbuild to 0.16.0 ([#279](https://github.com/mrgrain/cdk-esbuild/issues/279)) ([20bdefa](https://github.com/mrgrain/cdk-esbuild/commit/20bdefac870d13285e4ec4e2765b493f316e2320))
8
+
9
+ ### [3.11.6](https://github.com/mrgrain/cdk-esbuild/compare/v3.11.5...v3.11.6) (2022-12-17)
10
+
11
+ ### [3.11.5](https://github.com/mrgrain/cdk-esbuild/compare/v3.11.4...v3.11.5) (2022-12-03)
12
+
13
+ ### [3.11.4](https://github.com/mrgrain/cdk-esbuild/compare/v3.11.3...v3.11.4) (2022-11-05)
14
+
15
+ ### [3.11.3](https://github.com/mrgrain/cdk-esbuild/compare/v3.11.2...v3.11.3) (2022-10-29)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * 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)
21
+
22
+ ### [3.11.2](https://github.com/mrgrain/cdk-esbuild/compare/v3.11.1...v3.11.2) (2022-08-24)
23
+
24
+
25
+ ### Bug Fixes
26
+
27
+ * 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)
28
+
29
+ ### [3.11.1](https://github.com/mrgrain/cdk-esbuild/compare/v3.11.0...v3.11.1) (2022-08-22)
30
+
31
+
32
+ ### Bug Fixes
33
+
34
+ * `TransformOptions.tsconfigRaw` cannot receive an object ([#230](https://github.com/mrgrain/cdk-esbuild/issues/230)) ([1584ece](https://github.com/mrgrain/cdk-esbuild/commit/1584ecedaff5251f183f08aa512151010a54df16))
35
+
36
+ ## [3.11.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.10.0...v3.11.0) (2022-08-20)
37
+
38
+
39
+ ### Features
40
+
41
+ * auto install esbuild on jsii platforms ([#226](https://github.com/mrgrain/cdk-esbuild/issues/226)) ([d97688a](https://github.com/mrgrain/cdk-esbuild/commit/d97688a32cf62212b2756b91297c27248363619c))
42
+
43
+ ## [3.10.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.9.0...v3.10.0) (2022-08-14)
44
+
45
+
46
+ ### Features
47
+
48
+ * support custom esbuild module path ([#220](https://github.com/mrgrain/cdk-esbuild/issues/220)) ([9cc071e](https://github.com/mrgrain/cdk-esbuild/commit/9cc071edbb8f173288ec4b68162b1bbd1f0e18c2))
49
+
50
+ ## [3.9.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.8.1...v3.9.0) (2022-08-14)
51
+
52
+
53
+ ### Features
54
+
55
+ * 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))
56
+ * 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)
57
+ * 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)
58
+
59
+ ### [3.8.1](https://github.com/mrgrain/cdk-esbuild/compare/v3.8.0...v3.8.1) (2022-08-13)
60
+
61
+
62
+ ### Bug Fixes
63
+
64
+ * remove console object overwrites from InlineCode ([#214](https://github.com/mrgrain/cdk-esbuild/issues/214)) ([dde3fdf](https://github.com/mrgrain/cdk-esbuild/commit/dde3fdf23780c77e13e7454fb42eb2a9fda84d7e))
65
+
66
+ ## [3.8.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.7.2...v3.8.0) (2022-08-13)
67
+
68
+
69
+ ### Features
70
+
71
+ * 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))
72
+
73
+
74
+ ### Bug Fixes
75
+
76
+ * `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)
77
+ * esbuild messages printed out twice ([#212](https://github.com/mrgrain/cdk-esbuild/issues/212)) ([2596368](https://github.com/mrgrain/cdk-esbuild/commit/25963686f9f41e18af0f928e465263d1c87db611))
78
+
79
+ ### [3.7.2](https://github.com/mrgrain/cdk-esbuild/compare/v3.7.0...v3.7.2) (2022-07-15)
80
+
81
+
82
+ ### Bug Fixes
83
+
84
+ * 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))
85
+
86
+ ## [3.7.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.6.0...v3.7.0) (2022-07-13)
87
+
88
+
89
+ ### Features
90
+
91
+ * support absolute entry points, as long as they are within the working directory ([0e56b44](https://github.com/mrgrain/cdk-esbuild/commit/0e56b442a9b5c1874ee853721986f7f24d2ed455))
92
+
93
+ ## [3.6.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.5.0...v3.6.0) (2022-06-23)
94
+
95
+
96
+ ### Features
97
+
98
+ * allow setting of esbuildBinaryPath via Construct interface ([f4eeebe](https://github.com/mrgrain/cdk-esbuild/commit/f4eeebe613bf20b8b28313a81b328bdcd1c1a8e6))
99
+ * upgrade esbuild to support `supported` buildOption and new `copy` loader ([3ac5d92](https://github.com/mrgrain/cdk-esbuild/commit/3ac5d925342505669dbb3d3e88249f6e495b8566))
100
+
101
+
102
+ ### Bug Fixes
103
+
104
+ * make TypeScriptCode and JavaScriptCode correctly extend aws_lambda.Code in jsii ([d04db27](https://github.com/mrgrain/cdk-esbuild/commit/d04db2798559b38d424a2012ebb96d14ebde4fb0))
105
+
2
106
  ## [3.5.0](https://github.com/mrgrain/cdk-esbuild/compare/v3.4.0...v3.5.0) (2022-06-02)
3
107
 
4
108
 
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,63 @@
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) | [Migrating to v3](#migrating-to-v3) |
6
- [Documentation](#documentation) | [API Reference](#api-reference) | [Versioning](#versioning)
9
+ [Getting started](#getting-started) |
10
+ [Documentation](#documentation) | [API Reference](#api-reference) | [FAQ](#faq)
7
11
 
8
- > 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).
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
16
  _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.
17
+ This package makes _esbuild_ available to deploy AWS Lambda Functions, static websites and publish assets for use.
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.
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.
18
20
 
19
21
  **Production readiness**
20
22
 
21
23
  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
24
 
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_.
25
+ _Esbuild_ minor version upgrades are introduced in **minor versions** of this package and inherit breaking changes from _esbuild_.
24
26
 
25
27
  ## Getting started
26
28
 
27
- Install `cdk-esbuild`:
29
+ Install `cdk-esbuild` for Node.js with your favorite package manager:
28
30
 
29
- ```
30
- npm install @mrgrain/cdk-esbuild@3
31
+ ```sh
32
+ # npm
33
+ npm install @mrgrain/cdk-esbuild@4
34
+ # Yarn
35
+ yarn add @mrgrain/cdk-esbuild@4
36
+ # pnpm
37
+ pnpm add @mrgrain/cdk-esbuild@4
31
38
  ```
32
39
 
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:
40
+ For Python and Dotnet, use these commands:
34
41
 
35
- ```
36
- npm install @mrgrain/cdk-esbuild@3 esbuild
42
+ ```sh
43
+ # Python
44
+ pip install streamlink-serverless
45
+
46
+ # Dotnet
47
+ dotnet add package StreamlinkServerless
37
48
  ```
38
49
 
39
50
  ### AWS Lambda: Serverless function
40
51
 
41
- > 💡 See [Lambda Function](examples/lambda) for a complete working example of a how to deploy a lambda function.
52
+ > 💡 See [Lambda Function](examples/typescript/lambda) for a complete working example of a how to deploy a lambda function.
42
53
 
43
54
  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
55
 
45
56
  ```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");
57
+ const bundledCode = new TypeScriptCode("src/handler.ts");
50
58
 
51
59
  const fn = new lambda.Function(stack, "MyFunction", {
52
- runtime: lambda.Runtime.NODEJS_16_X,
60
+ runtime: lambda.Runtime.NODEJS_18_X,
53
61
  handler: "index.handler",
54
62
  code: bundledCode,
55
63
  });
@@ -57,21 +65,17 @@ const fn = new lambda.Function(stack, "MyFunction", {
57
65
 
58
66
  ### AWS S3: Static Website
59
67
 
60
- > 💡 See [Static Website with React](examples/website) for a complete working example of a how to deploy a React app to S3.
68
+ > 💡 See [Static Website with React](examples/typescript/website) for a complete working example of a how to deploy a React app to S3.
61
69
 
62
70
  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
71
 
64
72
  ```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
73
  const websiteBundle = new TypeScriptSource("src/index.tsx");
70
74
 
71
75
  const websiteBucket = new s3.Bucket(stack, "WebsiteBucket", {
72
76
  autoDeleteObjects: true,
73
77
  publicReadAccess: true,
74
- removalPolicy: RemovalPolicy.DESTROY,
78
+ removalPolicy: cdk.RemovalPolicy.DESTROY,
75
79
  websiteIndexDocument: "index.html",
76
80
  });
77
81
 
@@ -83,21 +87,18 @@ new s3deploy.BucketDeployment(stack, "DeployWebsite", {
83
87
 
84
88
  ### Amazon CloudWatch Synthetics: Canary monitoring
85
89
 
86
- > 💡 See [Monitored Website](examples/website) for a complete working example of a deployed and monitored website.
90
+ > 💡 See [Monitored Website](examples/typescript/website) for a complete working example of a deployed and monitored website.
87
91
 
88
92
  Synthetics runs a canary to produce traffic to an application for monitoring purposes. Use `TypeScriptCode` as the `code` of a Canary test:
89
93
 
90
94
  > ℹ️ 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
95
  >
92
- > ```
96
+ > ```sh
93
97
  > npm i @aws-cdk/aws-synthetics-alpha
94
98
  > ```
95
99
 
96
100
  ```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", {
101
+ const bundledCode = new TypeScriptCode("src/canary.ts", {
101
102
  buildOptions: {
102
103
  outdir: "nodejs/node_modules", // This is required by Synthetics
103
104
  },
@@ -108,7 +109,7 @@ const canary = new synthetics.Canary(stack, "MyCanary", {
108
109
  test: synthetics.Test.custom({
109
110
  code: bundledCode,
110
111
  handler: "index.handler",
111
- });
112
+ }),
112
113
  });
113
114
  ```
114
115
 
@@ -123,7 +124,6 @@ For use in **Lambda Functions** and **Synthetic Canaries**, the following classe
123
124
  Inline code is only supported by **Lambda**:
124
125
 
125
126
  - `InlineTypeScriptCode` & `InlineJavaScriptCode`
126
- - `InlineTsxCode` & `InlineJsxCode`
127
127
 
128
128
  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
129
 
@@ -139,129 +139,250 @@ The following classes power the other features. You normally won't have to use t
139
139
  - `EsbuildBundler` implements `core.BundlingOptions` ([reference](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.BundlingOptions.html)) \
140
140
  provides an interface for a _esbuild_ bundler wherever needed
141
141
 
142
+ - `EsbuildProvider` implements `IBuildProvider` and `ITransformProvider` \
143
+ provides the default _esbuild_ API implementation and can be replaced with a custom implementation
144
+
142
145
  ### [API Reference](API.md)
143
146
 
144
- Auto-generated reference for classes and structs. This information is also available as part of your IDE's code completion.
147
+ Auto-generated reference for Classes and Structs. This information is also available as part of your IDE's code completion.
148
+
149
+ ### Python and Dotnet
150
+
151
+ Because _esbuild_ requires a platform and architecture specific binary, it currently has to be installed using npm (or any other Node.js package manager).
152
+
153
+ When `cdk-esbuild` is used with Python or Dotnet, it will automatically detect a local or global installation of the _esbuild_ npm package - or fallback to dynamically installing a copy into a temporary location.
154
+ The exact algorithm of this mechanism must be treated as an implementation detail and should not be relied on.
155
+ It can however be configured to a certain extent.
156
+ See the examples below for more details.
157
+
158
+ While this "best effort" approach makes it easy to get started, it is not always desirable.
159
+ For example in environments with limited network access or when guaranteed repeatability of builds is a concern.
160
+ You have several options to opt-out of this behavior:
161
+
162
+ - **Recommended** - Install _esbuild_ as a local package\
163
+ The recommended approach is to manage an additional Node.js project in the root of your AWS CDK project.
164
+ _esbuild_ should then be added to the `package.json` file and it is your responsibility to ensure the required setup steps are run in every environment (development machines & CI/CD systems).
165
+ The _esbuild_ package will then be detected automatically.
166
+ - Install _esbuild_ as a global package\
167
+ Instead of installing the package in a local project, it can also be installed globally with `npm install -g esbuild` or a similar command.
168
+ The _esbuild_ package will be detected automatically from the global installation.
169
+ This approach might be preferred if a build container is prepared ahead of time, thus avoiding repeated package installation.
170
+ - Set `CDK_ESBUILD_MODULE_PATH` env variable\
171
+ If an installed _esbuild_ module cannot be reliably detected by the algorithm, you can provide the absolute path to the module as an environment variable.
172
+ This approach has the advantage that the location of the module can be different on different systems.
173
+ While it can be combined with either installation approach, it is usually used with a global installation of the _esbuild_ package.
174
+ - Set `esbuildModulePath` prop\
175
+ Similar to setting the module path via env variable, it can also be passed as the `esbuildModulePath` prop to a `EsbuildProvider`:
176
+
177
+ ```ts
178
+ new EsbuildProvider({
179
+ esbuildModulePath: '/home/user/node_modules/esbuild/lib/main.js'
180
+ });
181
+ ```
182
+
183
+ - Override the default implementation provider\
184
+ Using the previous approach, but setting it for every usage:
185
+
186
+ ```ts
187
+ const customModule = new EsbuildProvider({
188
+ esbuildModulePath: '/home/user/node_modules/esbuild/lib/main.js'
189
+ });
190
+ EsbuildProvider.overrideDefaultProvider(customModule);
191
+ ```
192
+
193
+ The `esbuildModulePath` can be provided as a known, absolute or relative path.
194
+ When using the programmatic interface, this package additionally offers some helper methods to influence to fine-tune to automatic detection algorithm:
195
+
196
+ ```ts
197
+ // This will force installation to a temporary location
198
+ new EsbuildProvider({
199
+ esbuildModulePath: EsbuildSource.install()
200
+ });
201
+ ```
202
+
203
+ Please see the [`EsbuildSource`](API.md#esbuildsource) reference for a list of available methods.
145
204
 
146
- ### Escape hatches
205
+ ### Customizing the Esbuild API
147
206
 
148
- It's possible that you want to use an implementation of esbuild that's different to the default one. Common reasons are:
207
+ This package uses the _esbuild_ JavaScript API. For most use cases the default configuration will be suitable.
149
208
 
150
- - The current version constraints for esbuild are not suitable
209
+ In some cases it might be useful to configure _esbuild_ differently or even provide a completely custom implementation.
210
+ Common examples for this are:
211
+
212
+ - To use a pre-installed version of _esbuild_ with Python and Dotnet
213
+ - If features not supported by the synchronous API are required, e.g. support for plugins
214
+ - If the default version constraints for _esbuild_ are not suitable
151
215
  - To use a version of esbuild that is installed by any other means than `npm`, including Docker
152
- - Plugin support is needed for building
153
216
 
154
- For these situations, this package offers an escape hatch to bypass regular the implementation and provide a custom build and transform function.
217
+ For these scenarios, this package offers customization options and an interface to provide a custom implementation:
218
+
219
+ ```ts
220
+ declare const myCustomBuildProvider: IBuildProvider;
221
+
222
+ new TypeScriptCode("src/handler.ts", {
223
+ buildProvider: myCustomBuildProvider,
224
+ });
225
+
226
+
227
+ declare const myCustomTransformProvider: ITransformProvider;
228
+
229
+ new InlineTypeScriptCode("let x: number = 1", {
230
+ transformProvider: myCustomTransformProvider,
231
+ });
232
+ ```
155
233
 
156
234
  #### Esbuild binary path
157
235
 
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:
236
+ It is possible to override the binary used by _esbuild_ by setting a property on `EsbuildProvider`.
237
+ This is the same as setting the `ESBUILD_BINARY_PATH` environment variable.
238
+ Defining the `esbuildBinaryPath` prop takes precedence.
159
239
 
160
240
  ```ts
161
- new TypeScriptCode("fixtures/handlers/ts-handler.ts", {
162
- esbuildBinaryPath: "path/to/esbuild/binary"
241
+ const buildProvider = new EsbuildProvider({
242
+ esbuildBinaryPath: "path/to/esbuild/binary",
163
243
  });
164
- ```
165
244
 
166
- #### Custom build function
245
+ // This will use a different esbuild binary
246
+ new TypeScriptCode("src/handler.ts", { buildProvider });
247
+ ```
167
248
 
168
- > 💡 See [Using esbuild with plugins](examples/esbuild-with-plugins) for a complete working example of a custom build function using this escape hatch.
249
+ #### Esbuild module path
169
250
 
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.
251
+ The Node.js module discovery algorithm will normally be used to find the _esbuild_ package.
252
+ 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.
171
253
 
172
254
  ```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
- },
255
+ const buildProvider = new EsbuildProvider({
256
+ esbuildModulePath: "/home/user/node_modules/esbuild/lib/main.js",
181
257
  });
258
+
259
+ // This will use a different esbuild module
260
+ new TypeScriptCode("src/handler.ts", { buildProvider });
182
261
  ```
183
262
 
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.
263
+ Alternatively supported by setting the `CDK_ESBUILD_MODULE_PATH` environment variable, which will apply to all uses.
264
+ Defining the `esbuildModulePath` prop takes precedence.
185
265
 
186
- Failures have to cause a `BuildFailure` exception in order to be fully handled.
266
+ #### Custom Build and Transform API implementations
187
267
 
188
- #### Custom transform function
268
+ > 💡 See [Using esbuild with plugins](examples/typescript/esbuild-with-plugins) for a complete working example of a custom Build API implementation.
189
269
 
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.
270
+ A custom implementation can be provided by implementing `IBuildProvider` or `ITransformProvider`:
191
271
 
192
272
  ```ts
193
- 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
273
+ class CustomEsbuild implements IBuildProvider, ITransformProvider {
274
+ buildSync(options: BuildOptions): void {
275
+ // custom implementation goes here
276
+ }
277
+
278
+ transformSync(code: string, options?: TransformOptions): string {
279
+ // custom implementation goes here, return transformed code
280
+ return 'transformed code';
199
281
  }
200
- },,
282
+ }
283
+
284
+ // These will use the custom implementation
285
+ new TypeScriptCode("src/handler.ts", {
286
+ buildProvider: new CustomEsbuild(),
287
+ });
288
+ new InlineTypeScriptCode("let x: number = 1", {
289
+ transformProvider: new CustomEsbuild(),
201
290
  });
202
291
  ```
203
292
 
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.
293
+ Instead of _esbuild_, the custom methods will be invoked with all computed options.
294
+ The custom implementation can amend, change or discard any of these.
295
+ However with `IBuildProvider` the integration with CDK relies heavily on the `outdir`/`outfile` values and it's usually required to use them unchanged.
296
+ `ITransformProvider` must return the transformed code as a string.
205
297
 
206
- Failures have to cause a `TransformFailure` exception in order to be fully handled.
298
+ Failures and warnings should be printed to stderr and thrown as the respective _esbuild_ error.
207
299
 
208
- ### Migrating to v3
300
+ #### Overriding the default implementation providers
209
301
 
210
- The release of cdk-esbuild v3 brings compatibility with AWS CDK v2. Furthermore all deprecated properties and classes have been removed. In particular `InlineCode` classes now take `TransformerProps` as second parameter instead of transform options.
302
+ It is also possible to change the default Esbuild API implementation for all usages of this package.
303
+ You can change the default for both APIs or set a different implementation for each of them.
211
304
 
212
- #### Upgrading
305
+ ```ts
306
+ const myCustomEsbuildProvider = new MyCustomEsbuildProvider();
213
307
 
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:
308
+ EsbuildProvider.overrideDefaultProvider(myCustomEsbuildProvider);
309
+ EsbuildProvider.overrideDefaultBuildProvider(myCustomEsbuildProvider);
310
+ EsbuildProvider.overrideDefaultTransformationProvider(myCustomEsbuildProvider);
219
311
 
220
- ```ts
221
- const oldOptions: TransformOptions = {...}
312
+ // This will use the custom provider without the need to define it as a prop
313
+ new TypeScriptCode("src/handler.ts");
314
+ ```
222
315
 
223
- new InlineTypeScriptCode('// inline code', {
224
- transformOptions: oldOptions
225
- });
226
- ```
316
+ ### Roadmap & Contributions
227
317
 
228
- ## Versioning
318
+ [The project's roadmap is available on GitHub.](https://github.com/users/mrgrain/projects/1/views/7) Please submit feature requests as issues to the repository.
229
319
 
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`.
320
+ All contributions are welcome, no matter if they are for already planned or completely new features.
231
321
 
232
- ### Npm Tags
322
+ ## FAQ
233
323
 
234
- Some users prefer to use tags over version ranges. The following stable tags are available for use:
324
+ ### How do I upgrade from `@mrgrain/cdk-esbuild` v3?
235
325
 
236
- - `cdk-v1`, `cdk-v2` are provided for the latest release compatible with each version of the AWS CDK.
326
+ 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.
237
327
 
238
- - `latest` is the most recent stable release.
328
+ ### [TS/JS] Why am I getting the error `Cannot find module 'esbuild'`?
239
329
 
240
- These tags also exist, but usage is strongly not recommended:
330
+ 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:
241
331
 
242
- - `unstable`, `next` are used for pre-release of the current and next major version respectively.
332
+ ```console
333
+ npm install esbuild
334
+ ```
243
335
 
244
- - ~~`cdk-1.x.x`~~ tags have been deprecated in favour of `cdk-v1`. Use that one instead.
336
+ ### [TS/JS] How can I use a different version of _esbuild_?
245
337
 
246
- ## Roadmap & Contributions
338
+ 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:
247
339
 
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.
340
+ ```json
341
+ {
342
+ "overrides": {
343
+ "esbuild": "0.14.7"
344
+ }
345
+ }
346
+ ```
249
347
 
250
- All contributions are welcome, no matter if they are for already planned or completely new features.
348
+ Build and Transform interfaces are relatively stable across _esbuild_ versions.
349
+ However if any incompatibilities occur, `buildOptions` / `transformOptions` can be cast to `any`:
350
+
351
+ ```ts
352
+ const bundledCode = new TypeScriptCode("src/handler.ts", {
353
+ buildOptions: {
354
+ unsupportedOption: "value"
355
+ } as any,
356
+ });
357
+ ```
251
358
 
252
- ## Library authors
359
+ ### [Python/Dotnet] How can I use a different version of _esbuild_?
253
360
 
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):
361
+ Install the desired version of _esbuild_ locally or globally [as described in the documentation above](#python-and-dotnet).
255
362
 
256
- ```ts
257
- // file: project/src/index.ts
363
+ Build and Transform interfaces are relatively stable across _esbuild_ versions.
364
+ However if any incompatibilities occur, use the appropriate language features to cast any incompatible `buildOptions` / `transformOptions` to the correct types.
365
+
366
+ ### Can I use this package in my published AWS CDK Construct?
367
+
368
+ It is possible to use `cdk-esbuild` in a published AWS CDK Construct library, but not recommended. Always prefer to ship a compiled `.js` file or even bundle a zip archive in your package. For AWS Lambda Functions, [projen provides an excellent solution](https://projen.io/awscdk.html#aws-lambda-functions).
369
+
370
+ 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:
371
+
372
+ ```js
373
+ // file: node_modules/construct-library/src/index.ts
258
374
  const props = {
259
375
  buildOptions: {
260
- absWorkingDir: path.resolve(__dirname, ".."), // now: /user/project
376
+ absWorkingDir: path.resolve(__dirname, ".."),
377
+ // now: /user/local-app/node_modules/construct-library
261
378
  },
262
379
  };
263
380
  ```
264
381
 
265
- This will dynamically resolve to the correct path, wherever the package is installed.
382
+ This will dynamically resolve to the correct path, wherever the package is installed. Please open an issue if you encounter any difficulties.
383
+
384
+ ### Can I use this package with AWS CDK v1?
385
+
386
+ 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
387
 
267
- Please open an issue if you encounter any difficulties.
388
+ 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
  */
@@ -27,9 +38,13 @@ export interface AssetProps extends AssetBaseProps {
27
38
  declare type JavaScriptAssetProps = AssetProps;
28
39
  declare type TypeScriptAssetProps = AssetProps;
29
40
  /**
30
- * @stability stable
41
+ * Represents a generic esbuild asset.
42
+ *
43
+ * You should always use `TypeScriptAsset` or `JavaScriptAsset`.
44
+ *
45
+ * @stability experimental
31
46
  */
32
- export declare abstract class Asset<Props extends AssetProps> extends S3Asset {
47
+ export declare class EsbuildAsset<Props extends AssetProps> extends S3Asset {
33
48
  /**
34
49
  * @stability stable
35
50
  */
@@ -42,7 +57,7 @@ export declare abstract class Asset<Props extends AssetProps> extends S3Asset {
42
57
  *
43
58
  * @stability stable
44
59
  */
45
- export declare class JavaScriptAsset extends Asset<JavaScriptAssetProps> {
60
+ export declare class JavaScriptAsset extends EsbuildAsset<JavaScriptAssetProps> {
46
61
  }
47
62
  /**
48
63
  * Bundles the entry points and creates a CDK asset which is uploaded to the bootstrapped CDK S3 bucket during deployment.
@@ -51,6 +66,6 @@ export declare class JavaScriptAsset extends Asset<JavaScriptAssetProps> {
51
66
  *
52
67
  * @stability stable
53
68
  */
54
- export declare class TypeScriptAsset extends Asset<TypeScriptAssetProps> {
69
+ export declare class TypeScriptAsset extends EsbuildAsset<TypeScriptAssetProps> {
55
70
  }
56
71
  export {};