From 9843e71219bfe8b5ca675ac322e3dc5b3ab6381c Mon Sep 17 00:00:00 2001 From: Jerry Kindall <52084730+Jerry-AWS@users.noreply.github.com> Date: Tue, 17 Nov 2020 07:51:44 -0800 Subject: [PATCH] fix(init): TypeScript code is not being recompiled automatically (#11470) The use of `npx ts-node` to run the app in the TypeScript init templates is intended to make sure the source code gets transpiled before every synth without the user needing to `npm run build` explicitly. I have had reports on the Hello World dev guide topic that an `npm run build` is still needed, even though it shouldn't be. I was able to reproduce this issue today (was not able to do so previously). The problem is that, by default, `ts-node` prefers resolving imports to JavaScript files. If a JavaScript file exists, as it would be after the first build, it is simply imported as-is. The corresponding TypeScript module is not transpiled. Therefore changes you make to the TS code are never picked up and stale JS code is used. The flag `--prefer-ts-exts` solves this issue. It causes module imports to prefer importing TypeScript files, which means that the modules will get recompiled if necessary. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../aws-cdk/lib/init-templates/app/typescript/cdk.template.json | 2 +- .../lib/init-templates/app/typescript/package.template.json | 2 +- .../lib/init-templates/sample-app/typescript/cdk.template.json | 2 +- .../init-templates/sample-app/typescript/package.template.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/aws-cdk/lib/init-templates/app/typescript/cdk.template.json b/packages/aws-cdk/lib/init-templates/app/typescript/cdk.template.json index 8442bd8052356..4b132c728abd7 100644 --- a/packages/aws-cdk/lib/init-templates/app/typescript/cdk.template.json +++ b/packages/aws-cdk/lib/init-templates/app/typescript/cdk.template.json @@ -1,3 +1,3 @@ { - "app": "npx ts-node bin/%name%.ts" + "app": "npx ts-node --prefer-ts-exts bin/%name%.ts" } diff --git a/packages/aws-cdk/lib/init-templates/app/typescript/package.template.json b/packages/aws-cdk/lib/init-templates/app/typescript/package.template.json index 8aeb1df90f6b7..015a6d688d755 100644 --- a/packages/aws-cdk/lib/init-templates/app/typescript/package.template.json +++ b/packages/aws-cdk/lib/init-templates/app/typescript/package.template.json @@ -17,7 +17,7 @@ "jest": "^26.4.2", "ts-jest": "^26.2.0", "aws-cdk": "%cdk-version%", - "ts-node": "^8.1.0", + "ts-node": "^8.3.0", "typescript": "~3.9.7" }, "dependencies": { diff --git a/packages/aws-cdk/lib/init-templates/sample-app/typescript/cdk.template.json b/packages/aws-cdk/lib/init-templates/sample-app/typescript/cdk.template.json index 8442bd8052356..4b132c728abd7 100644 --- a/packages/aws-cdk/lib/init-templates/sample-app/typescript/cdk.template.json +++ b/packages/aws-cdk/lib/init-templates/sample-app/typescript/cdk.template.json @@ -1,3 +1,3 @@ { - "app": "npx ts-node bin/%name%.ts" + "app": "npx ts-node --prefer-ts-exts bin/%name%.ts" } diff --git a/packages/aws-cdk/lib/init-templates/sample-app/typescript/package.template.json b/packages/aws-cdk/lib/init-templates/sample-app/typescript/package.template.json index 77d515d129e3b..3f9e0309f07d1 100644 --- a/packages/aws-cdk/lib/init-templates/sample-app/typescript/package.template.json +++ b/packages/aws-cdk/lib/init-templates/sample-app/typescript/package.template.json @@ -17,7 +17,7 @@ "@types/node": "10.17.27", "jest": "^26.4.2", "ts-jest": "^26.2.0", - "ts-node": "^8.1.0", + "ts-node": "^8.3.0", "typescript": "~3.9.7" }, "dependencies": {