From b21f7aa6adbb2441e0ce39be2315d93b5539760a Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Wed, 14 Jun 2017 09:15:36 +0200 Subject: [PATCH 1/5] Allow to add custom loaders --- lib/WebpackConfig.js | 5 +++++ lib/config-generator.js | 6 ++++++ test/WebpackConfig.js | 18 ++++++++++++++++++ test/config-generator.js | 24 ++++++++++++++++++++++++ 4 files changed, 53 insertions(+) diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index 8cafc62b..991b5d67 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -49,6 +49,7 @@ class WebpackConfig { this.providedVariables = {}; this.babelConfigurationCallback = function() {}; this.useReact = false; + this.loaders = new Set(); } getContext() { @@ -157,6 +158,10 @@ class WebpackConfig { this.styleEntries.set(name, src); } + addLoader(test, use, options = { include: null, exclude: null }) { + this.loaders.add({ 'test': test, 'use': use, 'include': options.include || null, 'exclude': options.exclude || null }); + } + enableVersioning(enabled = true) { this.useVersioning = enabled; } diff --git a/lib/config-generator.js b/lib/config-generator.js index 83bdeb71..94302a90 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -235,6 +235,12 @@ class ConfigGenerator { }); } + if (this.webpackConfig.loaders.size > 0) { + this.webpackConfig.loaders.forEach((loader) => { + rules.push(loader); + }); + } + return rules; } diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 7942b247..802c2394 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -276,4 +276,22 @@ describe('WebpackConfig object', () => { }).to.throw('Invalid option "fake_option" passed to enableSassLoader()'); }); }); + + describe('addLoader', () => { + it('Adds a new loader with default options', () => { + const config = createConfig(); + + config.addLoader(/\.custom$/, 'custom-loader'); + + expect(Array.from(config.loaders)).to.deep.equals([{ 'test': /\.custom$/, 'use': 'custom-loader', 'include': null, 'exclude': null }]); + }); + + it('Adds a custom exclude path', () => { + const config = createConfig(); + + config.addLoader(/\.custom$/, 'custom-loader', { 'exclude': 'node_modules' }); + + expect(Array.from(config.loaders)).to.deep.equals([{ 'test': /\.custom$/, 'use': 'custom-loader', 'include': null, 'exclude': 'node_modules' }]); + }); + }); }); diff --git a/test/config-generator.js b/test/config-generator.js index bb0f6483..2ad3a897 100644 --- a/test/config-generator.js +++ b/test/config-generator.js @@ -348,6 +348,30 @@ describe('The config-generator function', () => { }); }); + describe('addLoader() adds custom rules', () => { + it('addLoader()', () => { + const config = createConfig(); + config.outputPath = '/tmp/output/public-path'; + config.publicPath = '/public-path'; + config.addLoader(/\.custom$/, 'custom-loader'); + + const actualConfig = configGenerator(config); + + expect(actualConfig.module.rules).to.deep.include({ 'test': /\.custom$/, 'use': 'custom-loader', 'include': null, 'exclude': null }); + }); + + it('addLoader() with custom exlude path', () => { + const config = createConfig(); + config.outputPath = '/tmp/output/public-path'; + config.publicPath = '/public-path'; + config.addLoader(/\.custom$/, 'custom-loader', { 'exclude': 'node_modules' }); + + const actualConfig = configGenerator(config); + + expect(actualConfig.module.rules).to.deep.include({ 'test': /\.custom$/, 'use': 'custom-loader', 'include': null, 'exclude': 'node_modules' }); + }); + }); + describe('.js rule receives different configuration', () => { it('Use default config', () => { const config = createConfig(); From e078772d59cb1995dcb0acc3d7af6f7c7395b153 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Thu, 15 Jun 2017 08:25:51 +0200 Subject: [PATCH 2/5] Update addLoader to take a raw loader config object --- index.js | 9 +++++++++ lib/WebpackConfig.js | 6 +++--- lib/config-generator.js | 8 +++----- test/WebpackConfig.js | 12 ++---------- test/config-generator.js | 17 +++-------------- 5 files changed, 20 insertions(+), 32 deletions(-) diff --git a/index.js b/index.js index d85876bd..5c374a0f 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,12 @@ +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + 'use strict'; const WebpackConfig = require('./lib/WebpackConfig'); diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index 991b5d67..f394a64d 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -49,7 +49,7 @@ class WebpackConfig { this.providedVariables = {}; this.babelConfigurationCallback = function() {}; this.useReact = false; - this.loaders = new Set(); + this.loaders = []; } getContext() { @@ -158,8 +158,8 @@ class WebpackConfig { this.styleEntries.set(name, src); } - addLoader(test, use, options = { include: null, exclude: null }) { - this.loaders.add({ 'test': test, 'use': use, 'include': options.include || null, 'exclude': options.exclude || null }); + addLoader(loader) { + this.loaders.push(loader); } enableVersioning(enabled = true) { diff --git a/lib/config-generator.js b/lib/config-generator.js index 94302a90..beebc093 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -235,11 +235,9 @@ class ConfigGenerator { }); } - if (this.webpackConfig.loaders.size > 0) { - this.webpackConfig.loaders.forEach((loader) => { - rules.push(loader); - }); - } + this.webpackConfig.loaders.forEach((loader) => { + rules.push(loader); + }); return rules; } diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 802c2394..d5a098f8 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -281,17 +281,9 @@ describe('WebpackConfig object', () => { it('Adds a new loader with default options', () => { const config = createConfig(); - config.addLoader(/\.custom$/, 'custom-loader'); + config.addLoader({ 'test': /\.custom$/, 'loader': 'custom-loader' }); - expect(Array.from(config.loaders)).to.deep.equals([{ 'test': /\.custom$/, 'use': 'custom-loader', 'include': null, 'exclude': null }]); - }); - - it('Adds a custom exclude path', () => { - const config = createConfig(); - - config.addLoader(/\.custom$/, 'custom-loader', { 'exclude': 'node_modules' }); - - expect(Array.from(config.loaders)).to.deep.equals([{ 'test': /\.custom$/, 'use': 'custom-loader', 'include': null, 'exclude': 'node_modules' }]); + expect(Array.from(config.loaders)).to.deep.equals([{ 'test': /\.custom$/, 'loader': 'custom-loader' }]); }); }); }); diff --git a/test/config-generator.js b/test/config-generator.js index 2ad3a897..18a99a03 100644 --- a/test/config-generator.js +++ b/test/config-generator.js @@ -348,27 +348,16 @@ describe('The config-generator function', () => { }); }); - describe('addLoader() adds custom rules', () => { + describe('addLoader() adds a custom loader', () => { it('addLoader()', () => { const config = createConfig(); config.outputPath = '/tmp/output/public-path'; config.publicPath = '/public-path'; - config.addLoader(/\.custom$/, 'custom-loader'); + config.addLoader({ 'test': /\.custom$/, 'loader': 'custom-loader' }); const actualConfig = configGenerator(config); - expect(actualConfig.module.rules).to.deep.include({ 'test': /\.custom$/, 'use': 'custom-loader', 'include': null, 'exclude': null }); - }); - - it('addLoader() with custom exlude path', () => { - const config = createConfig(); - config.outputPath = '/tmp/output/public-path'; - config.publicPath = '/public-path'; - config.addLoader(/\.custom$/, 'custom-loader', { 'exclude': 'node_modules' }); - - const actualConfig = configGenerator(config); - - expect(actualConfig.module.rules).to.deep.include({ 'test': /\.custom$/, 'use': 'custom-loader', 'include': null, 'exclude': 'node_modules' }); + expect(actualConfig.module.rules).to.deep.include({ 'test': /\.custom$/, 'loader': 'custom-loader' }); }); }); From 0ea9d74913d96217cf549666e8efdd46e5edc586 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Thu, 15 Jun 2017 08:27:49 +0200 Subject: [PATCH 3/5] Update test description --- test/WebpackConfig.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index d5a098f8..5c7c4521 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -278,7 +278,7 @@ describe('WebpackConfig object', () => { }); describe('addLoader', () => { - it('Adds a new loader with default options', () => { + it('Adds a new loader', () => { const config = createConfig(); config.addLoader({ 'test': /\.custom$/, 'loader': 'custom-loader' }); From 539fd1125a88985a1687135a5b2429914f0f7c90 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Thu, 15 Jun 2017 10:06:52 +0200 Subject: [PATCH 4/5] Add addLoader and addRule to public api --- index.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/index.js b/index.js index 5c374a0f..e48d39e3 100644 --- a/index.js +++ b/index.js @@ -135,6 +135,32 @@ module.exports = { return this; }, + /** + * Adds a custom loader config + * + * @param {object} loader The loader config object + * + * @returns {exports} + */ + addLoader(loader) { + webpackConfig.addLoader(loader); + + return this; + }, + + /** + * Alias to addLoader + * + * @param {object} rule + * + * @returns {exports} + */ + addRule(rule) { + this.addLoader(rule); + + return this; + }, + /** * When enabled, files are rendered with a hash based * on their contents (e.g. main.a2b61cc.js) From d6a1bc021e169ea8b463cdd8d602d56327b3465a Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Thu, 15 Jun 2017 10:07:29 +0200 Subject: [PATCH 5/5] Remove Array.from call --- test/WebpackConfig.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 5c7c4521..10294a08 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -283,7 +283,7 @@ describe('WebpackConfig object', () => { config.addLoader({ 'test': /\.custom$/, 'loader': 'custom-loader' }); - expect(Array.from(config.loaders)).to.deep.equals([{ 'test': /\.custom$/, 'loader': 'custom-loader' }]); + expect(config.loaders).to.deep.equals([{ 'test': /\.custom$/, 'loader': 'custom-loader' }]); }); }); });