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: add plugin.from where declare the plugin #13

Merged
merged 1 commit into from
Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions lib/loader/mixin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ module.exports = {
const config = loadFile(configPath);

for (const name in config) {
this.normalizePluginConfig(config, name);
this.normalizePluginConfig(config, name, configPath);
}

extendPlugins(plugins, config);
Expand All @@ -166,7 +166,7 @@ module.exports = {
return plugins;
},

normalizePluginConfig(plugins, name) {
normalizePluginConfig(plugins, name, configPath) {
const plugin = plugins[name];

// plugin_name: false
Expand All @@ -176,6 +176,7 @@ module.exports = {
enable: plugin,
dep: [],
env: [],
from: configPath,
};
return;
}
Expand All @@ -186,6 +187,7 @@ module.exports = {
plugin.name = name;
plugin.dep = plugin.dep || [];
plugin.env = plugin.env || [];
plugin.from = configPath;
},

// Read plugin infomation from package.json and merge
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/plugin-from/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/plugin-from/b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"eggPlugin": {
"name": "b"
}
}
3 changes: 3 additions & 0 deletions test/fixtures/plugin-from/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.a = true;
14 changes: 14 additions & 0 deletions test/fixtures/plugin-from/framework/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const path = require('path');

module.exports = {
a: {
enable: false,
path: path.join(__dirname, '../../a'),
},
b: {
enable: true,
path: path.join(__dirname, '../../b'),
},
};
3 changes: 3 additions & 0 deletions test/fixtures/plugin-from/framework/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "plugin-from-framework"
}
3 changes: 3 additions & 0 deletions test/fixtures/plugin-from/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "plugin-from"
}
27 changes: 27 additions & 0 deletions test/loader/mixin/load_plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const fs = require('fs');
const mm = require('mm');
const rimraf = require('rimraf');
const utils = require('../../utils');
const EggCore = require('../../..').EggCore;
const EggLoader = require('../../..').EggLoader;

describe('test/load_plugin.test.js', function() {

Expand All @@ -25,20 +27,23 @@ describe('test/load_plugin.test.js', function() {
dep: [],
env: [],
path: path.join(baseDir, 'node_modules/b'),
from: path.join(baseDir, 'config/plugin.js'),
});
loader.plugins.c.should.eql({
enable: true,
name: 'c',
dep: [],
env: [],
path: path.join(baseDir, 'node_modules/c'),
from: path.join(baseDir, 'config/plugin.js'),
});
loader.plugins.e.should.eql({
enable: true,
name: 'e',
dep: [ 'f' ],
env: [],
path: path.join(baseDir, 'plugins/e'),
from: path.join(baseDir, 'config/plugin.js'),
});
loader.orderPlugins.should.be.an.Array;
});
Expand All @@ -57,6 +62,7 @@ describe('test/load_plugin.test.js', function() {
env: [],
package: 'rds',
path: path.join(baseDir, 'node_modules/rds'),
from: path.join(baseDir, 'config/plugin.js'),
});
});

Expand All @@ -74,6 +80,7 @@ describe('test/load_plugin.test.js', function() {
dep: [],
env: [],
path: path.join(baseDir, 'node_modules/d'),
from: path.join(baseDir, 'config/plugin.js'),
});
should.not.exists(loader.plugins.d);
});
Expand All @@ -92,6 +99,7 @@ describe('test/load_plugin.test.js', function() {
env: [],
path: path.join(baseDir, 'plugins/g'),
version: '1.0.0',
from: path.join(baseDir, 'config/plugin.js'),
});
});

Expand Down Expand Up @@ -133,6 +141,7 @@ describe('test/load_plugin.test.js', function() {
dep: [],
env: [ 'unittest' ],
path: path.join(baseDir, 'node_modules/d'),
from: path.join(baseDir, 'config/plugin.js'),
});
loader.plugins.foo.should.eql({
enable: true,
Expand Down Expand Up @@ -339,6 +348,7 @@ describe('test/load_plugin.test.js', function() {
dep: [ 'd1' ],
env: [ 'local', 'prod' ],
path: path.join(baseDir, 'node_modules/a1'),
from: path.join(baseDir, 'config/plugin.js'),
});
});

Expand Down Expand Up @@ -383,4 +393,21 @@ describe('test/load_plugin.test.js', function() {
plugin.name.should.equal('a');
plugin.path.should.equal(utils.getFilepath('realpath/a'));
});

it('should get the defining plugin path in every plugin', () => {
app = utils.createApp('plugin-from', {
Application: class Application extends EggCore {
get [Symbol.for('egg#loader')]() {
return EggLoader;
}
get [Symbol.for('egg#eggPath')]() {
return utils.getFilepath('plugin-from/framework');
}
},
});
const loader = app.loader;
loader.loadPlugin();
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'));
});
});