diff --git a/index.js b/index.js index 3177cd2c..5b40fe91 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: + * + * Encore.configureManifestPlugin(function(options){ + * options.fileName: '../../var/assets/manifest.json' + * }) + * + * @param {function} manifestPluginOptionsCallback + * @returns {exports} + */ + configureManifestPlugin(manifestPluginOptionsCallback = () => {}) { + webpackConfig.configureManifestPlugin(manifestPluginOptionsCallback); + + return this; + }, + /** * Adds a JavaScript file that should be webpacked: * diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index f1c354dc..9b399fb6 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -63,6 +63,7 @@ class WebpackConfig { this.useImagesLoader = true; this.useFontsLoader = true; this.configuredFilenames = {}; + this.manifestPluginOptionsCallback = function() {}; } getContext() { @@ -117,6 +118,14 @@ class WebpackConfig { this.manifestKeyPrefix = manifestKeyPrefix; } + configureManifestPlugin(manifestPluginOptionsCallback = () => {}) { + if (typeof manifestPluginOptionsCallback !== 'function') { + throw new Error('Argument 1 to configureManifestPlugin() must be a callback function'); + } + + this.manifestPluginOptionsCallback = manifestPluginOptionsCallback; + } + /** * Returns the value that should be used as the publicPath, * which can be overridden by enabling the webpackDevServer diff --git a/lib/plugins/manifest.js b/lib/plugins/manifest.js index a5e76002..0df0dc5c 100644 --- a/lib/plugins/manifest.js +++ b/lib/plugins/manifest.js @@ -24,11 +24,18 @@ module.exports = function(plugins, webpackConfig) { manifestPrefix = webpackConfig.publicPath.replace(/^\//, ''); } - plugins.push(new ManifestPlugin({ + 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, - })); + }; + + webpackConfig.manifestPluginOptionsCallback.apply( + manifestPluginOptions, + [manifestPluginOptions] + ); + + plugins.push(new ManifestPlugin(manifestPluginOptions)); }; diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index 192c6910..91920a06 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(); + const callback = () => {}; + config.configureManifestPlugin(callback); + + // 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', () => { it('Calling with a duplicate name throws an error', () => { const config = createConfig();