Skip to content

Commit 7f1fcbd

Browse files
committed
Add various methods to configure default plugins
1 parent d23aa92 commit 7f1fcbd

17 files changed

+752
-50
lines changed

index.js

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,90 @@ const publicApi = {
102102
return this;
103103
},
104104

105+
/**
106+
* Allows you to configure the options passed to the DefinePlugin
107+
* A list of available options can be found at https://webpack.js.org/plugins/define-plugin/
108+
*
109+
* For example:
110+
*
111+
* Encore.configureDefinePlugin((options) => {
112+
* options.VERSION = JSON.stringify('1.0.0');
113+
* })
114+
*
115+
* @param {function} definePluginOptionsCallback
116+
* @returns {exports}
117+
*/
118+
configureDefinePlugin(definePluginOptionsCallback = () => {}) {
119+
webpackConfig.configureDefinePlugin(definePluginOptionsCallback);
120+
121+
return this;
122+
},
123+
124+
/**
125+
* Allows you to configure the options passed to the extract-text-webpack-plugin.
126+
* A list of available options can be found at https://github.com/webpack-contrib/extract-text-webpack-plugin
127+
*
128+
* For example:
129+
*
130+
* Encore.configureExtractTextPlugin((options) => {
131+
* options.ignoreOrder = true;
132+
* })
133+
*
134+
* @param {function} extractTextPluginOptionsCallback
135+
* @returns {exports}
136+
*/
137+
configureExtractTextPlugin(extractTextPluginOptionsCallback = () => {}) {
138+
webpackConfig.configureExtractTextPlugin(extractTextPluginOptionsCallback);
139+
140+
return this;
141+
},
142+
143+
/**
144+
* Allows you to configure the options passed to the friendly-errors-webpack-plugin.
145+
* A list of available options can be found at https://github.com/geowarin/friendly-errors-webpack-plugin
146+
*
147+
* For example:
148+
*
149+
* Encore.configureFriendlyErrorsPlugin((options) => {
150+
* options.clearConsole = true;
151+
* })
152+
*
153+
* @param {function} friendlyErrorsPluginOptionsCallback
154+
* @returns {exports}
155+
*/
156+
configureFriendlyErrorsPlugin(friendlyErrorsPluginOptionsCallback = () => {}) {
157+
webpackConfig.configureFriendlyErrorsPlugin(friendlyErrorsPluginOptionsCallback);
158+
159+
return this;
160+
},
161+
162+
/**
163+
* Allows you to configure the options passed to the LoaderOptionsPlugins
164+
* A list of available options can be found at https://webpack.js.org/plugins/loader-options-plugin/
165+
*
166+
* For example:
167+
*
168+
* Encore.configureLoaderOptionsPlugin((options) => {
169+
* options.minimize = true;
170+
* })
171+
*
172+
* @param {function} loaderOptionsPluginOptionsCallback
173+
* @returns {exports}
174+
*/
175+
configureLoaderOptionsPlugin(loaderOptionsPluginOptionsCallback = () => {}) {
176+
webpackConfig.configureLoaderOptionsPlugin(loaderOptionsPluginOptionsCallback);
177+
178+
return this;
179+
},
180+
105181
/**
106182
* Allows you to configure the options passed to webpack-manifest-plugin.
107183
* A list of available options can be found at https://github.com/danethurber/webpack-manifest-plugin
108184
*
109185
* For example:
110186
*
111-
* Encore.configureManifestPlugin(function(options){
112-
* options.fileName: '../../var/assets/manifest.json'
187+
* Encore.configureManifestPlugin((options) => {
188+
* options.fileName = '../../var/assets/manifest.json';
113189
* })
114190
*
115191
* @param {function} manifestPluginOptionsCallback
@@ -121,6 +197,26 @@ const publicApi = {
121197
return this;
122198
},
123199

200+
/**
201+
* Allows you to configure the options passed to the uglifyjs-webpack-plugin
202+
* A list of available options can be found at https://github.com/webpack-contrib/uglifyjs-webpack-plugin/tree/v0.4.6
203+
*
204+
* For example:
205+
*
206+
* Encore.configureUglifyJsPlugin((options) => {
207+
* options.compress = false;
208+
* options.beautify = true;
209+
* })
210+
*
211+
* @param {function} uglifyJsPluginOptionsCallback
212+
* @returns {exports}
213+
*/
214+
configureUglifyJsPlugin(uglifyJsPluginOptionsCallback = () => {}) {
215+
webpackConfig.configureUglifyJsPlugin(uglifyJsPluginOptionsCallback);
216+
217+
return this;
218+
},
219+
124220
/**
125221
* Adds a JavaScript file that should be webpacked:
126222
*

lib/WebpackConfig.js

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,56 @@ function validateRuntimeConfig(runtimeConfig) {
3030
class WebpackConfig {
3131
constructor(runtimeConfig) {
3232
validateRuntimeConfig(runtimeConfig);
33+
3334
this.runtimeConfig = runtimeConfig;
34-
this.outputPath = null;
35-
this.publicPath = null;
36-
this.manifestKeyPrefix = null;
3735
this.entries = new Map();
3836
this.styleEntries = new Map();
3937
this.plugins = [];
38+
this.loaders = [];
39+
40+
// Global settings
41+
this.outputPath = null;
42+
this.publicPath = null;
43+
this.manifestKeyPrefix = null;
44+
this.sharedCommonsEntryName = null;
45+
this.providedVariables = {};
46+
this.configuredFilenames = {};
47+
48+
// Features/Loaders flags
4049
this.useVersioning = false;
4150
this.useSourceMaps = false;
51+
this.cleanupOutput = false;
52+
this.useImagesLoader = true;
53+
this.useFontsLoader = true;
4254
this.usePostCssLoader = false;
43-
this.postCssLoaderOptionsCallback = function() {};
44-
this.useSassLoader = false;
45-
this.sassLoaderOptionsCallback = function() {};
46-
this.sassOptions = {
47-
resolve_url_loader: true
48-
};
4955
this.useLessLoader = false;
50-
this.lessLoaderOptionsCallback = function() {};
51-
this.cleanupOutput = false;
52-
this.sharedCommonsEntryName = null;
53-
this.providedVariables = {};
54-
this.babelConfigurationCallback = function() {};
56+
this.useSassLoader = false;
5557
this.useReact = false;
5658
this.useVueLoader = false;
57-
this.vueLoaderOptionsCallback = () => {};
58-
this.loaders = [];
5959
this.useTypeScriptLoader = false;
60-
this.tsConfigurationCallback = function() {};
6160
this.useForkedTypeScriptTypeChecking = false;
61+
62+
// Features/Loaders options
63+
this.sassOptions = {
64+
resolve_url_loader: true
65+
};
66+
67+
// Features/Loaders options callbacks
68+
this.postCssLoaderOptionsCallback = () => {};
69+
this.sassLoaderOptionsCallback = () => {};
70+
this.lessLoaderOptionsCallback = () => {};
71+
this.babelConfigurationCallback = () => {};
72+
this.vueLoaderOptionsCallback = () => {};
73+
this.tsConfigurationCallback = () => {};
74+
75+
// Plugins callbacks
76+
this.definePluginOptionsCallback = () => {};
77+
this.extractTextPluginOptionsCallback = () => {};
6278
this.forkedTypeScriptTypesCheckOptionsCallback = () => {};
63-
this.useImagesLoader = true;
64-
this.useFontsLoader = true;
65-
this.configuredFilenames = {};
66-
this.manifestPluginOptionsCallback = function() {};
79+
this.friendlyErrorsPluginOptionsCallback = () => {};
80+
this.loaderOptionsPluginOptionsCallback = () => {};
81+
this.manifestPluginOptionsCallback = () => {};
82+
this.uglifyJsPluginOptionsCallback = () => {};
6783
}
6884

6985
getContext() {
@@ -118,6 +134,38 @@ class WebpackConfig {
118134
this.manifestKeyPrefix = manifestKeyPrefix;
119135
}
120136

137+
configureDefinePlugin(definePluginOptionsCallback = () => {}) {
138+
if (typeof definePluginOptionsCallback !== 'function') {
139+
throw new Error('Argument 1 to configureDefinePlugin() must be a callback function');
140+
}
141+
142+
this.definePluginOptionsCallback = definePluginOptionsCallback;
143+
}
144+
145+
configureExtractTextPlugin(extractTextPluginOptionsCallback = () => {}) {
146+
if (typeof extractTextPluginOptionsCallback !== 'function') {
147+
throw new Error('Argument 1 to configureExtractTextPlugin() must be a callback function');
148+
}
149+
150+
this.extractTextPluginOptionsCallback = extractTextPluginOptionsCallback;
151+
}
152+
153+
configureFriendlyErrorsPlugin(friendlyErrorsPluginOptionsCallback = () => {}) {
154+
if (typeof friendlyErrorsPluginOptionsCallback !== 'function') {
155+
throw new Error('Argument 1 to configureFriendlyErrorsPlugin() must be a callback function');
156+
}
157+
158+
this.friendlyErrorsPluginOptionsCallback = friendlyErrorsPluginOptionsCallback;
159+
}
160+
161+
configureLoaderOptionsPlugin(loaderOptionsPluginOptionsCallback = () => {}) {
162+
if (typeof loaderOptionsPluginOptionsCallback !== 'function') {
163+
throw new Error('Argument 1 to configureLoaderOptionsPlugin() must be a callback function');
164+
}
165+
166+
this.loaderOptionsPluginOptionsCallback = loaderOptionsPluginOptionsCallback;
167+
}
168+
121169
configureManifestPlugin(manifestPluginOptionsCallback = () => {}) {
122170
if (typeof manifestPluginOptionsCallback !== 'function') {
123171
throw new Error('Argument 1 to configureManifestPlugin() must be a callback function');
@@ -126,6 +174,14 @@ class WebpackConfig {
126174
this.manifestPluginOptionsCallback = manifestPluginOptionsCallback;
127175
}
128176

177+
configureUglifyJsPlugin(uglifyJsPluginOptionsCallback = () => {}) {
178+
if (typeof uglifyJsPluginOptionsCallback !== 'function') {
179+
throw new Error('Argument 1 to configureUglifyJsPlugin() must be a callback function');
180+
}
181+
182+
this.uglifyJsPluginOptionsCallback = uglifyJsPluginOptionsCallback;
183+
}
184+
129185
/**
130186
* Returns the value that should be used as the publicPath,
131187
* which can be overridden by enabling the webpackDevServer

lib/config-generator.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const loaderOptionsPluginUtil = require('./plugins/loader-options');
2626
const versioningPluginUtil = require('./plugins/versioning');
2727
const variableProviderPluginUtil = require('./plugins/variable-provider');
2828
const cleanPluginUtil = require('./plugins/clean');
29-
const commonChunksPluginUtil = require('./plugins/common-chunks');
29+
const commonsChunksPluginUtil = require('./plugins/commons-chunks');
3030
const definePluginUtil = require('./plugins/define');
3131
const uglifyPluginUtil = require('./plugins/uglify');
3232
const friendlyErrorPluginUtil = require('./plugins/friendly-errors');
@@ -220,13 +220,13 @@ class ConfigGenerator {
220220

221221
cleanPluginUtil(plugins, this.webpackConfig, ['**/*']);
222222

