Skip to content

Commit

Permalink
Merge pull request #7 from kovenliao/fix
Browse files Browse the repository at this point in the history
fix sha generation, always create new instance
  • Loading branch information
dsebastien authored May 31, 2017
2 parents 02f2433 + b5b3062 commit a9e9da9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
41 changes: 41 additions & 0 deletions spec/WebpackSHASpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,45 @@ describe("WebpackSHAHash", () => {
});
});
});

it("Should generate same hash if content not changed", (done) => {
var config = {
entry: {
entry: path.join(FIXTURES, "entry.js")
},
plugins: [
new WebpackSHAHash()
],
output: {
path: OUTPUT_DIR,
filename: "[name]-bundle.js",
chunkFilename: "[chunkhash].[id].chunk.js"
}
};

webpack(config, (err, stats) => {
expect(err).toBeFalsy();
expect(stats.compilation.errors).toEqual([]);
expect(stats.compilation.warnings).toEqual([]);

let firstRunResult = fs.readdirSync(OUTPUT_DIR);

expect(firstRunResult.length).toEqual(2);

rimraf(OUTPUT_DIR, () => {
webpack(config, (err, stats) => {
expect(err).toBeFalsy();
expect(stats.compilation.errors).toEqual([]);
expect(stats.compilation.warnings).toEqual([]);

let secondRunResult = fs.readdirSync(OUTPUT_DIR);

expect(secondRunResult.length).toEqual(2);

expect(firstRunResult).toEqual(secondRunResult);
done();
});
});
});
});
});
9 changes: 6 additions & 3 deletions src/webpack_sha_hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class WebpackSHAHash {
this.hashingAlgorithm = options.hashingAlgorithm;

try{
this.hashingAlgorithmInstance = createHash(this.hashingAlgorithm);
createHash(this.hashingAlgorithm);
} catch(e) {
throw new Error("You have most probably provided an invalid value for the 'hashingAlgorithm' option of the WebpackSHAHash plugin! Error details: " + e.stack + "\n -----------");
}
Expand Down Expand Up @@ -54,6 +54,10 @@ class WebpackSHAHash {
return result + moduleSource;
}

generateHash(content) {
return createHash(this.hashingAlgorithm).update(content, "utf8").digest("hex");
}

/**
* Called by Webpack which gives a referencer to its compiler object.
* Reference: https://github.com/webpack/docs/wiki/plugins
Expand All @@ -63,8 +67,7 @@ class WebpackSHAHash {
compiler.plugin("compilation", (compilation) => {
compilation.plugin("chunk-hash", (chunk, chunkHash) => {
const source = chunk.modules.sort(this.compareModules).map(this.getModuleSource).reduce(this.concatenateSource, ""); // we provide an initialValue in case there is an empty module source. Ref: http://es5.github.io/#x15.4.4.21
const hash = this.hashingAlgorithmInstance.update(source, "utf8");
const calculatedChunkHash = hash.digest("hex");
const calculatedChunkHash = this.generateHash(source);

chunkHash.digest = () => {
return calculatedChunkHash;
Expand Down

0 comments on commit a9e9da9

Please sign in to comment.