Skip to content

Commit

Permalink
feat: support ts by env (eggjs#158)
Browse files Browse the repository at this point in the history
* feat: support ts by env
* feat: check ts in options
* feat: remove typescript option
  • Loading branch information
whxaxes authored and hugh.hyj committed Sep 16, 2021
1 parent dc550ea commit 1ffd098
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 33 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 1 addition & 8 deletions lib/loader/egg_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

/**
Expand Down Expand Up @@ -359,7 +354,6 @@ class EggLoader {
directory,
target,
inject: this.app,
typescript: this.options.typescript,
}, opt);
new FileLoader(opt).load();
}
Expand All @@ -376,7 +370,6 @@ class EggLoader {
directory,
property,
inject: this.app,
typescript: this.options.typescript,
}, opt);
new ContextLoader(opt).load();
}
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 1 addition & 3 deletions lib/loader/file_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const defaults = {
directory: null,
target: null,
match: undefined,
typescript: false,
ignore: undefined,
lowercaseFirst: false,
caseStyle: 'camel',
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 12 additions & 12 deletions test/egg-ts.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const mm = require('mm');
const request = require('supertest');
const assert = require('assert');
const utils = require('./utils');
Expand All @@ -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();
Expand Down Expand Up @@ -57,26 +58,24 @@ 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);
assert(app.controller.test);
});

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();
Expand All @@ -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();
Expand Down
8 changes: 0 additions & 8 deletions test/loader/egg_loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
});
});

0 comments on commit 1ffd098

Please sign in to comment.