You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|**ES Modules support**|Added ES Modules support via dual CommonJS and ES Module bundling, this enables top-level `await` and tree-shaking.| No|
15
15
|**Middy.js middleware imports**| Updated import path for Middy.js middlewares to leverage subpath exports - i.e. `@aws-lambda-powertools/tracer/middleware`. | Yes |
16
16
|**Types imports**| Updated import path for TypeScript types to leverage subpath exports - i.e. `@aws-lambda-powertools/logger/types`. | Yes |
17
17
|**Logger - log sampling**| Changed implementation of [log sampling](./core/logger.md#sampling-logs) to dynamically switch log level to `DEBUG` on a percentage of requests. | - |
@@ -28,7 +28,78 @@ Before you start, we suggest making a copy of your current working project or cr
28
28
29
29
## ES Modules support
30
30
31
-
TBD
31
+
Starting with v2, Powertools for AWS Lambda (TypeScript) supports ES Modules. This means that you can now import the package using the `import` syntax instead of the `require` syntax. This is especially useful if you want to leverage features like top-level `await` in your Lambda function to run asynchronous code during the initialization phase.
With this change, you can also apply tree-shaking to your function bundle to reduce its size. As part of this release we have made changes to the package and its exports to better support this feature, and we remain committed to improving this in the future based on your feedback.
45
+
46
+
While we recommend using ES Modules, we understand that this change might not be possible for everyone. If you're unable to use ES Modules, you can continue to use the `require` syntax to import the package. Powertools for AWS Lambda (TypeScript) will continue to support this syntax by shipping CommonJS modules alongside ES Modules.
47
+
48
+
In some cases, even when opting for ES Modules, you might still need to use the `require` syntax to import a package. For example, if you're using a package that doesn't support ES Modules, or if one of your transitive dependencies is using the `require` syntax like it's the case for `@aws-lambda-powertools/tracer` which relies on the AWS X-Ray SDK for Node.js. In these cases, you can still use ES Modules for the rest of your codebase and set a special build flag to tell your bundler to inject a banner at the top of the file to use the `require` syntax for the specific package.
49
+
50
+
=== "With AWS CDK"
51
+
52
+
```typescript hl_lines="15 20-21"
53
+
import { Stack, type StackProps } from 'aws-cdk-lib';
54
+
import { Construct } from 'constructs';
55
+
import { NodejsFunction, OutputFormat } from 'aws-cdk-lib/aws-lambda-nodejs';
56
+
import { Runtime } from 'aws-cdk-lib/aws-lambda';
57
+
58
+
export class MyStack extends Stack {
59
+
public constructor(scope: Construct, id: string, props?: StackProps) {
60
+
super(scope, id, props);
61
+
62
+
const handler = new NodejsFunction(this, 'helloWorldFunction', {
63
+
runtime: Runtime.NODEJS_20_X,
64
+
handler: 'handler',
65
+
entry: 'src/index.ts',
66
+
bundling: {
67
+
format: OutputFormat.ESM,
68
+
minify: true,
69
+
esbuildArgs: {
70
+
"--tree-shaking": "true",
71
+
},
72
+
banner:
73
+
"import { createRequire } from 'module';const require = createRequire(import.meta.url);",
74
+
},
75
+
});
76
+
}
77
+
}
78
+
```
79
+
80
+
=== "With AWS SAM"
81
+
82
+
```yaml hl_lines="14 17-18"
83
+
Transform: AWS::Serverless-2016-10-31
84
+
Resources:
85
+
HelloWorldFunction:
86
+
Type: AWS::Serverless::Function
87
+
Properties:
88
+
Runtime: nodejs20.x
89
+
Handler: src/index.handler
90
+
Metadata:
91
+
BuildMethod: esbuild
92
+
BuildProperties:
93
+
Minify: true
94
+
Target: 'ES2020'
95
+
Sourcemap: true
96
+
Format: esm
97
+
EntryPoints:
98
+
- src/index.ts
99
+
Banner:
100
+
js: "import { createRequire } from 'module';const require = createRequire(import.meta.url);"
0 commit comments