From a74bae2f604c13b50dadb8468a796867315120c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=96=E7=8C=A9?= Date: Fri, 23 Apr 2021 18:20:49 +0800 Subject: [PATCH] feat: support switch ts compiler (#158) * feat: support switch compiler * fix: ci * fix: ci --- .travis.yml | 4 +-- appveyor.yml | 3 +- lib/command.js | 19 +++++++++---- package.json | 1 + test/fixtures/example-ts-pkg/agent.js | 1 + test/fixtures/example-ts-pkg/package.json | 3 +- test/fixtures/example-ts/agent.js | 7 +++++ test/lib/cmd/test.test.js | 23 ++++++++------- test/ts.test.js | 34 +++++++++++++++++++---- 9 files changed, 71 insertions(+), 24 deletions(-) create mode 100644 test/fixtures/example-ts/agent.js diff --git a/.travis.yml b/.travis.yml index 559b54f1..1dfab273 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ sudo: false language: node_js node_js: - - "6" - - "8" - "10" + - "12" + - "14" env: - EGG_VERSION=1 - EGG_VERSION=2 diff --git a/appveyor.yml b/appveyor.yml index 981e82b0..7c808d71 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,8 @@ environment: matrix: - - nodejs_version: '8' - nodejs_version: '10' + - nodejs_version: '12' + - nodejs_version: '14' install: - ps: Install-Product node $env:nodejs_version diff --git a/lib/command.js b/lib/command.js index aa0098e3..9f78ae71 100644 --- a/lib/command.js +++ b/lib/command.js @@ -16,7 +16,7 @@ class Command extends BaseCommand { // https://github.com/node-modules/common-bin/blob/master/lib/command.js#L158 this.options = { typescript: { - description: 'whether enable typescript support, will load `ts-node/register` etc', + description: 'whether enable typescript support, will load tscompiler on startup', type: 'boolean', alias: 'ts', default: undefined, @@ -28,6 +28,13 @@ class Command extends BaseCommand { alias: 'dts', default: undefined, }, + + tscompiler: { + description: 'ts compiler, like ts-node、ts-eager、esbuild-register etc.', + type: 'string', + alias: 'tsc', + default: undefined, + }, }; } @@ -68,6 +75,11 @@ class Command extends BaseCommand { argv.declarations = eggInfo.declarations === true; } + // read `egg.tscompiler` from package.json if not pass argv + if (argv.tscompiler === undefined && eggInfo) { + argv.tscompiler = eggInfo.tscompiler || 'ts-node/register'; + } + // read `egg.require` from package.json if (eggInfo && eggInfo.require && Array.isArray(eggInfo.require)) { execArgvObj.require = execArgvObj.require.concat(eggInfo.require); @@ -75,14 +87,11 @@ class Command extends BaseCommand { // load ts-node if (argv.typescript) { - execArgvObj.require.push(require.resolve('ts-node/register')); + execArgvObj.require.push(require.resolve(argv.tscompiler)); // 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'; } diff --git a/package.json b/package.json index 0785138b..2b62f07e 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "egg": "^2.20.2", "egg-mock": "^3.22.1", "enzyme": "^2.0.0", + "esbuild-register": "^2.5.0", "eslint": "^4.12.1", "eslint-config-egg": "^7.3.1", "jsdom": "^8.0.1", diff --git a/test/fixtures/example-ts-pkg/agent.js b/test/fixtures/example-ts-pkg/agent.js index 2fbe626b..06d959f2 100644 --- a/test/fixtures/example-ts-pkg/agent.js +++ b/test/fixtures/example-ts-pkg/agent.js @@ -3,4 +3,5 @@ module.exports = agent => { console.log(`agent.options.typescript = ${agent.options.typescript}`); + console.log(`agent.options.tscompiler = ${agent.options.tscompiler}`); }; diff --git a/test/fixtures/example-ts-pkg/package.json b/test/fixtures/example-ts-pkg/package.json index 9db0d2e2..8a0b4bfc 100644 --- a/test/fixtures/example-ts-pkg/package.json +++ b/test/fixtures/example-ts-pkg/package.json @@ -1,6 +1,7 @@ { "name": "example-ts-pkg", "egg": { - "typescript": true + "typescript": true, + "tscompiler": "esbuild-register" } } \ No newline at end of file diff --git a/test/fixtures/example-ts/agent.js b/test/fixtures/example-ts/agent.js new file mode 100644 index 00000000..06d959f2 --- /dev/null +++ b/test/fixtures/example-ts/agent.js @@ -0,0 +1,7 @@ +'use strict'; + + +module.exports = agent => { + console.log(`agent.options.typescript = ${agent.options.typescript}`); + console.log(`agent.options.tscompiler = ${agent.options.tscompiler}`); +}; diff --git a/test/lib/cmd/test.test.js b/test/lib/cmd/test.test.js index 7a47d781..cd8b5125 100644 --- a/test/lib/cmd/test.test.js +++ b/test/lib/cmd/test.test.js @@ -205,12 +205,13 @@ describe('test/lib/cmd/test.test.js', () => { it('should clean co error stack', done => { mm(process.env, 'TESTS', 'test/promise.test.js'); coffee.fork(eggBin, [ 'test' ], { cwd }) - // .debug() - .end((err, { stdout, code }) => { + // .debug() + .end((err, result) => { + if (err) return done(err); + const { stdout, code } = result; assert(stdout.match(/Error: this is an error/)); - assert(stdout.match(/at Promise .*promise.test.js:\d+:\d+/)); - assert(stdout.match(/at Context\. .*promise.test.js:\d+:\d+/)); - assert(stdout.match(/\bat\s+/g).length >= 3); + assert(stdout.match(/test\/promise.test.js:\d+:\d+/)); + assert(stdout.match(/\bat\s+/g).length); assert(code === 1); done(err); }); @@ -219,12 +220,14 @@ describe('test/lib/cmd/test.test.js', () => { it('should clean callback error stack', done => { mm(process.env, 'TESTS', 'test/sleep.test.js'); coffee.fork(eggBin, [ 'test' ], { cwd }) - // .debug() - .end((err, { stdout, code }) => { + // .debug() + .end((err, result) => { + if (err) return done(err); + const { stdout, code } = result; assert(stdout.match(/Error: this is an error/)); - assert(stdout.match(/at sleep .*sleep.test.js:\d+:\d+/)); - assert(stdout.match(/at Timeout.setTimeout .*node_modules.*my-sleep.*index.js:\d+:\d+/)); - assert(stdout.match(/\bat\s+/g).length === 2); + assert(stdout.match(/test\/sleep.test.js:\d+:\d+/)); + assert(stdout.match(/node_modules\/my-sleep\/index.js:\d+:\d+/)); + assert(stdout.match(/\bat\s+/g).length); assert(code === 1); done(err); }); diff --git a/test/ts.test.js b/test/ts.test.js index a57ef4f2..4a9f3067 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -106,10 +106,10 @@ describe('test/ts.test.js', () => { it('should correct error stack line number in testing app', () => { return coffee.fork(eggBin, [ 'test' ], { cwd }) - // .debug() + .debug() .expect('stdout', /error/) - .expect('stdout', /at Context\.it .+index\.test\.ts:8:11\)/) - .expect('stdout', /at Context\.it .+index\.test\.ts:14:5\)/) + .expect('stdout', /test\/index\.test\.ts:8:11\)/) + .expect('stdout', /test\/index\.test\.ts:14:5\)/) .end(); }); @@ -117,8 +117,8 @@ describe('test/ts.test.js', () => { return coffee.fork(eggBin, [ 'test' ], { cwd }) // .debug() .expect('stdout', /error/) - .expect('stdout', /at Context\.it .+index\.test\.ts:8:11\)/) - .expect('stdout', /at Context\.it .+index\.test\.ts:14:5\)/) + .expect('stdout', /test\/index\.test\.ts:8:11\)/) + .expect('stdout', /test\/index\.test\.ts:14:5\)/) .end(); }); }); @@ -152,6 +152,30 @@ describe('test/ts.test.js', () => { .end(); }); + it('should start app with other tscompiler without error', () => { + return coffee.fork(eggBin, [ 'dev', '--ts', '--tscompiler=esbuild-register' ], { + cwd: path.join(__dirname, './fixtures/example-ts'), + }) + // .debug() + .expect('stdout', /agent.options.typescript = true/) + .expect('stdout', /agent.options.tscompiler = esbuild-register/) + .expect('stdout', /started/) + .expect('code', 0) + .end(); + }); + + it('should start app with other tscompiler in package.json without error', () => { + return coffee.fork(eggBin, [ 'dev', '--ts' ], { + cwd: path.join(__dirname, './fixtures/example-ts-pkg'), + }) + // .debug() + .expect('stdout', /agent.options.typescript = true/) + .expect('stdout', /agent.options.tscompiler = esbuild-register/) + .expect('stdout', /started/) + .expect('code', 0) + .end(); + }); + it('should test app', () => { return coffee.fork(eggBin, [ 'test' ], { cwd }) // .debug()