From 80c01c5dd2aac751ea49bfa1b9ddac26fc35bf44 Mon Sep 17 00:00:00 2001 From: Thomas Sileghem Date: Sun, 8 Apr 2018 15:07:43 +0100 Subject: [PATCH] feat: allow publicPath to be overriten (#139) --- README.md | 12 +++++----- lib/plugin.js | 3 ++- spec/plugin.spec.js | 53 +++++++++++++++++++++++++++++++++------------ 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 7d38fce..0895812 100644 --- a/README.md +++ b/README.md @@ -51,12 +51,6 @@ module.exports = { } ``` -### `publicPath` - -Type: `String` - -A path prefix that will be added to values of the manifest. - ### `options.fileName` Type: `String`
@@ -64,6 +58,12 @@ Default: `manifest.json` The manifest filename in your output directory. +### `options.publicPath` + +Type: `String` +Default: `output.publicPath` + +A path prefix that will be added to values of the manifest. ### `options.basePath` diff --git a/lib/plugin.js b/lib/plugin.js index 3c24c59..52eff33 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -6,6 +6,7 @@ const emitCountMap = new Map(); function ManifestPlugin(opts) { this.opts = _.assign({ + publicPath: null, basePath: '', fileName: 'manifest.json', transformExtensions: /^(gz|map)$/i, @@ -50,7 +51,7 @@ ManifestPlugin.prototype.apply = function(compiler) { const emitCount = emitCountMap.get(outputName) - 1 emitCountMap.set(outputName, emitCount); - var publicPath = compilation.options.output.publicPath; + var publicPath = this.opts.publicPath != null ? this.opts.publicPath : compilation.options.output.publicPath; var stats = compilation.getStats().toJson(); var files = compilation.chunks.reduce(function(files, chunk) { diff --git a/spec/plugin.spec.js b/spec/plugin.spec.js index bb96162..c0a2993 100644 --- a/spec/plugin.spec.js +++ b/spec/plugin.spec.js @@ -153,22 +153,47 @@ describe('ManifestPlugin', function() { }); }); - it('prefixes paths with a public path', function(done) { - webpackCompile({ - context: __dirname, - entry: { - one: './fixtures/file.js', - }, - output: { - filename: '[name].[hash].js', - publicPath: '/app/' - } - }, {}, function(manifest, stats) { - expect(manifest).toEqual({ - 'one.js': '/app/one.' + stats.hash + '.js' + describe('publicPath', () => { + it('prefixes paths with a public path', function(done) { + webpackCompile({ + context: __dirname, + entry: { + one: './fixtures/file.js', + }, + output: { + filename: '[name].[hash].js', + publicPath: '/app/' + } + }, {}, function(manifest, stats) { + expect(manifest).toEqual({ + 'one.js': '/app/one.' + stats.hash + '.js' + }); + + done(); }); + }); - done(); + it('is possible to overrides publicPath', (done) => { + webpackCompile({ + context: __dirname, + entry: { + one: './fixtures/file.js', + }, + output: { + filename: '[name].[hash].js', + publicPath: '/app/' + } + }, { + manifestOptions: { + publicPath: '', + } + }, function(manifest, stats) { + expect(manifest).toEqual({ + 'one.js': 'one.' + stats.hash + '.js' + }); + + done(); + }); }); });