From abd6a705b30b68ccb982a9a923f0bf562336aaf0 Mon Sep 17 00:00:00 2001 From: Eli Perelman Date: Wed, 5 Sep 2018 13:45:47 -0500 Subject: [PATCH 1/3] Use the Resolve API to define ResolveLoader according to webpack --- README.md | 45 +++++++++++++++++++------------------------- src/ResolveLoader.js | 12 +++--------- 2 files changed, 22 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 430c679..3ebf387 100644 --- a/README.md +++ b/README.md @@ -529,31 +529,8 @@ config.resolve.mainFiles #### Config resolveLoader -```js -config.resolveLoader : ChainedMap -``` - -#### Config resolveLoader extensions - -```js -config.resolveLoader.extensions : ChainedSet - -config.resolveLoader.extensions - .add(value) - .prepend(value) - .clear() -``` - -#### Config resolveLoader modules - -```js -config.resolveLoader.modules : ChainedSet - -config.resolveLoader.modules - .add(value) - .prepend(value) - .clear() -``` +The API for `config.resolveLoader` is identical to `config.resolve` with +the following additions: #### Config resolveLoader moduleExtensions @@ -1088,10 +1065,26 @@ config.merge({ resolveLoader: { [key]: value, + alias: { + [key]: value + }, + aliasFields: [...values], + descriptionFields: [...values], extensions: [...values], + mainFields: [...values], + mainFiles: [...values], modules: [...values], moduleExtensions: [...values], - packageMains: [...values] + packageMains: [...values], + + plugin: { + [name]: { + plugin: WebpackPlugin, + args: [...args], + before, + after + } + } }, module: { diff --git a/src/ResolveLoader.js b/src/ResolveLoader.js index 0825e66..1f083b8 100644 --- a/src/ResolveLoader.js +++ b/src/ResolveLoader.js @@ -1,11 +1,9 @@ -const ChainedMap = require('./ChainedMap'); +const Resolve = require('./Resolve'); const ChainedSet = require('./ChainedSet'); -module.exports = class extends ChainedMap { +module.exports = class extends Resolve { constructor(parent) { super(parent); - this.extensions = new ChainedSet(this); - this.modules = new ChainedSet(this); this.moduleExtensions = new ChainedSet(this); this.packageMains = new ChainedSet(this); } @@ -14,20 +12,16 @@ module.exports = class extends ChainedMap { return this.clean( Object.assign( { - extensions: this.extensions.values(), - modules: this.modules.values(), moduleExtensions: this.moduleExtensions.values(), packageMains: this.packageMains.values(), }, - this.entries() || {} + super.toConfig() ) ); } merge(obj, omit = []) { const omissions = [ - 'extensions', - 'modules', 'moduleExtensions', 'packageMains', ]; From a5f98a9cbbabbbe0be284e16c5a99e1592daf106 Mon Sep 17 00:00:00 2001 From: Eli Perelman Date: Wed, 5 Sep 2018 13:49:17 -0500 Subject: [PATCH 2/3] Fix linting error --- src/ResolveLoader.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/ResolveLoader.js b/src/ResolveLoader.js index 1f083b8..e1491a3 100644 --- a/src/ResolveLoader.js +++ b/src/ResolveLoader.js @@ -21,10 +21,7 @@ module.exports = class extends Resolve { } merge(obj, omit = []) { - const omissions = [ - 'moduleExtensions', - 'packageMains', - ]; + const omissions = ['moduleExtensions', 'packageMains']; omissions.forEach(key => { if (!omit.includes(key) && key in obj) { From 9b1ccac2ce5c07955a1589fa5ab1a84b53a67d35 Mon Sep 17 00:00:00 2001 From: Eli Perelman Date: Thu, 6 Sep 2018 12:06:35 -0500 Subject: [PATCH 3/3] Test shorthands and plugins with ResolveLoader --- test/ResolveLoader.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/ResolveLoader.js b/test/ResolveLoader.js index ddaacb5..1c020ec 100644 --- a/test/ResolveLoader.js +++ b/test/ResolveLoader.js @@ -8,6 +8,18 @@ test('is Chainable', t => { t.is(resolveLoader.end(), parent); }); +test('shorthand methods', t => { + const resolveLoader = new ResolveLoader(); + const obj = {}; + + resolveLoader.shorthands.forEach(method => { + obj[method] = 'alpha'; + t.is(resolveLoader[method]('alpha'), resolveLoader); + }); + + t.deepEqual(resolveLoader.entries(), obj); +}); + test('sets methods', t => { const resolveLoader = new ResolveLoader(); const instance = resolveLoader.modules.add('src').end(); @@ -88,3 +100,11 @@ test('merge with omit', t => { moduleExtensions: ['-loader'], }); }); + +test('plugin with name', t => { + const resolveLoader = new ResolveLoader(); + + resolveLoader.plugin('alpha'); + + t.is(resolveLoader.plugins.get('alpha').name, 'alpha'); +});