From 3dd69f533e15c0a814db003acd0824c6d05d869a Mon Sep 17 00:00:00 2001 From: Bruno BOUTAREL Date: Thu, 17 Aug 2017 15:11:59 +0200 Subject: [PATCH 1/8] Add configureManifestPlugin method --- index.js | 37 +++++++++++++++++++++++++++++++------ lib/WebpackConfig.js | 9 +++++++++ lib/config-generator.js | 2 +- lib/plugins/manifest.js | 10 +++++++--- test/WebpackConfig.js | 20 ++++++++++++++++++++ 5 files changed, 68 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 3177cd2c..b67a1e8a 100644 --- a/index.js +++ b/index.js @@ -102,6 +102,25 @@ const publicApi = { return this; }, + /** + * Allows to set ManifestPlugin options and override default options + * List of available options can be found at https://github.com/danethurber/webpack-manifest-plugin + * + * For example: + * + * .configureManifestPlugin({ + * fileName: '../../var/assets/manifest.json' + * }) + * + * @param {Object} options + * @returns {publicApi} + */ + configureManifestPlugin(options) { + webpackConfig.configureManifestPlugin(options); + + return this; + }, + /** * Adds a JavaScript file that should be webpacked: * @@ -294,7 +313,8 @@ const publicApi = { * @param {function} postCssLoaderOptionsCallback * @return {exports} */ - enablePostCssLoader(postCssLoaderOptionsCallback = () => {}) { + enablePostCssLoader(postCssLoaderOptionsCallback = () => { + }) { webpackConfig.enablePostCssLoader(postCssLoaderOptionsCallback); return this; @@ -328,7 +348,8 @@ const publicApi = { * @param {object} encoreOptions * @return {exports} */ - enableSassLoader(sassLoaderOptionsCallback = () => {}, encoreOptions = {}) { + enableSassLoader(sassLoaderOptionsCallback = () => { + }, encoreOptions = {}) { webpackConfig.enableSassLoader(sassLoaderOptionsCallback, encoreOptions); return this; @@ -350,7 +371,8 @@ const publicApi = { * @param {function} lessLoaderOptionsCallback * @return {exports} */ - enableLessLoader(lessLoaderOptionsCallback = () => {}) { + enableLessLoader(lessLoaderOptionsCallback = () => { + }) { webpackConfig.enableLessLoader(lessLoaderOptionsCallback); return this; @@ -402,7 +424,8 @@ const publicApi = { * @param {function} callback * @return {exports} */ - enableTypeScriptLoader(callback = () => {}) { + enableTypeScriptLoader(callback = () => { + }) { webpackConfig.enableTypeScriptLoader(callback); return this; @@ -417,7 +440,8 @@ const publicApi = { * @param {function} forkedTypeScriptTypesCheckOptionsCallback * @return {exports} */ - enableForkedTypeScriptTypesChecking(forkedTypeScriptTypesCheckOptionsCallback = () => {}) { + enableForkedTypeScriptTypesChecking(forkedTypeScriptTypesCheckOptionsCallback = () => { + }) { webpackConfig.enableForkedTypeScriptTypesChecking( forkedTypeScriptTypesCheckOptionsCallback ); @@ -441,7 +465,8 @@ const publicApi = { * @param {function} vueLoaderOptionsCallback * @returns {exports} */ - enableVueLoader(vueLoaderOptionsCallback = () => {}) { + enableVueLoader(vueLoaderOptionsCallback = () => { + }) { webpackConfig.enableVueLoader(vueLoaderOptionsCallback); return this; diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index f1c354dc..59350c31 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -63,6 +63,7 @@ class WebpackConfig { this.useImagesLoader = true; this.useFontsLoader = true; this.configuredFilenames = {}; + this.manifestPluginOptions = {}; } getContext() { @@ -117,6 +118,14 @@ class WebpackConfig { this.manifestKeyPrefix = manifestKeyPrefix; } + configureManifestPlugin(options = {}) { + if (typeof options !== 'object') { + throw new Error('Argument 1 to configureManifestPlugin() must be an object'); + } + + this.manifestPluginOptions = options; + } + /** * Returns the value that should be used as the publicPath, * which can be overridden by enabling the webpackDevServer diff --git a/lib/config-generator.js b/lib/config-generator.js index 1440dfd4..fbe039fa 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -210,7 +210,7 @@ class ConfigGenerator { deleteUnusedEntriesPluginUtil(plugins, this.webpackConfig); // Dump the manifest.json file - manifestPluginUtil(plugins, this.webpackConfig); + manifestPluginUtil(plugins, this.webpackConfig, this.webpackConfig.manifestPluginOptions); loaderOptionsPluginUtil(plugins, this.webpackConfig); diff --git a/lib/plugins/manifest.js b/lib/plugins/manifest.js index a5e76002..d8fec4b3 100644 --- a/lib/plugins/manifest.js +++ b/lib/plugins/manifest.js @@ -14,9 +14,10 @@ const ManifestPlugin = require('../webpack/webpack-manifest-plugin'); /** * @param {Array} plugins * @param {WebpackConfig} webpackConfig + * @param {Object} manifestPluginOptions * @return {void} */ -module.exports = function(plugins, webpackConfig) { +module.exports = function(plugins, webpackConfig, manifestPluginOptions) { let manifestPrefix = webpackConfig.manifestKeyPrefix; if (null === manifestPrefix) { @@ -24,11 +25,14 @@ module.exports = function(plugins, webpackConfig) { manifestPrefix = webpackConfig.publicPath.replace(/^\//, ''); } - plugins.push(new ManifestPlugin({ + let defaultConfig = { basePath: manifestPrefix, // guarantee the value uses the public path (or CDN public path) publicPath: webpackConfig.getRealPublicPath(), // always write a manifest.json file, even with webpack-dev-server writeToFileEmit: true, - })); + }; + let manifestConfig = Object.assign(defaultConfig, manifestPluginOptions); + + plugins.push(new ManifestPlugin(manifestConfig)); }; diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 192c6910..2a6ff9b4 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -163,6 +163,26 @@ describe('WebpackConfig object', () => { }); }); + describe('configureManifestPlugin', () => { + it('Setting custom options', () => { + const config = createConfig(); + config.configureManifestPlugin({ + fileName: '../../var/assets/manifest.json', + }); + + // fileName option overridden + expect(config.manifestOptions.fileName).to.equal('../../var/assets/manifest.json'); + }); + + it('Setting a non object argument', () => { + const config = createConfig(); + + expect(() => { + config.configureManifestPlugin('../../var/assets/manifest.json'); + }).to.throw('Argument 1 to configureManifestPlugin() must be an object'); + }); + }); + describe('addEntry', () => { it('Calling with a duplicate name throws an error', () => { const config = createConfig(); From 69b043981ac8f94bd47b6ff06838276983504150 Mon Sep 17 00:00:00 2001 From: Bruno BOUTAREL Date: Thu, 17 Aug 2017 15:17:29 +0200 Subject: [PATCH 2/8] Fix CS --- index.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index b67a1e8a..4a100b57 100644 --- a/index.js +++ b/index.js @@ -313,8 +313,7 @@ const publicApi = { * @param {function} postCssLoaderOptionsCallback * @return {exports} */ - enablePostCssLoader(postCssLoaderOptionsCallback = () => { - }) { + enablePostCssLoader(postCssLoaderOptionsCallback = () => {}) { webpackConfig.enablePostCssLoader(postCssLoaderOptionsCallback); return this; @@ -348,8 +347,7 @@ const publicApi = { * @param {object} encoreOptions * @return {exports} */ - enableSassLoader(sassLoaderOptionsCallback = () => { - }, encoreOptions = {}) { + enableSassLoader(sassLoaderOptionsCallback = () => {}, encoreOptions = {}) { webpackConfig.enableSassLoader(sassLoaderOptionsCallback, encoreOptions); return this; @@ -371,8 +369,7 @@ const publicApi = { * @param {function} lessLoaderOptionsCallback * @return {exports} */ - enableLessLoader(lessLoaderOptionsCallback = () => { - }) { + enableLessLoader(lessLoaderOptionsCallback = () => {}) { webpackConfig.enableLessLoader(lessLoaderOptionsCallback); return this; @@ -424,8 +421,7 @@ const publicApi = { * @param {function} callback * @return {exports} */ - enableTypeScriptLoader(callback = () => { - }) { + enableTypeScriptLoader(callback = () => {}) { webpackConfig.enableTypeScriptLoader(callback); return this; @@ -440,8 +436,7 @@ const publicApi = { * @param {function} forkedTypeScriptTypesCheckOptionsCallback * @return {exports} */ - enableForkedTypeScriptTypesChecking(forkedTypeScriptTypesCheckOptionsCallback = () => { - }) { + enableForkedTypeScriptTypesChecking(forkedTypeScriptTypesCheckOptionsCallback = () => {}) { webpackConfig.enableForkedTypeScriptTypesChecking( forkedTypeScriptTypesCheckOptionsCallback ); @@ -465,8 +460,7 @@ const publicApi = { * @param {function} vueLoaderOptionsCallback * @returns {exports} */ - enableVueLoader(vueLoaderOptionsCallback = () => { - }) { + enableVueLoader(vueLoaderOptionsCallback = () => {}) { webpackConfig.enableVueLoader(vueLoaderOptionsCallback); return this; From 68de2b5da994d8a9e7ed94c7732c33deb8ad7f61 Mon Sep 17 00:00:00 2001 From: Bruno BOUTAREL Date: Thu, 17 Aug 2017 15:30:32 +0200 Subject: [PATCH 3/8] Remove useless parameter --- lib/config-generator.js | 2 +- lib/plugins/manifest.js | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/config-generator.js b/lib/config-generator.js index fbe039fa..1440dfd4 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -210,7 +210,7 @@ class ConfigGenerator { deleteUnusedEntriesPluginUtil(plugins, this.webpackConfig); // Dump the manifest.json file - manifestPluginUtil(plugins, this.webpackConfig, this.webpackConfig.manifestPluginOptions); + manifestPluginUtil(plugins, this.webpackConfig); loaderOptionsPluginUtil(plugins, this.webpackConfig); diff --git a/lib/plugins/manifest.js b/lib/plugins/manifest.js index d8fec4b3..5fb1a735 100644 --- a/lib/plugins/manifest.js +++ b/lib/plugins/manifest.js @@ -14,10 +14,9 @@ const ManifestPlugin = require('../webpack/webpack-manifest-plugin'); /** * @param {Array} plugins * @param {WebpackConfig} webpackConfig - * @param {Object} manifestPluginOptions * @return {void} */ -module.exports = function(plugins, webpackConfig, manifestPluginOptions) { +module.exports = function(plugins, webpackConfig) { let manifestPrefix = webpackConfig.manifestKeyPrefix; if (null === manifestPrefix) { @@ -32,7 +31,7 @@ module.exports = function(plugins, webpackConfig, manifestPluginOptions) { // always write a manifest.json file, even with webpack-dev-server writeToFileEmit: true, }; - let manifestConfig = Object.assign(defaultConfig, manifestPluginOptions); + let manifestConfig = Object.assign(defaultConfig, webpackConfig.manifestPluginOptions); plugins.push(new ManifestPlugin(manifestConfig)); }; From 3aa7f3830f03d7def458dcb94e64576543af1807 Mon Sep 17 00:00:00 2001 From: Bruno BOUTAREL Date: Thu, 17 Aug 2017 15:38:46 +0200 Subject: [PATCH 4/8] Update variable name in test --- test/WebpackConfig.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 2a6ff9b4..d240815b 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -171,7 +171,7 @@ describe('WebpackConfig object', () => { }); // fileName option overridden - expect(config.manifestOptions.fileName).to.equal('../../var/assets/manifest.json'); + expect(config.manifestPluginOptions.fileName).to.equal('../../var/assets/manifest.json'); }); it('Setting a non object argument', () => { From 01baa4a5c4617c9ed544947c5d900a1b5ac533bd Mon Sep 17 00:00:00 2001 From: Bruno BOUTAREL Date: Thu, 17 Aug 2017 16:00:41 +0200 Subject: [PATCH 5/8] Fix CS and doc --- index.js | 2 +- lib/plugins/manifest.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 4a100b57..bcbb3fec 100644 --- a/index.js +++ b/index.js @@ -113,7 +113,7 @@ const publicApi = { * }) * * @param {Object} options - * @returns {publicApi} + * @returns {exports} */ configureManifestPlugin(options) { webpackConfig.configureManifestPlugin(options); diff --git a/lib/plugins/manifest.js b/lib/plugins/manifest.js index 5fb1a735..9d0cb1d9 100644 --- a/lib/plugins/manifest.js +++ b/lib/plugins/manifest.js @@ -24,14 +24,14 @@ module.exports = function(plugins, webpackConfig) { manifestPrefix = webpackConfig.publicPath.replace(/^\//, ''); } - let defaultConfig = { + const defaultConfig = { basePath: manifestPrefix, // guarantee the value uses the public path (or CDN public path) publicPath: webpackConfig.getRealPublicPath(), // always write a manifest.json file, even with webpack-dev-server writeToFileEmit: true, }; - let manifestConfig = Object.assign(defaultConfig, webpackConfig.manifestPluginOptions); + const manifestConfig = Object.assign({}, defaultConfig, webpackConfig.manifestPluginOptions); plugins.push(new ManifestPlugin(manifestConfig)); }; From 1e2350630313e55d2952331fe66c37a4207e82eb Mon Sep 17 00:00:00 2001 From: Bruno BOUTAREL Date: Thu, 17 Aug 2017 16:27:00 +0200 Subject: [PATCH 6/8] Update to use callback function instead of an array --- index.js | 10 +++++----- lib/WebpackConfig.js | 12 +++++++----- lib/plugins/manifest.js | 10 +++++++--- test/WebpackConfig.js | 15 +++------------ 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/index.js b/index.js index bcbb3fec..5b40fe91 100644 --- a/index.js +++ b/index.js @@ -108,15 +108,15 @@ const publicApi = { * * For example: * - * .configureManifestPlugin({ - * fileName: '../../var/assets/manifest.json' + * Encore.configureManifestPlugin(function(options){ + * options.fileName: '../../var/assets/manifest.json' * }) * - * @param {Object} options + * @param {function} manifestPluginOptionsCallback * @returns {exports} */ - configureManifestPlugin(options) { - webpackConfig.configureManifestPlugin(options); + configureManifestPlugin(manifestPluginOptionsCallback = () => {}) { + webpackConfig.configureManifestPlugin(manifestPluginOptionsCallback); return this; }, diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index 59350c31..a4215eca 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -63,7 +63,7 @@ class WebpackConfig { this.useImagesLoader = true; this.useFontsLoader = true; this.configuredFilenames = {}; - this.manifestPluginOptions = {}; + this.manifestPluginOptionsCallback = function() {}; } getContext() { @@ -118,12 +118,14 @@ class WebpackConfig { this.manifestKeyPrefix = manifestKeyPrefix; } - configureManifestPlugin(options = {}) { - if (typeof options !== 'object') { - throw new Error('Argument 1 to configureManifestPlugin() must be an object'); + configureManifestPlugin(manifestPluginOptionsCallback = () => {}) { + this.usePostCssLoader = true; + + if (typeof manifestPluginOptionsCallback !== 'function') { + throw new Error('Argument 1 to enablePostCssLoader() must be a callback function.'); } - this.manifestPluginOptions = options; + this.manifestPluginOptionsCallback = manifestPluginOptionsCallback; } /** diff --git a/lib/plugins/manifest.js b/lib/plugins/manifest.js index 9d0cb1d9..0df0dc5c 100644 --- a/lib/plugins/manifest.js +++ b/lib/plugins/manifest.js @@ -24,14 +24,18 @@ module.exports = function(plugins, webpackConfig) { manifestPrefix = webpackConfig.publicPath.replace(/^\//, ''); } - const defaultConfig = { + const manifestPluginOptions = { basePath: manifestPrefix, // guarantee the value uses the public path (or CDN public path) publicPath: webpackConfig.getRealPublicPath(), // always write a manifest.json file, even with webpack-dev-server writeToFileEmit: true, }; - const manifestConfig = Object.assign({}, defaultConfig, webpackConfig.manifestPluginOptions); - plugins.push(new ManifestPlugin(manifestConfig)); + webpackConfig.manifestPluginOptionsCallback.apply( + manifestPluginOptions, + [manifestPluginOptions] + ); + + plugins.push(new ManifestPlugin(manifestPluginOptions)); }; diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index d240815b..2385f01f 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -166,20 +166,11 @@ describe('WebpackConfig object', () => { describe('configureManifestPlugin', () => { it('Setting custom options', () => { const config = createConfig(); - config.configureManifestPlugin({ - fileName: '../../var/assets/manifest.json', - }); + const callback = () => {}; + config.configureManifestPlugin(callback); // fileName option overridden - expect(config.manifestPluginOptions.fileName).to.equal('../../var/assets/manifest.json'); - }); - - it('Setting a non object argument', () => { - const config = createConfig(); - - expect(() => { - config.configureManifestPlugin('../../var/assets/manifest.json'); - }).to.throw('Argument 1 to configureManifestPlugin() must be an object'); + expect(config.manifestPluginOptionsCallback).to.equal(callback); }); }); From ba1d55bdc4e95c85d0ea33f770b2fd5bc6c1f7ac Mon Sep 17 00:00:00 2001 From: Bruno BOUTAREL Date: Thu, 17 Aug 2017 16:48:14 +0200 Subject: [PATCH 7/8] Fix copy paste and typos --- lib/WebpackConfig.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index a4215eca..9b399fb6 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -119,10 +119,8 @@ class WebpackConfig { } configureManifestPlugin(manifestPluginOptionsCallback = () => {}) { - this.usePostCssLoader = true; - if (typeof manifestPluginOptionsCallback !== 'function') { - throw new Error('Argument 1 to enablePostCssLoader() must be a callback function.'); + throw new Error('Argument 1 to configureManifestPlugin() must be a callback function'); } this.manifestPluginOptionsCallback = manifestPluginOptionsCallback; From 630c2e36bb1b63b6d6021757354501586520191c Mon Sep 17 00:00:00 2001 From: Bruno BOUTAREL Date: Thu, 17 Aug 2017 16:48:51 +0200 Subject: [PATCH 8/8] Add missing test --- test/WebpackConfig.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 2385f01f..91920a06 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -172,6 +172,15 @@ describe('WebpackConfig object', () => { // fileName option overridden expect(config.manifestPluginOptionsCallback).to.equal(callback); }); + + it('Setting invalid custom options argument', () => { + const config = createConfig(); + const callback = 'invalid'; + + expect(() => { + config.configureManifestPlugin(callback); + }).to.throw('Argument 1 to configureManifestPlugin() must be a callback function'); + }); }); describe('addEntry', () => {