From 813441789fe3e2c5740113e84d08b3611aaba9e4 Mon Sep 17 00:00:00 2001 From: popomore Date: Thu, 18 Aug 2016 17:01:39 +0800 Subject: [PATCH] feat: add plugin.from where declare the plugin --- lib/loader/mixin/plugin.js | 6 +++-- test/fixtures/plugin-from/a/package.json | 5 ++++ test/fixtures/plugin-from/b/package.json | 5 ++++ test/fixtures/plugin-from/config/plugin.js | 3 +++ .../plugin-from/framework/config/plugin.js | 14 ++++++++++ .../plugin-from/framework/package.json | 3 +++ test/fixtures/plugin-from/package.json | 3 +++ test/loader/mixin/load_plugin.test.js | 27 +++++++++++++++++++ 8 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/plugin-from/a/package.json create mode 100644 test/fixtures/plugin-from/b/package.json create mode 100644 test/fixtures/plugin-from/config/plugin.js create mode 100644 test/fixtures/plugin-from/framework/config/plugin.js create mode 100644 test/fixtures/plugin-from/framework/package.json create mode 100644 test/fixtures/plugin-from/package.json diff --git a/lib/loader/mixin/plugin.js b/lib/loader/mixin/plugin.js index aceb48f2..c7a9bc9c 100644 --- a/lib/loader/mixin/plugin.js +++ b/lib/loader/mixin/plugin.js @@ -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); @@ -166,7 +166,7 @@ module.exports = { return plugins; }, - normalizePluginConfig(plugins, name) { + normalizePluginConfig(plugins, name, configPath) { const plugin = plugins[name]; // plugin_name: false @@ -176,6 +176,7 @@ module.exports = { enable: plugin, dep: [], env: [], + from: configPath, }; return; } @@ -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 diff --git a/test/fixtures/plugin-from/a/package.json b/test/fixtures/plugin-from/a/package.json new file mode 100644 index 00000000..0993be8f --- /dev/null +++ b/test/fixtures/plugin-from/a/package.json @@ -0,0 +1,5 @@ +{ + "eggPlugin": { + "name": "a" + } +} diff --git a/test/fixtures/plugin-from/b/package.json b/test/fixtures/plugin-from/b/package.json new file mode 100644 index 00000000..09dc2b7d --- /dev/null +++ b/test/fixtures/plugin-from/b/package.json @@ -0,0 +1,5 @@ +{ + "eggPlugin": { + "name": "b" + } +} diff --git a/test/fixtures/plugin-from/config/plugin.js b/test/fixtures/plugin-from/config/plugin.js new file mode 100644 index 00000000..16eecc42 --- /dev/null +++ b/test/fixtures/plugin-from/config/plugin.js @@ -0,0 +1,3 @@ +'use strict'; + +exports.a = true; diff --git a/test/fixtures/plugin-from/framework/config/plugin.js b/test/fixtures/plugin-from/framework/config/plugin.js new file mode 100644 index 00000000..aebc7c35 --- /dev/null +++ b/test/fixtures/plugin-from/framework/config/plugin.js @@ -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'), + }, +}; diff --git a/test/fixtures/plugin-from/framework/package.json b/test/fixtures/plugin-from/framework/package.json new file mode 100644 index 00000000..6db6e614 --- /dev/null +++ b/test/fixtures/plugin-from/framework/package.json @@ -0,0 +1,3 @@ +{ + "name": "plugin-from-framework" +} diff --git a/test/fixtures/plugin-from/package.json b/test/fixtures/plugin-from/package.json new file mode 100644 index 00000000..dfdf1438 --- /dev/null +++ b/test/fixtures/plugin-from/package.json @@ -0,0 +1,3 @@ +{ + "name": "plugin-from" +} diff --git a/test/loader/mixin/load_plugin.test.js b/test/loader/mixin/load_plugin.test.js index fd873394..b7d4bfda 100644 --- a/test/loader/mixin/load_plugin.test.js +++ b/test/loader/mixin/load_plugin.test.js @@ -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() { @@ -25,6 +27,7 @@ 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, @@ -32,6 +35,7 @@ describe('test/load_plugin.test.js', function() { dep: [], env: [], path: path.join(baseDir, 'node_modules/c'), + from: path.join(baseDir, 'config/plugin.js'), }); loader.plugins.e.should.eql({ enable: true, @@ -39,6 +43,7 @@ describe('test/load_plugin.test.js', function() { dep: [ 'f' ], env: [], path: path.join(baseDir, 'plugins/e'), + from: path.join(baseDir, 'config/plugin.js'), }); loader.orderPlugins.should.be.an.Array; }); @@ -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'), }); }); @@ -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); }); @@ -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'), }); }); @@ -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, @@ -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'), }); }); @@ -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')); + }); });