diff --git a/README.md b/README.md index 62b3f2ac..ea727f90 100644 --- a/README.md +++ b/README.md @@ -227,8 +227,7 @@ Param | Type | Description -------------- | -------------- | ------------------------ directory | `String/Array` | directories to be loaded target | `Object` | attach the target object from loaded files -match | `String/Array` | match the files when load, default to `**/*.js`(if typescript was true, default to `[ '**/*.(js|ts)', '!**/*.d.ts' ]`) -typescript | `Boolean` | whether support typescript +match | `String/Array` | match the files when load, default to `**/*.js`(if process.env.EGG_TYPESCRIPT was true, default to `[ '**/*.(js|ts)', '!**/*.d.ts' ]`) ignore | `String/Array` | ignore the files when load initializer | `Function` | custom file exports, receive two parameters, first is the inject object(if not js file, will be content buffer), second is an `options` object that contain `path` caseStyle | `String/Function` | set property's case when converting a filepath to property list. diff --git a/lib/loader/egg_loader.js b/lib/loader/egg_loader.js index f9fc35ae..532a45ae 100644 --- a/lib/loader/egg_loader.js +++ b/lib/loader/egg_loader.js @@ -18,7 +18,6 @@ class EggLoader { * @constructor * @param {Object} options - options * @param {String} options.baseDir - the directory of application - * @param {Boolean} options.typescript - whether support typescript * @param {EggCore} options.app - Application instance * @param {Logger} options.logger - logger * @param {Object} [options.plugins] - custom plugins @@ -30,10 +29,6 @@ class EggLoader { assert(this.options.app, 'options.app is required'); assert(this.options.logger, 'options.logger is required'); - if (options.typescript && !require.extensions['.ts']) { - this.options.logger.warn('[egg:loader] `require.extensions` should contains `.ts` while `options.typescript` was true'); - } - this.app = this.options.app; /** @@ -359,7 +354,6 @@ class EggLoader { directory, target, inject: this.app, - typescript: this.options.typescript, }, opt); new FileLoader(opt).load(); } @@ -376,7 +370,6 @@ class EggLoader { directory, property, inject: this.app, - typescript: this.options.typescript, }, opt); new ContextLoader(opt).load(); } @@ -415,7 +408,7 @@ class EggLoader { return undefined; } - if (!this.options.typescript && fullPath.endsWith('.ts')) { + if (process.env.EGG_TYPESCRIPT !== 'true' && fullPath.endsWith('.ts')) { return undefined; } diff --git a/lib/loader/file_loader.js b/lib/loader/file_loader.js index f88b00f7..a38978bc 100644 --- a/lib/loader/file_loader.js +++ b/lib/loader/file_loader.js @@ -15,7 +15,6 @@ const defaults = { directory: null, target: null, match: undefined, - typescript: false, ignore: undefined, lowercaseFirst: false, caseStyle: 'camel', @@ -38,7 +37,6 @@ class FileLoader { * @param {String|Array} options.directory - directories to be loaded * @param {Object} options.target - attach the target object from loaded files * @param {String} options.match - match the files when load, support glob, default to all js files - * @param {Boolean} options.typescript - whether support typescript, default to false * @param {String} options.ignore - ignore the files when load, support glob * @param {Function} options.initializer - custom file exports, receive two parameters, first is the inject object(if not js file, will be content buffer), second is an `options` object that contain `path` * @param {Boolean} options.call - determine whether invoke when exports is function @@ -124,7 +122,7 @@ class FileLoader { parse() { let files = this.options.match; if (!files) { - files = (this.options.typescript && require.extensions['.ts']) + files = (process.env.EGG_TYPESCRIPT === 'true' && require.extensions['.ts']) ? [ '**/*.(js|ts)', '!**/*.d.ts' ] : [ '**/*.js' ]; } else { diff --git a/test/egg-ts.test.js b/test/egg-ts.test.js index 8cb072d0..dfd0d490 100644 --- a/test/egg-ts.test.js +++ b/test/egg-ts.test.js @@ -1,5 +1,6 @@ 'use strict'; +const mm = require('mm'); const request = require('supertest'); const assert = require('assert'); const utils = require('./utils'); @@ -12,13 +13,13 @@ describe('test/egg-ts.test.js', () => { }); afterEach(() => { + mm.restore(); delete require.extensions['.ts']; }); it('should support load ts file', async () => { - app = utils.createApp('egg-ts', { - typescript: true, - }); + mm(process.env, 'EGG_TYPESCRIPT', 'true'); + app = utils.createApp('egg-ts'); app.Helper = class Helper {}; app.loader.loadPlugin(); @@ -57,9 +58,8 @@ describe('test/egg-ts.test.js', () => { }); it('should not load d.ts files while typescript was true', async () => { - app = utils.createApp('egg-ts-js', { - typescript: true, - }); + mm(process.env, 'EGG_TYPESCRIPT', 'true'); + app = utils.createApp('egg-ts-js'); app.loader.loadController(); assert(!app.controller.god); @@ -67,16 +67,15 @@ describe('test/egg-ts.test.js', () => { }); it('should support load ts,js files', async () => { - app = utils.createApp('egg-ts-js', { - typescript: true, - }); + mm(process.env, 'EGG_TYPESCRIPT', 'true'); + app = utils.createApp('egg-ts-js'); app.loader.loadService(); assert(app.serviceClasses.lord); assert(app.serviceClasses.test); }); - it('should not load ts files while typescript was false', async () => { + it('should not load ts files while EGG_TYPESCRIPT was not exist', async () => { app = utils.createApp('egg-ts-js'); app.loader.loadApplicationExtend(); @@ -86,9 +85,10 @@ describe('test/egg-ts.test.js', () => { assert(!app.serviceClasses.test); }); - it('should not load ts files while typescript was true but no extensions', async () => { + it('should not load ts files while EGG_TYPESCRIPT was true but no extensions', async () => { + mm(process.env, 'EGG_TYPESCRIPT', 'true'); delete require.extensions['.ts']; - app = utils.createApp('egg-ts-js', { typescript: true }); + app = utils.createApp('egg-ts-js'); app.loader.loadApplicationExtend(); app.loader.loadService(); diff --git a/test/loader/egg_loader.test.js b/test/loader/egg_loader.test.js index 1727d257..1eab642a 100644 --- a/test/loader/egg_loader.test.js +++ b/test/loader/egg_loader.test.js @@ -39,13 +39,5 @@ describe('test/loader/egg_loader.test.js', () => { mm(process.env, 'EGG_HOME', '/path/to/home'); assert(app.loader.getHomedir() === '/path/to/home'); }); - - it('should warning when typescript was true but no ts extension', done => { - mm(process.stdout, 'write', msg => { - assert(msg.includes('`require.extensions` should contains `.ts` while `options.typescript` was true')); - done(); - }); - utils.createApp('nothing', { typescript: true }); - }); }); });