Skip to content

Commit

Permalink
feat: support {env}.js when load extend
Browse files Browse the repository at this point in the history
it's useful when want to export some unittest api
that can't be used in production
  • Loading branch information
popomore committed Oct 19, 2016
1 parent 6fc2dd0 commit d1d20e5
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/loader/mixin/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ module.exports = {
loadExtend(name, proto) {
// 获取需要加载的文件
const filepaths = this.getLoadUnits().map(unit => path.join(unit.path, 'app/extend', name));
for (let i = 0, l = filepaths.length; i < l; i++) {
const filepath = filepaths[i];
filepaths.push(filepath + `.${this.serverEnv}`);
}


const mergeRecord = new Map();
for (const filepath of filepaths) {
if (!utils.existsModule(filepath)) {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/extend-env/app/extend/application.custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.a = 'a1';

exports.custom = true;
4 changes: 4 additions & 0 deletions test/fixtures/extend-env/app/extend/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';

exports.a = 'a';
exports.b = 'b';
8 changes: 8 additions & 0 deletions test/fixtures/extend-env/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

const path = require('path');

exports.a = {
enable: true,
path: path.join(__dirname, '../plugins/a'),
};
3 changes: 3 additions & 0 deletions test/fixtures/extend-env/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "extend-env"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.b = 'b1';
5 changes: 5 additions & 0 deletions test/fixtures/extend-env/plugins/a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"eggPlugin": {
"name": "a"
}
}
14 changes: 14 additions & 0 deletions test/loader/mixin/load_extend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require('should');
const request = require('supertest');
const mm = require('mm');
const utils = require('../../utils');

describe('test/loader/mixin/load_extend.test.js', function() {
Expand Down Expand Up @@ -95,4 +96,17 @@ describe('test/loader/mixin/load_extend.test.js', function() {
app.loader.loadApplicationExtend();
app[utils.symbol.view].should.equal('view');
});

it('should load application by custom env', function() {
mm(process.env, 'EGG_SERVER_ENV', 'custom');
const app = utils.createApp('extend-env');
app.loader.loadPlugin();
app.loader.loadApplicationExtend();
app.custom.should.be.true();
// application.custom.js override application.js
app.a.should.eql('a1');
// application.custom.js in plugin also can override application.js in app
app.b.should.eql('b1');
});

});

0 comments on commit d1d20e5

Please sign in to comment.