diff --git a/src/baseCommand.ts b/src/baseCommand.ts index a69dce5e..db12bc1e 100644 --- a/src/baseCommand.ts +++ b/src/baseCommand.ts @@ -201,15 +201,15 @@ export abstract class BaseCommand 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, @@ -229,14 +229,15 @@ export abstract class BaseCommand 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 @@ -257,10 +258,7 @@ export abstract class BaseCommand 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}`); diff --git a/test/commands/cov.test.ts b/test/commands/cov.test.ts index 0e6c3155..4919fcca 100644 --- a/test/commands/cov.test.ts +++ b/test/commands/cov.test.ts @@ -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(); diff --git a/test/commands/test.test.ts b/test/commands/test.test.ts index 85e0e48f..fdc81e9d 100644 --- a/test/commands/test.test.ts +++ b/test/commands/test.test.ts @@ -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(); }); diff --git a/test/fixtures/example-ts/app/controller/home.ts b/test/fixtures/example-ts/app/controller/home.ts index c553a858..6121803f 100644 --- a/test/fixtures/example-ts/app/controller/home.ts +++ b/test/fixtures/example-ts/app/controller/home.ts @@ -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(); + } } diff --git a/test/fixtures/example-ts/app/module/foo.ts b/test/fixtures/example-ts/app/module/foo.ts new file mode 100644 index 00000000..4b20a166 --- /dev/null +++ b/test/fixtures/example-ts/app/module/foo.ts @@ -0,0 +1,5 @@ +export class Foo { + public bar() { + return 'bar'; + } +} diff --git a/test/fixtures/example-ts/app/module/package.json b/test/fixtures/example-ts/app/module/package.json new file mode 100644 index 00000000..738281a8 --- /dev/null +++ b/test/fixtures/example-ts/app/module/package.json @@ -0,0 +1,6 @@ +{ + "name": "foo", + "eggModule": { + "name": "foo" + } +} diff --git a/test/fixtures/example-ts/app/router.ts b/test/fixtures/example-ts/app/router.ts index 9c6705ba..d211ab6b 100644 --- a/test/fixtures/example-ts/app/router.ts +++ b/test/fixtures/example-ts/app/router.ts @@ -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); }; diff --git a/test/fixtures/example-ts/package.json b/test/fixtures/example-ts/package.json index c0670f9a..7ee523ea 100644 --- a/test/fixtures/example-ts/package.json +++ b/test/fixtures/example-ts/package.json @@ -4,6 +4,6 @@ "typescript": true }, "devDependencies": { - "@eggjs/mock": "beta" + "@eggjs/mock": "6" } } diff --git a/test/fixtures/example-ts/test/index.test.ts b/test/fixtures/example-ts/test/index.test.ts index ddfeda85..a089e112 100644 --- a/test/fixtures/example-ts/test/index.test.ts +++ b/test/fixtures/example-ts/test/index.test.ts @@ -1,7 +1,8 @@ -// @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() @@ -9,4 +10,17 @@ describe('test/index.test.ts', () => { .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'); + }); }); diff --git a/test/fixtures/example-ts/tsconfig.json b/test/fixtures/example-ts/tsconfig.json index 376b9024..2128873b 100644 --- a/test/fixtures/example-ts/tsconfig.json +++ b/test/fixtures/example-ts/tsconfig.json @@ -1,3 +1,13 @@ { - "extends": "@eggjs/tsconfig" + "extends": "@eggjs/tsconfig", + "compilerOptions": { + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "declaration": false, + "paths": { + "@/module/*": ["./app/module/*"] + }, + "baseUrl": "." + } }