From 423cc7c86db090438fae841462fd23757664402a Mon Sep 17 00:00:00 2001 From: caub Date: Tue, 19 Jun 2018 23:26:51 +0200 Subject: [PATCH 1/2] clean: remove .bind and map -> forEach --- .gitignore | 1 + lib/plugin.js | 51 ++++++++++++++++++++++----------------------------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index a76582a..6d83b3f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ yarn.lock .nyc_output coverage spec/output/ +package-lock.json diff --git a/lib/plugin.js b/lib/plugin.js index d21f96b..33b2316 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -16,13 +16,13 @@ function ManifestPlugin(opts) { map: null, generate: null, sort: null, - serialize: function(manifest) { + serialize: function (manifest) { return JSON.stringify(manifest, null, 2); }, }, opts || {}); } -ManifestPlugin.prototype.getFileType = function(str) { +ManifestPlugin.prototype.getFileType = function (str) { str = str.replace(/\?.*/, ''); var split = str.split('.'); var ext = split.pop(); @@ -32,7 +32,7 @@ ManifestPlugin.prototype.getFileType = function(str) { return ext; }; -ManifestPlugin.prototype.apply = function(compiler) { +ManifestPlugin.prototype.apply = function (compiler) { var moduleAssets = {}; var outputFolder = compiler.options.output.path; @@ -48,7 +48,7 @@ ManifestPlugin.prototype.apply = function(compiler) { } }; - var emit = function(compilation, compileCallback) { + var emit = (compilation, compileCallback) => { const emitCount = emitCountMap.get(outputFile) - 1 emitCountMap.set(outputFile, emitCount); @@ -57,8 +57,8 @@ ManifestPlugin.prototype.apply = function(compiler) { 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) { - return chunk.files.reduce(function (files, path) { + var files = compilation.chunks.reduce((files, chunk) => { + return chunk.files.reduce((files, path) => { var name = chunk.name ? chunk.name : null; if (name) { @@ -80,12 +80,12 @@ ManifestPlugin.prototype.apply = function(compiler) { isAsset: false, isModuleAsset: false }); - }.bind(this), files); - }.bind(this), []); + }, files); + }, []); // module assets don't show up in assetsByChunkName. // we're getting them this way; - files = stats.assets.reduce(function (files, asset) { + files = stats.assets.reduce((files, asset) => { var name = moduleAssets[asset.name]; if (name) { return files.concat({ @@ -113,7 +113,7 @@ ManifestPlugin.prototype.apply = function(compiler) { }); }, files); - files = files.filter(function (file) { + files = files.filter(file => { // Don't add hot updates to manifest var isUpdateChunk = file.path.indexOf('hot-update') >= 0; // Don't add manifest from another instance @@ -125,26 +125,23 @@ ManifestPlugin.prototype.apply = function(compiler) { // Append optional basepath onto all references. // This allows output path to be reflected in the manifest. if (this.opts.basePath) { - files = files.map(function(file) { + files.forEach(file => { file.name = this.opts.basePath + file.name; - return file; - }.bind(this)); + }); } if (publicPath) { // Similar to basePath but only affects the value (similar to how // output.publicPath turns require('foo/bar') into '/public/foo/bar', see // https://github.com/webpack/docs/wiki/configuration#outputpublicpath - files = files.map(function(file) { + files.forEach(file => { file.path = publicPath + file.path; - return file; - }.bind(this)); + }); } - files = files.map(file => { + files.forEach(file => { file.name = file.name.replace(/\\/g, '/'); file.path = file.path.replace(/\\/g, '/'); - return file; }); if (this.opts.filter) { @@ -163,7 +160,7 @@ ManifestPlugin.prototype.apply = function(compiler) { if (this.opts.generate) { manifest = this.opts.generate(seed, files); } else { - manifest = files.reduce(function (manifest, file) { + manifest = files.reduce((manifest, file) => { manifest[file.name] = file.path; return manifest; }, seed); @@ -174,12 +171,8 @@ ManifestPlugin.prototype.apply = function(compiler) { var output = this.opts.serialize(manifest); compilation.assets[outputName] = { - source: function() { - return output; - }, - size: function() { - return output.length; - } + source: () => output, + size: () => output.length }; if (this.opts.writeToFileEmit) { @@ -192,9 +185,9 @@ ManifestPlugin.prototype.apply = function(compiler) { } else { compilation.applyPluginsAsync('webpack-manifest-plugin-after-emit', manifest, compileCallback); } - }.bind(this); + }; - function beforeRun (compiler, callback) { + function beforeRun(compiler, callback) { let emitCount = emitCountMap.get(outputFile) || 0; emitCountMap.set(outputFile, emitCount + 1); @@ -211,7 +204,7 @@ ManifestPlugin.prototype.apply = function(compiler) { }; compiler.hooks.webpackManifestPluginAfterEmit = new SyncWaterfallHook(['manifest']); - compiler.hooks.compilation.tap(pluginOptions, function (compilation) { + compiler.hooks.compilation.tap(pluginOptions, compilation => { compilation.hooks.moduleAsset.tap(pluginOptions, moduleAsset); }); compiler.hooks.emit.tap(pluginOptions, emit); @@ -219,7 +212,7 @@ ManifestPlugin.prototype.apply = function(compiler) { compiler.hooks.run.tap(pluginOptions, beforeRun); compiler.hooks.watchRun.tap(pluginOptions, beforeRun); } else { - compiler.plugin('compilation', function (compilation) { + compiler.plugin('compilation', compilation => { compilation.plugin('module-asset', moduleAsset); }); compiler.plugin('emit', emit); From f8193a06629ed5f4254cbeb63d47e9b52a275cd3 Mon Sep 17 00:00:00 2001 From: caub Date: Wed, 20 Jun 2018 00:08:18 +0200 Subject: [PATCH 2/2] pass compilation object to opts.generate --- lib/plugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugin.js b/lib/plugin.js index 33b2316..f385fd6 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -158,7 +158,7 @@ ManifestPlugin.prototype.apply = function (compiler) { var manifest; if (this.opts.generate) { - manifest = this.opts.generate(seed, files); + manifest = this.opts.generate(seed, files, compilation); } else { manifest = files.reduce((manifest, file) => { manifest[file.name] = file.path;