From 225a0dacafcb9a164815332ed27b98956d2fff7c Mon Sep 17 00:00:00 2001 From: Hippolyte Alain Date: Thu, 15 Jun 2017 12:25:54 +0200 Subject: [PATCH] Allow to add extra plugin --- index.js | 15 +++++++++++++++ lib/WebpackConfig.js | 5 +++++ lib/config-generator.js | 4 ++++ test/WebpackConfig.js | 14 ++++++++++++++ test/config-generator.js | 14 ++++++++++++++ 5 files changed, 52 insertions(+) diff --git a/index.js b/index.js index d85876bd..1dd210c4 100644 --- a/index.js +++ b/index.js @@ -126,6 +126,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; + }, + /** * 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 a44b0f4f..0676abc3 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; @@ -157,6 +158,10 @@ class WebpackConfig { this.styleEntries.set(name, src); } + addPlugin(plugin) { + this.plugins.push(plugin); + } + enableVersioning(enabled = true) { this.useVersioning = enabled; } diff --git a/lib/config-generator.js b/lib/config-generator.js index 52aa29e5..7ef6ddd4 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -417,6 +417,10 @@ class ConfigGenerator { plugins.push(new webpack.HotModuleReplacementPlugin()); } + this.webpackConfig.plugins.forEach(function(plugin) { + plugins.push(plugin); + }); + return plugins; } diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 7942b247..96fff928 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(); @@ -276,4 +277,17 @@ describe('WebpackConfig object', () => { }).to.throw('Invalid option "fake_option" passed to enableSassLoader()'); }); }); + + 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); + }); + }); }); diff --git a/test/config-generator.js b/test/config-generator.js index bb0f6483..87a86e76 100644 --- a/test/config-generator.js +++ b/test/config-generator.js @@ -487,4 +487,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; + }); + }); });