Skip to content

Commit

Permalink
feat: support plugin.{env}.js
Browse files Browse the repository at this point in the history
Control bundle of plugins by env easily
  • Loading branch information
popomore committed Oct 17, 2016
1 parent fba2943 commit 160a9f2
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/loader/mixin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/load-plugin-by-env/config/plugin.custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

const path = require('path');

exports.c = {
enable: true,
path: path.join(__dirname, '../plugins/c'),
}
8 changes: 8 additions & 0 deletions test/fixtures/load-plugin-by-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'),
}
10 changes: 10 additions & 0 deletions test/fixtures/load-plugin-by-env/config/plugin.unittest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const path = require('path');

exports.a = false;

exports.b = {
enable: true,
path: path.join(__dirname, '../plugins/b'),
}
3 changes: 3 additions & 0 deletions test/fixtures/load-plugin-by-env/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "load-plugin-by-env"
}
5 changes: 5 additions & 0 deletions test/fixtures/load-plugin-by-env/plugins/a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"eggPlugin": {
"name": "a"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/load-plugin-by-env/plugins/b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"eggPlugin": {
"name": "b"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/load-plugin-by-env/plugins/c/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"eggPlugin": {
"name": "c"
}
}
19 changes: 19 additions & 0 deletions test/loader/mixin/load_plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 160a9f2

Please sign in to comment.