Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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:
*
* .configureManifestPlugin({
* fileName: '../../var/assets/manifest.json'
* })
*
* @param {Object} options
* @returns {publicApi}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@returns {exports}

*/
configureManifestPlugin(options) {
webpackConfig.configureManifestPlugin(options);

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.manifestPluginOptions = {};
}

getContext() {
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions lib/plugins/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,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, webpackConfig.manifestPluginOptions);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor remark here, Object.assign modifies its first argument, so in this case defaultConfig (which doesn't really makes sense since it won't be the default config anymore after that).

You probably could either do:

const defaultConfig = { /* ... */ };
const manifestConfig = Object.assign({}, defaultConfig, webpackConfig.manifestPluginOptions);

Or only use a single variable (which looks a bit less clear in my opinion):

const manifestConfig = Object.assign({}, { /* ... */ }, webpackConfig.manifestPluginOptions);


plugins.push(new ManifestPlugin(manifestConfig));
};
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();
config.configureManifestPlugin({
fileName: '../../var/assets/manifest.json',
});

// 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');
});
});

describe('addEntry', () => {
it('Calling with a duplicate name throws an error', () => {
const config = createConfig();
Expand Down