cds-plugin-typescript
v0.1.6
Published
A CDS build plugin for SAP CAP that compiles TypeScript sources to JavaScript as part of cds build.
Maintainers
Readme
cds-plugin-typescript
A CDS build plugin for SAP CAP that compiles TypeScript sources to JavaScript as part of cds build.
It is similar to cds-typer but focused exclusively on TypeScript compilation. It also supports resolving TypeScript path aliases via tsc-alias.
Prerequisites
- Node.js
>=22 - npm
>=10 @sap/cds>=8typescript>=5tsc-alias>=1(optional — automatically detected and used if listed in your project'spackage.json)
Installation
npm install --save-dev cds-plugin-typescriptUsage
Once installed, the plugin is automatically discovered and activated — no manual registration needed. It's a cds-plugin and will be loaded by CAP automatically.
Simply run cds build and the plugin will:
- Detect your tsconfig file (see tsconfig resolution below)
- Compile TypeScript sources with
tsc - Resolve path aliases with
tsc-alias(automatic — runs iftsc-aliasis found in your project's dependencies or devDependencies) - Remove
.tssource files from the build output
Configuration
Zero configuration is required by default. Simply:
- Install the plugin (
npm install --save-dev cds-plugin-typescript) - Maintain a tsconfig file — the plugin automatically looks for
tsconfig.cdsbuild.json,tsconfig.build.json, ortsconfig.json(first found wins) - Run
cds build
That's it — no package.json changes needed.
Custom tsConfig path
The cds.build.tasks configuration in package.json is only needed if you want to point the plugin at a tsconfig file with a non-standard name (i.e. something other than the three files listed above):
{
"cds": {
"build": {
"tasks": [
{ "for": "nodejs" },
{
"for": "typescript",
"options": {
"tsConfig": "tsconfig.custom.json"
}
}
]
}
}
}| Option | Type | Default | Description |
| ---------- | -------- | ------- | -------------------------------------------------------------------------------------------------- |
| tsConfig | string | — | Custom TypeScript config file path. When provided, this takes highest priority in the resolution order. |
tsconfig resolution
The plugin resolves the tsconfig file using the following priority order:
- Custom path via the
tsConfigoption (if provided in the build task configuration) tsconfig.cdsbuild.json— a CDS-build-specific configtsconfig.build.json— a general build-specific configtsconfig.json— the standard TypeScript config
The plugin uses the first file found in this order. This allows you to maintain separate TypeScript configurations for CDS builds, general builds, and development without conflicts.
The default/recommended tsconfig for the default CDS folder structure is:
{
"compilerOptions": {
"rootDir": "srv",
"outDir": "gen/srv/srv",
"declaration": true,
"sourceMap": true,
"removeComments": true
},
"include": [
"srv/**/*"
]
}The
outDirin your tsconfig determines where compiled output lands — make sure it aligns with your CDS build target (default:gen).
Path aliases
If your project uses TypeScript path aliases (e.g. @/* → src/*), simply add tsc-alias to your project's dependencies or devDependencies:
npm install --save-dev tsc-aliasThe plugin automatically detects whether tsc-alias is listed in your package.json (under dependencies or devDependencies) and runs it after tsc to rewrite the aliases in the compiled JavaScript. No additional configuration is needed.
If tsc-alias is not listed in your package.json, the path alias resolution step is skipped entirely.
How it works
The plugin extends cds.build.Plugin and is registered under the task type typescript.
Build steps:
- Resolve the tsconfig path (priority:
tsConfigoption →tsconfig.cdsbuild.json→tsconfig.build.json→tsconfig.json) - Run
npx tsc -p <tsconfig>from the task source directory — logs: "Compiling TypeScript using <tsconfig>" - If
tsc-aliasis detected inpackage.json: runnpx tsc-alias -p <tsconfig>— logs: "Resolving path aliases" - Delete all
.tsfiles (excluding.d.ts) from the build output — logs: "Removing TypeScript sources"
Clean step:
cds build --clean removes all .js and .map files emitted by this plugin from the destination directory.
