Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*
Expand Down
9 changes: 9 additions & 0 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class WebpackConfig {
this.useImagesLoader = true;
this.useFontsLoader = true;
this.configuredFilenames = {};
this.manifestPluginOptionsCallback = function() {};
}

getContext() {
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions lib/plugins/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};
20 changes: 20 additions & 0 deletions test/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down