aws / aws/aws-cdk

aws_lambda_nodejs: specify outDir when using preCompilation

Open
#19,841 8 comments 11 reactions 0 assignees View on GitHub
@aws-cdk/aws-lambda-nodejs effort/medium feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the feature

When bundling an AWS Lambda using the `preCompilation` option, JS files are emitted in the same location as the TS files.
When working with a lot of TS files, it's very annoying to get dozen of JS files generated.
We should be able to define an output directory.
Pre-compilation forces the `outDir` option to be `./` which totally overrides the value defined in the `tsconfig.json`.

### Use Case

When doing a `cdk synth` or `cdk build` when the `preCompilation` option is used, it's frustrating to get JS files generated in the same location as the TS files.
I would like to specify an output directory for the JS files but the `outDir` option from the `tsconfig.json` is ignored.

### Proposed Solution

The function `getTsconfigCompilerOptions` force the `outDir` to `./`.
```
const options: Record = {
...compilerOptions,
// Overrides
incremental: false,
// Intentionally Setting rootDir and outDir, so that the compiled js file always end up next to .ts file.
rootDir: './',
outDir: './',
};
```
If the `outDir` option is defined in the `tsconfig.json`, it should be taken into account.
esbuild will not know where is the generated JS file but we may be able to specify the path using the `esbuildArgs` option.

### Other Information

_No response_

### Acknowledgements

- [ ] I may be able to implement this feature request
- [x] This feature might incur a breaking change

### CDK version used

2.20.0

### Environment details (OS name and version, etc.)

macOS 12.2.1

Contributor guide

Open the contributing guide

Research direction

Start by reading getTsconfigCompilerOptions and the preCompilation path described in the issue. Check how tsconfig.json compilerOptions and esbuildArgs determine the generated JavaScript location. Done means a configured outDir is respected without leaving compiled files beside the TypeScript sources, with the resulting esbuild path handled.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
build-system, cloud
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.