diff --git a/index.js b/index.js index e48d39e3..2d1bbd56 100644 --- a/index.js +++ b/index.js @@ -135,6 +135,21 @@ module.exports = { return this; }, + /** + * Add a plugin to the sets of plugins already registered by Encore + * + * For example, if you want to add the "webpack.IgnorePlugin()", then: + * .addPlugin(new webpack.IgnorePlugin(requestRegExp, contextRegExp)) + * + * @param {string} plugin + * @return {exports} + */ + addPlugin(plugin) { + webpackConfig.addPlugin(plugin); + + return this; + }, + /** * Adds a custom loader config * diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index 33dd8bbb..5d4c910c 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -36,6 +36,7 @@ class WebpackConfig { this.manifestKeyPrefix = null; this.entries = new Map(); this.styleEntries = new Map(); + this.plugins = []; this.useVersioning = false; this.useSourceMaps = false; this.usePostCssLoader = false; @@ -158,6 +159,10 @@ class WebpackConfig { this.styleEntries.set(name, src); } + addPlugin(plugin) { + this.plugins.push(plugin); + } + addLoader(loader) { this.loaders.push(loader); } diff --git a/lib/config-generator.js b/lib/config-generator.js index 1a2c5d50..8467c796 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -411,6 +411,10 @@ class ConfigGenerator { plugins.push(new AssetOutputDisplayPlugin(outputPath, friendlyErrorsPlugin)); } + this.webpackConfig.plugins.forEach(function(plugin) { + plugins.push(plugin); + }); + return plugins; } diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 10294a08..79674718 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -14,6 +14,7 @@ const WebpackConfig = require('../lib/WebpackConfig'); const RuntimeConfig = require('../lib/config/RuntimeConfig'); const path = require('path'); const fs = require('fs'); +const webpack = require('webpack'); function createConfig() { const runtimeConfig = new RuntimeConfig(); @@ -277,6 +278,19 @@ describe('WebpackConfig object', () => { }); }); + describe('addPlugin', () => { + it('extends the current registered plugins', () => { + const config = createConfig(); + const nbOfPlugins = config.plugins.length; + + expect(nbOfPlugins).to.equal(0); + + config.addPlugin(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)); + + expect(config.plugins.length).to.equal(1); + }); + }); + describe('addLoader', () => { it('Adds a new loader', () => { const config = createConfig(); diff --git a/test/config-generator.js b/test/config-generator.js index 18a99a03..b8fbbcfe 100644 --- a/test/config-generator.js +++ b/test/config-generator.js @@ -500,4 +500,18 @@ describe('The config-generator function', () => { expect(actualConfig.devServer.publicPath).to.equal('/subdirectory/build/'); }); }); + + describe('test for addPlugin config', () => { + it('extra plugin is set correctly', () => { + const config = createConfig(); + config.outputPath = '/tmp/public/build'; + config.setPublicPath('/build/'); + config.addPlugin(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)); + + const actualConfig = configGenerator(config); + + const ignorePlugin = findPlugin(webpack.IgnorePlugin, actualConfig.plugins); + expect(ignorePlugin).to.not.be.undefined; + }); + }); });