diff --git a/lib/loader/mixin/plugin.js b/lib/loader/mixin/plugin.js index c7a9bc9c..4fead235 100644 --- a/lib/loader/mixin/plugin.js +++ b/lib/loader/mixin/plugin.js @@ -148,6 +148,13 @@ module.exports = { configPaths = [ configPaths ]; } + // read plugin.js and plugins.${env}.js + // note: can't use for-of + for (let i = 0, l = configPaths.length; i < l; i++) { + const configPath = configPaths[i]; + configPaths.push(configPath.replace(/plugin.js$/, `plugin.${this.serverEnv}.js`)); + } + const plugins = {}; for (const configPath of configPaths) { if (!fs.existsSync(configPath)) { diff --git a/test/fixtures/load-plugin-by-env/config/plugin.custom.js b/test/fixtures/load-plugin-by-env/config/plugin.custom.js new file mode 100644 index 00000000..46419f0a --- /dev/null +++ b/test/fixtures/load-plugin-by-env/config/plugin.custom.js @@ -0,0 +1,8 @@ +'use strict'; + +const path = require('path'); + +exports.c = { + enable: true, + path: path.join(__dirname, '../plugins/c'), +} diff --git a/test/fixtures/load-plugin-by-env/config/plugin.js b/test/fixtures/load-plugin-by-env/config/plugin.js new file mode 100644 index 00000000..ef9b5024 --- /dev/null +++ b/test/fixtures/load-plugin-by-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/load-plugin-by-env/config/plugin.unittest.js b/test/fixtures/load-plugin-by-env/config/plugin.unittest.js new file mode 100644 index 00000000..1fefc15b --- /dev/null +++ b/test/fixtures/load-plugin-by-env/config/plugin.unittest.js @@ -0,0 +1,10 @@ +'use strict'; + +const path = require('path'); + +exports.a = false; + +exports.b = { + enable: true, + path: path.join(__dirname, '../plugins/b'), +} diff --git a/test/fixtures/load-plugin-by-env/package.json b/test/fixtures/load-plugin-by-env/package.json new file mode 100644 index 00000000..2b4addd6 --- /dev/null +++ b/test/fixtures/load-plugin-by-env/package.json @@ -0,0 +1,3 @@ +{ + "name": "load-plugin-by-env" +} diff --git a/test/fixtures/load-plugin-by-env/plugins/a/package.json b/test/fixtures/load-plugin-by-env/plugins/a/package.json new file mode 100644 index 00000000..0993be8f --- /dev/null +++ b/test/fixtures/load-plugin-by-env/plugins/a/package.json @@ -0,0 +1,5 @@ +{ + "eggPlugin": { + "name": "a" + } +} diff --git a/test/fixtures/load-plugin-by-env/plugins/b/package.json b/test/fixtures/load-plugin-by-env/plugins/b/package.json new file mode 100644 index 00000000..09dc2b7d --- /dev/null +++ b/test/fixtures/load-plugin-by-env/plugins/b/package.json @@ -0,0 +1,5 @@ +{ + "eggPlugin": { + "name": "b" + } +} diff --git a/test/fixtures/load-plugin-by-env/plugins/c/package.json b/test/fixtures/load-plugin-by-env/plugins/c/package.json new file mode 100644 index 00000000..2df18dea --- /dev/null +++ b/test/fixtures/load-plugin-by-env/plugins/c/package.json @@ -0,0 +1,5 @@ +{ + "eggPlugin": { + "name": "c" + } +} diff --git a/test/loader/mixin/load_plugin.test.js b/test/loader/mixin/load_plugin.test.js index 3e394800..9cf5e9d3 100644 --- a/test/loader/mixin/load_plugin.test.js +++ b/test/loader/mixin/load_plugin.test.js @@ -386,4 +386,23 @@ describe('test/load_plugin.test.js', function() { loader.plugins.a.from.should.equal(utils.getFilepath('plugin-from/config/plugin.js')); loader.plugins.b.from.should.equal(utils.getFilepath('plugin-from/framework/config/plugin.js')); }); + + it('should load plugin.unittest.js override default', function() { + mm(process.env, 'EGG_SERVER_ENV', 'unittest'); + app = utils.createApp('load-plugin-by-env'); + const loader = app.loader; + loader.loadPlugin(); + loader.allPlugins.a.enable.should.be.false(); + loader.allPlugins.b.enable.should.be.true(); + }); + + it('should load plugin.custom.js when env is custom', function() { + mm(process.env, 'EGG_SERVER_ENV', 'custom'); + app = utils.createApp('load-plugin-by-env'); + const loader = app.loader; + loader.loadPlugin(); + loader.allPlugins.a.enable.should.be.true(); + should.not.exists(loader.allPlugins.b); + loader.allPlugins.c.enable.should.be.true(); + }); });