Skip to content

Commit a9e9da9

Browse files
authored
Merge pull request #7 from kovenliao/fix
fix sha generation, always create new instance
2 parents 02f2433 + b5b3062 commit a9e9da9

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

spec/WebpackSHASpec.js

+41
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,45 @@ describe("WebpackSHAHash", () => {
120120
});
121121
});
122122
});
123+
124+
it("Should generate same hash if content not changed", (done) => {
125+
var config = {
126+
entry: {
127+
entry: path.join(FIXTURES, "entry.js")
128+
},
129+
plugins: [
130+
new WebpackSHAHash()
131+
],
132+
output: {
133+
path: OUTPUT_DIR,
134+
filename: "[name]-bundle.js",
135+
chunkFilename: "[chunkhash].[id].chunk.js"
136+
}
137+
};
138+
139+
webpack(config, (err, stats) => {
140+
expect(err).toBeFalsy();
141+
expect(stats.compilation.errors).toEqual([]);
142+
expect(stats.compilation.warnings).toEqual([]);
143+
144+
let firstRunResult = fs.readdirSync(OUTPUT_DIR);
145+
146+
expect(firstRunResult.length).toEqual(2);
147+
148+
rimraf(OUTPUT_DIR, () => {
149+
webpack(config, (err, stats) => {
150+
expect(err).toBeFalsy();
151+
expect(stats.compilation.errors).toEqual([]);
152+
expect(stats.compilation.warnings).toEqual([]);
153+
154+
let secondRunResult = fs.readdirSync(OUTPUT_DIR);
155+
156+
expect(secondRunResult.length).toEqual(2);
157+
158+
expect(firstRunResult).toEqual(secondRunResult);
159+
done();
160+
});
161+
});
162+
});
163+
});
123164
});

src/webpack_sha_hash.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class WebpackSHAHash {
1616
this.hashingAlgorithm = options.hashingAlgorithm;
1717

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

57+
generateHash(content) {
58+
return createHash(this.hashingAlgorithm).update(content, "utf8").digest("hex");
59+
}
60+
5761
/**
5862
* Called by Webpack which gives a referencer to its compiler object.
5963
* Reference: https://github.com/webpack/docs/wiki/plugins
@@ -63,8 +67,7 @@ class WebpackSHAHash {
6367
compiler.plugin("compilation", (compilation) => {
6468
compilation.plugin("chunk-hash", (chunk, chunkHash) => {
6569
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
66-
const hash = this.hashingAlgorithmInstance.update(source, "utf8");
67-
const calculatedChunkHash = hash.digest("hex");
70+
const calculatedChunkHash = this.generateHash(source);
6871

6972
chunkHash.digest = () => {
7073
return calculatedChunkHash;

0 commit comments

Comments
 (0)