Skip to content

Commit 5c2157e

Browse files
committed
feature #19 Allow to add extra plugin (alOneh)
This PR was squashed before being merged into the master branch (closes #19). Discussion ---------- Allow to add extra plugin Encore does not support adding an extra plugin to the list of registered plugins. In Symfony demo, we try to reduce the size of one of our entries and have to manually add the plugin to the config by doing this : ```js var config = Encore.getWebpackConfig(); config.plugins.push(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)); ``` I propose to update the API of Encore through a new method `addPlugin(plugin)`. ```js Encore // ... .addPlugin(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)) ``` Commits ------- dbe7ea2 Merge branch 'master' into add-plugins 225a0da Allow to add extra plugin
2 parents 83341db + dbe7ea2 commit 5c2157e

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
@@ -135,6 +135,21 @@ module.exports = {
135135
return this;
136136
},
137137

138+
/**
139+
* Add a plugin to the sets of plugins already registered by Encore
140+
*
141+
* For example, if you want to add the "webpack.IgnorePlugin()", then:
142+
* .addPlugin(new webpack.IgnorePlugin(requestRegExp, contextRegExp))
143+
*
144+
* @param {string} plugin
145+
* @return {exports}
146+
*/
147+
addPlugin(plugin) {
148+
webpackConfig.addPlugin(plugin);
149+
150+
return this;
151+
},
152+
138153
/**
139154
* Adds a custom loader config
140155
*

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;
@@ -158,6 +159,10 @@ class WebpackConfig {
158159
this.styleEntries.set(name, src);
159160
}
160161

162+
addPlugin(plugin) {
163+
this.plugins.push(plugin);
164+
}
165+
161166
addLoader(loader) {
162167
this.loaders.push(loader);
163168
}

lib/config-generator.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,10 @@ class ConfigGenerator {
411411
plugins.push(new AssetOutputDisplayPlugin(outputPath, friendlyErrorsPlugin));
412412
}
413413

414+
this.webpackConfig.plugins.forEach(function(plugin) {
415+
plugins.push(plugin);
416+
});
417+
414418
return plugins;
415419
}
416420

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();
@@ -277,6 +278,19 @@ describe('WebpackConfig object', () => {
277278
});
278279
});
279280

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+
});
293+
280294
describe('addLoader', () => {
281295
it('Adds a new loader', () => {
282296
const config = createConfig();

test/config-generator.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,4 +500,18 @@ describe('The config-generator function', () => {
500500
expect(actualConfig.devServer.publicPath).to.equal('/subdirectory/build/');
501501
});
502502
});
503+
504+
describe('test for addPlugin config', () => {
505+
it('extra plugin is set correctly', () => {
506+
const config = createConfig();
507+
config.outputPath = '/tmp/public/build';
508+
config.setPublicPath('/build/');
509+
config.addPlugin(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/));
510+
511+
const actualConfig = configGenerator(config);
512+
513+
const ignorePlugin = findPlugin(webpack.IgnorePlugin, actualConfig.plugins);
514+
expect(ignorePlugin).to.not.be.undefined;
515+
});
516+
});
503517
});

0 commit comments

Comments
 (0)