diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 172a9d6a..e40dcc95 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -2,10 +2,10 @@ name: CI on: push: - branches: [ master ] + branches: [ master, 4.x ] pull_request: - branches: [ master ] + branches: [ master, 4.x ] workflow_dispatch: {} diff --git a/lib/loader/egg_loader.js b/lib/loader/egg_loader.js index 9bbba45b..b19ab7b5 100644 --- a/lib/loader/egg_loader.js +++ b/lib/loader/egg_loader.js @@ -46,7 +46,13 @@ class EggLoader { // auto require('tsconfig-paths/register') on typescript app // support env.EGG_TYPESCRIPT = true or { "egg": { "typescript": true } } on package.json if (process.env.EGG_TYPESCRIPT === 'true' || (this.pkg.egg && this.pkg.egg.typescript)) { - require('tsconfig-paths').register({ cwd: this.options.baseDir }); + // skip require tsconfig-paths if tsconfig.json not exists + const tsConfigFile = path.join(this.options.baseDir, 'tsconfig.json'); + if (fs.existsSync(tsConfigFile)) { + require('tsconfig-paths').register({ cwd: this.options.baseDir }); + } else { + this.options.logger.info('[egg:loader] skip register "tsconfig-paths" because tsconfig.json not exists at %s', tsConfigFile); + } } /** diff --git a/test/egg-ts.test.js b/test/egg-ts.test.js index 6a82ff68..0a4d6869 100644 --- a/test/egg-ts.test.js +++ b/test/egg-ts.test.js @@ -122,6 +122,15 @@ describe('test/egg-ts.test.js', () => { assert(app.serviceClasses.test); }); + it('should auto require tsconfig-paths', async () => { + mm(process.env, 'EGG_TYPESCRIPT', 'true'); + app = utils.createApp('egg-ts-js-tsconfig-paths'); + + app.loader.loadService(); + assert(app.serviceClasses.lord); + assert(app.serviceClasses.test); + }); + it('should not load ts files while EGG_TYPESCRIPT was not exist', async () => { app = utils.createApp('egg-ts-js'); diff --git a/test/fixtures/egg-ts-js-tsconfig-paths/app/controller/god.d.ts b/test/fixtures/egg-ts-js-tsconfig-paths/app/controller/god.d.ts new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/egg-ts-js-tsconfig-paths/app/controller/test.ts b/test/fixtures/egg-ts-js-tsconfig-paths/app/controller/test.ts new file mode 100644 index 00000000..e868e3f7 --- /dev/null +++ b/test/fixtures/egg-ts-js-tsconfig-paths/app/controller/test.ts @@ -0,0 +1,3 @@ +module.exports = async ctx => { + ctx.body = 'ok'; +} \ No newline at end of file diff --git a/test/fixtures/egg-ts-js-tsconfig-paths/app/extend/application.ts b/test/fixtures/egg-ts-js-tsconfig-paths/app/extend/application.ts new file mode 100644 index 00000000..a867bb33 --- /dev/null +++ b/test/fixtures/egg-ts-js-tsconfig-paths/app/extend/application.ts @@ -0,0 +1,3 @@ +module.exports = { + appExtend: 'hello' +} \ No newline at end of file diff --git a/test/fixtures/egg-ts-js-tsconfig-paths/app/service/lord.js b/test/fixtures/egg-ts-js-tsconfig-paths/app/service/lord.js new file mode 100644 index 00000000..c9d1df43 --- /dev/null +++ b/test/fixtures/egg-ts-js-tsconfig-paths/app/service/lord.js @@ -0,0 +1,5 @@ +module.exports = class LordService { + jsService() { + return 'from js service'; + } +} \ No newline at end of file diff --git a/test/fixtures/egg-ts-js-tsconfig-paths/app/service/test.ts b/test/fixtures/egg-ts-js-tsconfig-paths/app/service/test.ts new file mode 100644 index 00000000..96ac64eb --- /dev/null +++ b/test/fixtures/egg-ts-js-tsconfig-paths/app/service/test.ts @@ -0,0 +1,5 @@ +module.exports = class TestService { + tsService() { + return 'from ts service'; + } +} \ No newline at end of file diff --git a/test/fixtures/egg-ts-js-tsconfig-paths/package.json b/test/fixtures/egg-ts-js-tsconfig-paths/package.json new file mode 100644 index 00000000..d71db5a8 --- /dev/null +++ b/test/fixtures/egg-ts-js-tsconfig-paths/package.json @@ -0,0 +1,3 @@ +{ + "name": "egg-ts" +} diff --git a/test/fixtures/egg-ts-js-tsconfig-paths/tsconfig.json b/test/fixtures/egg-ts-js-tsconfig-paths/tsconfig.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/test/fixtures/egg-ts-js-tsconfig-paths/tsconfig.json @@ -0,0 +1 @@ +{}