From 35e89dbdbfcb6d2c6cd07f73145ead7c4c5421ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=96=E7=8C=A9?= Date: Thu, 23 Aug 2018 15:16:48 +0800 Subject: [PATCH] feat: upgrade espower-typescript to 9.0 (#106) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ##### Checklist - [x] `npm test` passes - [ ] tests and/or benchmarks are included - [ ] documentation is changed or added - [x] commit message follows commit guidelines ##### Affected core subsystem(s) ##### Description of change ### 修复两个问题 1. 将 espower-typescript 升级到 9.0.0 版本,由于老版本的 espower-typescript 自己进行了 ts 解析而没进行类型检查 ,所以会导致此前跑单测,对代码是不会做类型检查的。而 espower-typescript 9.0 版本改成使用 ts-node 进行解析,所以可以开启类型检查。 2. 之前 4.8.0 版本将 espower-typescript 升级到了 9.0.0 版本,但是由于 espower-typescript 中会 register 一下 ts-node,而本身 egg-bin 也会 register 一下 ts-node,从而导致触发了两次 register,就会导致 ts 被编译成 js 后又会再一次去被编译一次。从而导致爆出类型错误,所以在 4.8.0 的 PR 中又将 espower-typescript 改回了 8.0 版本。该 PR 在升级 espower-typescript 的同时也修复了该问题,因为这个多编译一次的问题是会在当前单测中稳定复现的,所以不需要新增单测。 ### 其他修改 由于 ts-node 可能是由 espower-typescript 引入,也可能是由 egg-bin 引入,所以删除 ts-helper.js( 此前是用来给 ts-node 传相关参数 ),改成使用环境变量来控制 ts-node 的入参,才能保证 espower-typescript 及 egg-bin 的表现一致。 --- lib/cmd/test.js | 14 ++++++++++++++ lib/command.js | 12 ++++++++++-- lib/ts-helper.js | 6 ------ package.json | 2 +- 4 files changed, 25 insertions(+), 9 deletions(-) delete mode 100644 lib/ts-helper.js diff --git a/lib/cmd/test.js b/lib/cmd/test.js index 7045b289..88a25df0 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -50,6 +50,19 @@ class TestCommand extends Command { yield this.helper.forkNode(mochaFile, testArgs, opt); } + get context() { + const context = super.context; + const { argv, execArgvObj } = context; + + // remove ts-node, ts-node and espower-typescript can't coexist + // because espower-typescript@9 has already register ts-node + if (argv.typescript) { + execArgvObj.require.splice(execArgvObj.require.indexOf(require.resolve('ts-node/register')), 1); + } + + return context; + } + /** * format test args then change it to array style * @param {Object} context - { cwd, argv, ...} @@ -90,6 +103,7 @@ class TestCommand extends Command { // for power-assert if (testArgv.typescript) { + // remove ts-node in context getter on top. requireArr.push(require.resolve('espower-typescript/guess')); } diff --git a/lib/command.js b/lib/command.js index 9bcb1e4b..0b750ee0 100644 --- a/lib/command.js +++ b/lib/command.js @@ -50,8 +50,16 @@ class Command extends BaseCommand { // execArgv if (argv.typescript) { execArgvObj.require = execArgvObj.require || []; - execArgvObj.require.push(path.join(__dirname, './ts-helper.js')); - env.EGG_TYPESCRIPT = true; + execArgvObj.require.push(require.resolve('ts-node/register')); + + // tell egg loader to load ts file + env.EGG_TYPESCRIPT = 'true'; + + // use type check + env.TS_NODE_TYPE_CHECK = process.env.TS_NODE_TYPE_CHECK || 'true'; + + // load files from tsconfig on startup + env.TS_NODE_FILES = process.env.TS_NODE_FILES || 'true'; } return context; diff --git a/lib/ts-helper.js b/lib/ts-helper.js deleted file mode 100644 index cb435ad3..00000000 --- a/lib/ts-helper.js +++ /dev/null @@ -1,6 +0,0 @@ -'use strict'; - -require('ts-node').register({ - typeCheck: true, - files: true, -}); diff --git a/package.json b/package.json index 66e873c8..11434457 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "debug": "^3.1.0", "detect-port": "^1.2.3", "egg-utils": "^2.4.0", - "espower-typescript": "^8.0.0", + "espower-typescript": "^9.0.0", "globby": "^8.0.1", "inspector-proxy": "^1.2.1", "intelli-espower-loader": "^1.0.1",