Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support switch ts compiler #158

Merged
merged 3 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
whxaxes marked this conversation as resolved.
Show resolved Hide resolved

// 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}`);
};
20 changes: 15 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,16 @@ 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 })
whxaxes marked this conversation as resolved.
Show resolved Hide resolved
// .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