From c47ee2d52c1be4fcdbe10adef865e45d6fd729ef Mon Sep 17 00:00:00 2001 From: Guy Date: Sat, 4 Aug 2018 12:13:32 -0500 Subject: [PATCH] merge resolve plugins just like config --- .gitignore | 3 +++ src/Resolve.js | 11 ++++++++--- test/Resolve.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 76f7224..c32b425 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,6 @@ jspm_packages # Webstorm project metadata .idea + +# macOS +.DS_Store \ No newline at end of file diff --git a/src/Resolve.js b/src/Resolve.js index ed94243..ab3b272 100644 --- a/src/Resolve.js +++ b/src/Resolve.js @@ -55,16 +55,21 @@ module.exports = class extends ChainedMap { 'extensions', 'mainFields', 'mainFiles', - 'modules', - 'plugins', + 'modules' ]; + if (!omit.includes('plugin') && 'plugin' in obj) { + Object.keys(obj.plugin).forEach(name => + this.plugin(name).merge(obj.plugin[name]) + ); + } + omissions.forEach(key => { if (!omit.includes(key) && key in obj) { this[key].merge(obj[key]); } }); - return super.merge(obj, [...omit, ...omissions]); + return super.merge(obj, [...omit, ...omissions, 'plugin']); } }; diff --git a/test/Resolve.js b/test/Resolve.js index bdd9c02..0be4912 100644 --- a/test/Resolve.js +++ b/test/Resolve.js @@ -1,6 +1,16 @@ import test from 'ava'; import Resolve from '../src/Resolve'; +class StringifyPlugin { + constructor(...args) { + this.values = args; + } + + apply() { + return JSON.stringify(this.values); + } +} + test('is Chainable', t => { const parent = { parent: true }; const resolve = new Resolve(parent); @@ -123,3 +133,25 @@ test('plugin with name', t => { t.is(resolve.plugins.get('alpha').name, 'alpha'); }); + + +test('plugin empty', t => { + const resolve = new Resolve(); + const instance = resolve + .plugin('stringify') + .use(StringifyPlugin) + .end(); + + t.is(instance, resolve); + t.true(resolve.plugins.has('stringify')); + t.deepEqual(resolve.plugins.get('stringify').get('args'), []); +}); + +test('plugin with args', t => { + const resolve = new Resolve(); + + resolve.plugin('stringify').use(StringifyPlugin, ['alpha', 'beta']); + + t.true(resolve.plugins.has('stringify')); + t.deepEqual(resolve.plugins.get('stringify').get('args'), ['alpha', 'beta']); +}); \ No newline at end of file