@aws-cdk/aws-lambda-nodejs: esbuildArgs should allow specifying alias multiple times
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the bug
Very closely related to #25385.
I want to be able to specify _multiple_ aliases. There isn't currently a way to do that, since objects cannot have repeated keys. Given the API decision to specify possibly-repeated args in an object, I would expect to be able to indicate a repeated arg by supplying an array, like so
```ts
new NodejsFunction(this, 'test-function', {
// ...
bundling: {
// ...
esbuildArgs: {
'--alias': ['@layer1=/opt/nodejs/layer1', '@layer2=/opt/nodejs/layer2']
},
},
});
```
### Regression Issue
- [ ] Select this option if this issue appears to be a regression.
### Last Known Working CDK Version
_No response_
### Expected Behavior
This should result in esbuild being run with `--alias:@layer1=/opt/nodejs/layer1 --alias:@layer2=/opt/nodejs/layer2`.
### Current Behavior
(TS) Type error: Type 'string[]' is not assignable to type 'string | boolean'.
If we ignore the types and pass in an array anyway, I think at runtime this becomes `--alias:"@layer1=/opt/nodejs/layer1,@layer2=/opt/nodejs/layer2"`, which also doesn't work.
### Reproduction Steps
```ts
new NodejsFunction(this, 'test-function', {
// ...
bundling: {
// ...
esbuildArgs: {
'--alias': ['@layer1=/opt/nodejs/layer1', '@layer2=/opt/nodejs/layer2']
},
},
});
```
### Possible Solution
When an array is provided as the value, it should be mapped to specify the option multiple times, i.e. `--alias:@layer1=/opt/nodejs/layer1 --alias:@layer2=/opt/nodejs/layer2`
### Additional Information/Context
I've used essentially the same workaround as suggested in https://github.com/aws/aws-cdk/issues/25385#issuecomment-1529994229, namely
```ts
new NodejsFunction(this, 'test-function', {
// ...
bundling: {
// ...
esbuildArgs: {
'--alias:@layer1': '/opt/nodejs/layer1',
'--alias:@layer2': '/opt/nodejs/layer2'
},
},
});
```
While this works, it's a little anxiety-inducing, since it's a hack that depends on the implementation detail that `{ key: 'value' }` will be used with an equal sign, to form `key=value`.
### CDK CLI Version
2.155.0 (build 34dcc5a)
### Framework Version
_No response_
### Node.js Version
node 18
### OS
macos
### Language
TypeScript
### Language Version
_No response_
### Other information
_No response_
Contributor guide
Research direction
Start at the NodejsFunction bundling implementation that consumes esbuildArgs, then trace how the object is converted into esbuild command-line arguments. Use the TypeScript reproduction with two --alias values as the check; done means arrays are accepted by the type and produce separate --alias:value arguments while existing string and boolean values continue to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- backend, cloud
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100