Skip to content

Commit

Permalink
fix: skip register tsconfig-paths if tsconfig.json not exists (#261)
Browse files Browse the repository at this point in the history
Avoid warning message: Couldn't find tsconfig.json. tsconfig-paths will be skipped

closes #254 (comment)
  • Loading branch information
fengmk2 committed Apr 6, 2023
1 parent 5cc4023 commit 24a0d64
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
name: Node.js
uses: artusjs/github-actions/.github/workflows/node-test.yml@v1
with:
version: '14.17.0, 14, 16, 18'
version: '14.19.0, 14, 16, 18'
8 changes: 7 additions & 1 deletion lib/loader/egg_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"homepage": "https://github.com/eggjs/egg-core#readme",
"engines": {
"node": ">= 14.17.0"
"node": ">= 14.19.0"
},
"devDependencies": {
"await-event": "^2.1.0",
Expand Down
9 changes: 9 additions & 0 deletions test/egg-ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,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');

Expand Down
Empty file.
3 changes: 3 additions & 0 deletions test/fixtures/egg-ts-js-tsconfig-paths/app/controller/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = async ctx => {
ctx.body = 'ok';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
appExtend: 'hello'
}
5 changes: 5 additions & 0 deletions test/fixtures/egg-ts-js-tsconfig-paths/app/service/lord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = class LordService {
jsService() {
return 'from js service';
}
}
5 changes: 5 additions & 0 deletions test/fixtures/egg-ts-js-tsconfig-paths/app/service/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = class TestService {
tsService() {
return 'from ts service';
}
}
3 changes: 3 additions & 0 deletions test/fixtures/egg-ts-js-tsconfig-paths/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "egg-ts"
}
1 change: 1 addition & 0 deletions test/fixtures/egg-ts-js-tsconfig-paths/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit 24a0d64

Please sign in to comment.