Skip to content

Commit

Permalink
feat: support switch ts compiler (#158)
Browse files Browse the repository at this point in the history
* feat: support switch compiler

* fix: ci

* fix: ci
  • Loading branch information
whxaxes authored Apr 23, 2021
1 parent 2b54a42 commit a74bae2
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
sudo: false
language: node_js
node_js:
- "6"
- "8"
- "10"
- "12"
- "14"
env:
- EGG_VERSION=1
- EGG_VERSION=2
Expand Down
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 14 additions & 5 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
},
};
}

Expand Down Expand Up @@ -68,21 +75,23 @@ 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);
}

// 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';
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/example-ts-pkg/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

module.exports = agent => {
console.log(`agent.options.typescript = ${agent.options.typescript}`);
console.log(`agent.options.tscompiler = ${agent.options.tscompiler}`);
};
3 changes: 2 additions & 1 deletion test/fixtures/example-ts-pkg/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "example-ts-pkg",
"egg": {
"typescript": true
"typescript": true,
"tscompiler": "esbuild-register"
}
}
7 changes: 7 additions & 0 deletions test/fixtures/example-ts/agent.js
Original file line number Diff line number Diff line change
@@ -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}`);
};
23 changes: 13 additions & 10 deletions test/lib/cmd/test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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\.<anonymous> .*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);
});
Expand All @@ -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);
});
Expand Down
34 changes: 29 additions & 5 deletions test/ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@ 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();
});

it('should correct error stack line number in covering app', () => {
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();
});
});
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a74bae2

Please sign in to comment.