diff --git a/spec/WebpackSHASpec.js b/spec/WebpackSHASpec.js index 43e58e6..4e129e7 100644 --- a/spec/WebpackSHASpec.js +++ b/spec/WebpackSHASpec.js @@ -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(); + }); + }); + }); + }); }); diff --git a/src/webpack_sha_hash.js b/src/webpack_sha_hash.js index 24fc874..ff4e4fc 100644 --- a/src/webpack_sha_hash.js +++ b/src/webpack_sha_hash.js @@ -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 -----------"); } @@ -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 @@ -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;