223-
commonChunksPluginUtil(plugins, this.webpackConfig);
223+
commonsChunksPluginUtil(plugins, this.webpackConfig);
224224

225-
// todo - options here should be configurable
226225
definePluginUtil(plugins, this.webpackConfig);
226+
227227
uglifyPluginUtil(plugins, this.webpackConfig);
228228

229-
let friendlyErrorPlugin = friendlyErrorPluginUtil();
229+
const friendlyErrorPlugin = friendlyErrorPluginUtil(this.webpackConfig);
230230
plugins.push(friendlyErrorPlugin);
231231

232232
assetOutputDisplay(plugins, this.webpackConfig, friendlyErrorPlugin);
File renamed without changes.

lib/plugins/define.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@ const webpack = require('webpack');
1414
/**
1515
* @param {Array} plugins
1616
* @param {WebpackConfig} webpackConfig
17-
* @param {Object} defineOptions
1817
* @return {void}
1918
*/
20-
module.exports = function(plugins, webpackConfig, defineOptions = {}) {
19+
module.exports = function(plugins, webpackConfig) {
2120

2221
if (!webpackConfig.isProduction()) {
2322
return;
2423
}
2524

26-
let defineConfig = Object.assign({}, defineOptions, {
25+
const definePluginOptions = {
2726
'process.env': {
2827
NODE_ENV: '"production"'
2928
}
30-
});
31-
let define = new webpack.DefinePlugin(defineConfig);
29+
};
3230

33-
plugins.push(define);
31+
webpackConfig.definePluginOptionsCallback.apply(
32+
definePluginOptions,
33+
[definePluginOptions]
34+
);
35+
36+
plugins.push(new webpack.DefinePlugin(definePluginOptions));
3437
};

lib/plugins/extract-text.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ const ExtractTextPlugin = require('extract-text-webpack-plugin');
1414
/**
1515
* @param {Array} plugins
1616
* @param {WebpackConfig} webpackConfig
17-
* @param {Object} extractTextOptions Options to pass to the plugin
1817
* @return {void}
1918
*/
20-
module.exports = function(plugins, webpackConfig, extractTextOptions = {}) {
19+
module.exports = function(plugins, webpackConfig) {
2120

2221
/*
2322
* All CSS/SCSS content (due to the loaders above) will be
@@ -37,13 +36,18 @@ module.exports = function(plugins, webpackConfig, extractTextOptions = {}) {
3736
filename = webpackConfig.configuredFilenames.css;
3837
}
3938

40-
let config = Object.assign({}, extractTextOptions, {
39+
const extractTextPluginOptions = {
4140
filename: filename,
4241
// if true, async CSS (e.g. loaded via require.ensure())
4342
// is extracted to the entry point CSS. If false, it's
4443
// inlined in the AJAX-loaded .js file.
4544
allChunks: false
46-
});
45+
};
4746

48-
plugins.push(new ExtractTextPlugin(config));
47+
webpackConfig.extractTextPluginOptionsCallback.apply(
48+
extractTextPluginOptions,
49+
[extractTextPluginOptions]
50+
);
51+
52+
plugins.push(new ExtractTextPlugin(extractTextPluginOptions));
4953
};

lib/plugins/friendly-errors.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ const vueUnactivatedLoaderTransformer = require('../friendly-errors/transformers
1818
const vueUnactivatedLoaderFormatter = require('../friendly-errors/formatters/vue-unactivated-loader-error');
1919

2020
/**
21+
* @param {WebpackConfig} webpackConfig
2122
* @return {FriendlyErrorsWebpackPlugin}
2223
*/
23-
module.exports = function() {
24-
return new FriendlyErrorsWebpackPlugin({
24+
module.exports = function(webpackConfig) {
25+
const friendlyErrorsPluginOptions = {
2526
clearConsole: false,
2627
additionalTransformers: [
2728
missingLoaderTransformer,
@@ -36,5 +37,12 @@ module.exports = function() {
3637
compilationSuccessInfo: {
3738
messages: []
3839
}
39-
});
40+
};
41+
42+
webpackConfig.friendlyErrorsPluginOptionsCallback.apply(
43+
friendlyErrorsPluginOptions,
44+
[friendlyErrorsPluginOptions]
45+
);
46+
47+
return new FriendlyErrorsWebpackPlugin(friendlyErrorsPluginOptions);
4048
};

lib/plugins/loader-options.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const webpack = require('webpack');
1717
* @return {void}
1818
*/
1919
module.exports = function(plugins, webpackConfig) {
20-
2120
/*
2221
* This section is a bit mysterious. The "minimize"
2322
* true is read and used to minify the CSS.
@@ -27,11 +26,18 @@ module.exports = function(plugins, webpackConfig) {
2726
* not totally sure what's going on here
2827
* https://github.com/jtangelder/sass-loader/issues/285
2928
*/
30-
plugins.push(new webpack.LoaderOptionsPlugin({
29+
const loaderOptionsPluginOptions = {
3130
debug: !webpackConfig.isProduction(),
3231
options: {
3332
context: webpackConfig.getContext(),
3433
output: { path: webpackConfig.outputPath }
3534
}
36-
}));
35+
};
36+
37+
webpackConfig.loaderOptionsPluginOptionsCallback.apply(
38+
loaderOptionsPluginOptions,
39+
[loaderOptionsPluginOptions]
40+
);
41+
42+
plugins.push(new webpack.LoaderOptionsPlugin(loaderOptionsPluginOptions));
3743
};

0 commit comments

Comments
 (0)