Skip to content

Commit

Permalink
feat: upgrade espower-typescript to 9.0 (#106)
Browse files Browse the repository at this point in the history
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md

感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [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)
<!-- Provide 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 的表现一致。
  • Loading branch information
whxaxes authored and popomore committed Aug 23, 2018
1 parent e31f476 commit 35e89db
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
14 changes: 14 additions & 0 deletions lib/cmd/test.js
Expand Up @@ -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, ...}
Expand Down Expand Up @@ -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'));
}

Expand Down
12 changes: 10 additions & 2 deletions lib/command.js
Expand Up @@ -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;
Expand Down
6 changes: 0 additions & 6 deletions lib/ts-helper.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down

0 comments on commit 35e89db

Please sign in to comment.