Skip to content

Commit 7f087e7

Browse files
whxaxespopomore
authored andcommitted
feat: change assert to warning (#157)
1 parent 063185d commit 7f087e7

5 files changed

Lines changed: 26 additions & 17 deletions

File tree

lib/loader/egg_loader.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class EggLoader {
3030
assert(this.options.app, 'options.app is required');
3131
assert(this.options.logger, 'options.logger is required');
3232

33+
if (options.typescript && !require.extensions['.ts']) {
34+
this.options.logger.warn('[egg:loader] `require.extensions` should contains `.ts` while `options.typescript` was true');
35+
}
36+
3337
this.app = this.options.app;
3438

3539
/**

lib/loader/file_loader.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ class FileLoader {
5050
constructor(options) {
5151
assert(options.directory, 'options.directory is required');
5252
assert(options.target, 'options.target is required');
53-
if (options.typescript) {
54-
assert(require.extensions['.ts'], '`require.extensions` should contains `.ts` while `options.typescript` was true');
55-
}
5653
this.options = Object.assign({}, defaults, options);
5754

5855
// compatible old options _lowercaseFirst_
@@ -127,7 +124,7 @@ class FileLoader {
127124
parse() {
128125
let files = this.options.match;
129126
if (!files) {
130-
files = this.options.typescript
127+
files = (this.options.typescript && require.extensions['.ts'])
131128
? [ '**/*.(js|ts)', '!**/*.d.ts' ]
132129
: [ '**/*.js' ];
133130
} else {

test/egg-ts.test.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ const utils = require('./utils');
77
describe('test/egg-ts.test.js', () => {
88
let app;
99

10-
before(() => {
10+
beforeEach(() => {
1111
require.extensions['.ts'] = require.extensions['.js'];
1212
});
1313

14-
after(() => {
14+
afterEach(() => {
1515
delete require.extensions['.ts'];
1616
});
1717

@@ -85,4 +85,15 @@ describe('test/egg-ts.test.js', () => {
8585
assert(app.serviceClasses.lord);
8686
assert(!app.serviceClasses.test);
8787
});
88+
89+
it('should not load ts files while typescript was true but no extensions', async () => {
90+
delete require.extensions['.ts'];
91+
app = utils.createApp('egg-ts-js', { typescript: true });
92+
93+
app.loader.loadApplicationExtend();
94+
app.loader.loadService();
95+
assert(!app.appExtend);
96+
assert(app.serviceClasses.lord);
97+
assert(!app.serviceClasses.test);
98+
});
8899
});

test/loader/egg_loader.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,13 @@ describe('test/loader/egg_loader.test.js', () => {
3939
mm(process.env, 'EGG_HOME', '/path/to/home');
4040
assert(app.loader.getHomedir() === '/path/to/home');
4141
});
42+
43+
it('should warning when typescript was true but no ts extension', done => {
44+
mm(process.stdout, 'write', msg => {
45+
assert(msg.includes('`require.extensions` should contains `.ts` while `options.typescript` was true'));
46+
done();
47+
});
48+
utils.createApp('nothing', { typescript: true });
49+
});
4250
});
4351
});

test/loader/file_loader.test.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,17 +254,6 @@ describe('test/loader/file_loader.test.js', () => {
254254
}, /_private is not match 'a-z0-9_-' in _private.js/);
255255
});
256256

257-
it('should throw when typescript was true but no ts extension', () => {
258-
const mod = {};
259-
assert.throws(() => {
260-
new FileLoader({
261-
directory: path.join(dirBase, 'error/dotdir'),
262-
target: mod,
263-
typescript: true,
264-
}).load();
265-
}, /`require.extensions` should contains `.ts` while `options.typescript` was true/);
266-
});
267-
268257
describe('caseStyle', () => {
269258
it('should load when caseStyle = upper', () => {
270259
const target = {};

0 commit comments

Comments
 (0)