Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support plugin.{env}.js #20

Merged
merged 4 commits into from
Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要改 configPaths 本身?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/plugin.js$/ => /\/plugin.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();
});
});