diff --git a/lib/loader/mixin/extend.js b/lib/loader/mixin/extend.js index c2fbe17a..f24aa8cc 100644 --- a/lib/loader/mixin/extend.js +++ b/lib/loader/mixin/extend.js @@ -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)) { diff --git a/test/fixtures/extend-env/app/extend/application.custom.js b/test/fixtures/extend-env/app/extend/application.custom.js new file mode 100644 index 00000000..dd3aa85c --- /dev/null +++ b/test/fixtures/extend-env/app/extend/application.custom.js @@ -0,0 +1,5 @@ +'use strict'; + +exports.a = 'a1'; + +exports.custom = true; diff --git a/test/fixtures/extend-env/app/extend/application.js b/test/fixtures/extend-env/app/extend/application.js new file mode 100644 index 00000000..6161bff4 --- /dev/null +++ b/test/fixtures/extend-env/app/extend/application.js @@ -0,0 +1,4 @@ +'use strict'; + +exports.a = 'a'; +exports.b = 'b'; diff --git a/test/fixtures/extend-env/config/plugin.js b/test/fixtures/extend-env/config/plugin.js new file mode 100644 index 00000000..5cf8a060 --- /dev/null +++ b/test/fixtures/extend-env/config/plugin.js @@ -0,0 +1,8 @@ +'use strict'; + +const path = require('path'); + +exports.a = { + enable: true, + path: path.join(__dirname, '../plugins/a'), +}; diff --git a/test/fixtures/extend-env/package.json b/test/fixtures/extend-env/package.json new file mode 100644 index 00000000..691c4598 --- /dev/null +++ b/test/fixtures/extend-env/package.json @@ -0,0 +1,3 @@ +{ + "name": "extend-env" +} diff --git a/test/fixtures/extend-env/plugins/a/app/extend/application.custom.js b/test/fixtures/extend-env/plugins/a/app/extend/application.custom.js new file mode 100644 index 00000000..61958cb8 --- /dev/null +++ b/test/fixtures/extend-env/plugins/a/app/extend/application.custom.js @@ -0,0 +1,3 @@ +'use strict'; + +exports.b = 'b1'; diff --git a/test/fixtures/extend-env/plugins/a/package.json b/test/fixtures/extend-env/plugins/a/package.json new file mode 100644 index 00000000..0993be8f --- /dev/null +++ b/test/fixtures/extend-env/plugins/a/package.json @@ -0,0 +1,5 @@ +{ + "eggPlugin": { + "name": "a" + } +} diff --git a/test/loader/mixin/load_extend.test.js b/test/loader/mixin/load_extend.test.js index b06bb74a..07fff6f5 100644 --- a/test/loader/mixin/load_extend.test.js +++ b/test/loader/mixin/load_extend.test.js @@ -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() { @@ -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'); + }); + });