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

fix: skip register tsconfig-paths if tsconfig.json not exists #263

Merged
merged 2 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name: CI

on:
push:
branches: [ master ]
branches: [ master, 4.x ]

pull_request:
branches: [ master ]
branches: [ master, 4.x ]

workflow_dispatch: {}

Expand Down
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
9 changes: 9 additions & 0 deletions test/egg-ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

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 @@
{}