Skip to content
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ module.exports = {
#### `shaLength`

Allows truncating the SHA to a specific character length. 7 by default.

#### `useHead`

Boolean: if useHead is true will return current git hash, if false will return most recent git hash of files within chunk
27 changes: 16 additions & 11 deletions lib/GitSHAPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ var async = require('async'),
function GitSHAPlugin(options) {
options = options || {};
this.shaLength = options.shaLength || 7;
this.useHead = options.useHead;
}
module.exports = GitSHAPlugin;

GitSHAPlugin.prototype.constructor = GitSHAPlugin;
GitSHAPlugin.prototype.apply = function(compiler) {
var replaceSha = this.replaceSha.bind(this);
var useHead = this.useHead;
var projectRoot = path.resolve();
var cwdToProjectRoot = path.relative(process.cwd(), projectRoot) || '.';
compiler.plugin("compilation", function(compilation) {
Expand All @@ -22,17 +24,20 @@ GitSHAPlugin.prototype.apply = function(compiler) {
// final hashes and asset paths are computed, so it's most appropriate.
compilation.plugin("optimize-tree", function(chunks, modules, done) {
var tasks = chunks.map(function (chunk) {
var files = chunk.modules.map(function (m) {
return m.resource
&& m.resource.indexOf(projectRoot) === 0
// trim paths a bit - we can give git relative paths, and this
// helps us avoid argument length limits
&& m.resource
.replace(projectRoot, cwdToProjectRoot)
.replace(STRIP_QUERY_REGEXP, '');
}).filter(Boolean);

return gitsha.bind(null, files);
if(!useHead) {
var files = chunk.modules.map(function (m) {
return m.resource
&& m.resource.indexOf(projectRoot) === 0
// trim paths a bit - we can give git relative paths, and this
// helps us avoid argument length limits
&& m.resource
.replace(projectRoot, cwdToProjectRoot)
.replace(STRIP_QUERY_REGEXP, '');
}).filter(Boolean);

return gitsha.bind(null, files);
}
else return gitsha;
});

async.parallel(tasks, function(err, res) {
Expand Down