Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove .bind and map -> forEach #154

Closed
wants to merge 2 commits into from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ yarn.lock
.nyc_output
coverage
spec/output/
package-lock.json
53 changes: 23 additions & 30 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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);

Expand All @@ -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) {
Expand All @@ -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({
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -161,9 +158,9 @@ 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(function (manifest, file) {
manifest = files.reduce((manifest, file) => {
manifest[file.name] = file.path;
return manifest;
}, seed);
Expand All @@ -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) {
Expand All @@ -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);

Expand All @@ -211,15 +204,15 @@ 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);

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);
Expand Down