Skip to content

Commit 225a0da

Browse files
committed
Allow to add extra plugin
1 parent 3cb1de9 commit 225a0da

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,21 @@ module.exports = {
126126
return this;
127127
},
128128

129+
/**
130+
* Add a plugin to the sets of plugins already registered by Encore
131+
*
132+
* For example, if you want to add the "webpack.IgnorePlugin()", then:
133+
* .addPlugin(new webpack.IgnorePlugin(requestRegExp, contextRegExp))
134+
*
135+
* @param {string} plugin
136+
* @return {exports}
137+
*/
138+
addPlugin(plugin) {
139+
webpackConfig.addPlugin(plugin);
140+
141+
return this;
142+
},
143+
129144
/**
130145
* When enabled, files are rendered with a hash based
131146
* on their contents (e.g. main.a2b61cc.js)

lib/WebpackConfig.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class WebpackConfig {
3636
this.manifestKeyPrefix = null;
3737
this.entries = new Map();
3838
this.styleEntries = new Map();
39+
this.plugins = [];
3940
this.useVersioning = false;
4041
this.useSourceMaps = false;
4142
this.usePostCssLoader = false;
@@ -157,6 +158,10 @@ class WebpackConfig {
157158
this.styleEntries.set(name, src);
158159
}
159160

161+
addPlugin(plugin) {
162+
this.plugins.push(plugin);
163+
}
164+
160165
enableVersioning(enabled = true) {
161166
this.useVersioning = enabled;
162167
}

lib/config-generator.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,10 @@ class ConfigGenerator {
417417
plugins.push(new webpack.HotModuleReplacementPlugin());
418418
}
419419

420+
this.webpackConfig.plugins.forEach(function(plugin) {
421+
plugins.push(plugin);
422+
});
423+
420424
return plugins;
421425
}
422426

test/WebpackConfig.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const WebpackConfig = require('../lib/WebpackConfig');
1414
const RuntimeConfig = require('../lib/config/RuntimeConfig');
1515
const path = require('path');
1616
const fs = require('fs');
17+
const webpack = require('webpack');
1718

1819
function createConfig() {
1920
const runtimeConfig = new RuntimeConfig();
@@ -276,4 +277,17 @@ describe('WebpackConfig object', () => {
276277
}).to.throw('Invalid option "fake_option" passed to enableSassLoader()');
277278
});
278279
});
280+
281+
describe('addPlugin', () => {
282+
it('extends the current registered plugins', () => {
283+
const config = createConfig();
284+
const nbOfPlugins = config.plugins.length;
285+
286+
expect(nbOfPlugins).to.equal(0);
287+
288+
config.addPlugin(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/));
289+
290+
expect(config.plugins.length).to.equal(1);
291+
});
292+
});
279293
});

test/config-generator.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,4 +487,18 @@ describe('The config-generator function', () => {
487487
expect(actualConfig.devServer.publicPath).to.equal('/subdirectory/build/');
488488
});
489489
});
490+
491+
describe('test for addPlugin config', () => {
492+
it('extra plugin is set correctly', () => {
493+
const config = createConfig();
494+
config.outputPath = '/tmp/public/build';
495+
config.setPublicPath('/build/');
496+
config.addPlugin(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/));
497+
498+
const actualConfig = configGenerator(config);
499+
500+
const ignorePlugin = findPlugin(webpack.IgnorePlugin, actualConfig.plugins);
501+
expect(ignorePlugin).to.not.be.undefined;
502+
});
503+
});
490504
});

0 commit comments

Comments
 (0)