Skip to content

Commit

Permalink
fix: auto import tsconfig-paths/register.js (#282)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
	- Added a new `/foo` route in the application.
	- Introduced a `Foo` class with a `bar` method.
	- Enhanced TypeScript path resolution configuration.

- **Tests**
- Updated test cases to cover new functionality, including the `/foo`
endpoint.
- Improved test suite with additional assertions and updated expected
outputs.
	- Updated mock dependency to version 6.

- **Chores**
- Updated TypeScript compiler settings to target ES2022 and refined
module resolution strategies.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Jan 3, 2025
1 parent 120c285 commit 515614a
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 23 deletions.
28 changes: 13 additions & 15 deletions src/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,15 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
}
}
flags.typescript = typescript;

let rootDir = path.dirname(getSourceDirname());
if (path.basename(rootDir) === 'dist') {
rootDir = path.dirname(rootDir);
}
// try app baseDir first on custom tscompiler
// then try to find tscompiler in @eggjs/bin/node_modules
const findPaths: string[] = [ flags.base, rootDir ];
this.isESM = pkg.type === 'module';
if (typescript) {
const findPaths: string[] = [ getSourceDirname() ];
if (flags.tscompiler) {
// try app baseDir first on custom tscompiler
// then try to find tscompiler in @eggjs/bin/node_modules
findPaths.unshift(flags.base);
}
flags.tscompiler = flags.tscompiler ?? 'ts-node/register';
const tsNodeRegister = importResolve(flags.tscompiler, {
paths: findPaths,
Expand All @@ -229,14 +229,15 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
this.env.TS_NODE_FILES = process.env.TS_NODE_FILES ?? 'true';
// keep same logic with egg-core, test cmd load files need it
// see https://github.com/eggjs/egg-core/blob/master/lib/loader/egg_loader.js#L49
// addNodeOptionsToEnv(`--require ${importResolve('tsconfig-paths/register', {
// paths: [ getSourceDirname() ],
// })}`, ctx.env);
const tsConfigPathsRegister = importResolve('tsconfig-paths/register', {
paths: findPaths,
});
this.addNodeOptions(this.formatImportModule(tsConfigPathsRegister));
}
if (this.isESM) {
// use ts-node/esm loader on esm
let esmLoader = importResolve('ts-node/esm', {
paths: [ getSourceDirname() ],
paths: findPaths,
});
// ES Module loading with absolute path fails on windows
// https://github.com/nodejs/node/issues/31710#issuecomment-583916239
Expand All @@ -257,10 +258,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
}
if (flags.declarations) {
const etsBin = importResolve('egg-ts-helper/dist/bin', {
paths: [
flags.base,
getSourceDirname(),
],
paths: findPaths,
});
debug('run ets first: %o', etsBin);
await runScript(`node ${etsBin}`);
Expand Down
2 changes: 1 addition & 1 deletion test/commands/cov.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('test/commands/cov.test.ts', () => {
await coffee.fork(eggBin, [ 'cov' ], { cwd })
// .debug()
.expect('stdout', /should work/)
.expect('stdout', /1 passing/)
.expect('stdout', /3 passing/)
.expect('stdout', /Statements\s+: 100% \( \d+\/\d+ \)/)
.expect('code', 0)
.end();
Expand Down
4 changes: 2 additions & 2 deletions test/commands/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ describe('test/commands/test.test.ts', () => {
it('should success on ts', async () => {
const cwd = getFixtures('example-ts');
await coffee.fork(eggBin, [ 'test' ], { cwd })
// .debug()
.debug()
.expect('stdout', /should work/)
.expect('stdout', /1 passing/)
.expect('stdout', /3 passing \(/)
.expect('code', 0)
.end();
});
Expand Down
7 changes: 6 additions & 1 deletion test/fixtures/example-ts/app/controller/home.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Controller } from 'egg';

import { Foo } from '@/module/foo';
export default class HomeController extends Controller {
public async index() {
const obj: PlainObject = {};
obj.text = 'hi, egg';
this.ctx.body = obj.text;
}

async foo() {
const instance = new Foo();
this.ctx.body = instance.bar();
}
}
5 changes: 5 additions & 0 deletions test/fixtures/example-ts/app/module/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class Foo {
public bar() {
return 'bar';
}
}
6 changes: 6 additions & 0 deletions test/fixtures/example-ts/app/module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "foo",
"eggModule": {
"name": "foo"
}
}
1 change: 1 addition & 0 deletions test/fixtures/example-ts/app/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { Application } from 'egg';

export default (app: Application) => {
app.router.get('/', app.controller.home.index);
app.router.get('/foo', app.controller.home.foo);
};
2 changes: 1 addition & 1 deletion test/fixtures/example-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"typescript": true
},
"devDependencies": {
"@eggjs/mock": "beta"
"@eggjs/mock": "6"
}
}
18 changes: 16 additions & 2 deletions test/fixtures/example-ts/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
// @ts-ignore
import { strict as assert } from 'node:assert';
import { app } from '@eggjs/mock/bootstrap';
import { Foo } from '@/module/foo'

describe('test/index.test.ts', () => {
describe('example-ts/test/index.test.ts', () => {
it('should work', async () => {
await app.ready();
await app.httpRequest()
.get('/')
.expect('hi, egg')
.expect(200);
});

it('should paths work', async () => {
await app.ready();
await app.httpRequest()
.get('/foo')
.expect('bar')
.expect(200);
});

it('should auto import tsconfig-paths/register', async () => {
const instance = new Foo();
assert.equal(instance.bar(), 'bar');
});
});
12 changes: 11 additions & 1 deletion test/fixtures/example-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{
"extends": "@eggjs/tsconfig"
"extends": "@eggjs/tsconfig",
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"declaration": false,
"paths": {
"@/module/*": ["./app/module/*"]
},
"baseUrl": "."
}
}

0 comments on commit 515614a

Please sign in to comment.