diff --git a/index.js b/index.js index d85876bd..e48d39e3 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'); @@ -126,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) diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index 8cafc62b..f394a64d 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 = []; } getContext() { @@ -157,6 +158,10 @@ class WebpackConfig { this.styleEntries.set(name, src); } + addLoader(loader) { + this.loaders.push(loader); + } + enableVersioning(enabled = true) { this.useVersioning = enabled; } diff --git a/lib/config-generator.js b/lib/config-generator.js index 83bdeb71..beebc093 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -235,6 +235,10 @@ class ConfigGenerator { }); } + this.webpackConfig.loaders.forEach((loader) => { + rules.push(loader); + }); + return rules; } diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 7942b247..10294a08 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -276,4 +276,14 @@ describe('WebpackConfig object', () => { }).to.throw('Invalid option "fake_option" passed to enableSassLoader()'); }); }); + + describe('addLoader', () => { + it('Adds a new loader', () => { + const config = createConfig(); + + config.addLoader({ 'test': /\.custom$/, 'loader': 'custom-loader' }); + + expect(config.loaders).to.deep.equals([{ 'test': /\.custom$/, 'loader': 'custom-loader' }]); + }); + }); }); diff --git a/test/config-generator.js b/test/config-generator.js index bb0f6483..18a99a03 100644 --- a/test/config-generator.js +++ b/test/config-generator.js @@ -348,6 +348,19 @@ describe('The config-generator function', () => { }); }); + describe('addLoader() adds a custom loader', () => { + it('addLoader()', () => { + const config = createConfig(); + config.outputPath = '/tmp/output/public-path'; + config.publicPath = '/public-path'; + config.addLoader({ 'test': /\.custom$/, 'loader': 'custom-loader' }); + + const actualConfig = configGenerator(config); + + expect(actualConfig.module.rules).to.deep.include({ 'test': /\.custom$/, 'loader': 'custom-loader' }); + }); + }); + describe('.js rule receives different configuration', () => { it('Use default config', () => { const config = createConfig();