Skip to content

Commit

Permalink
Added an option to select the hashing algorithm (closes #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsebastien committed Feb 12, 2016
1 parent 8a8d325 commit 8bff214
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
* 1.0.0
* added an option to select the hashing algorithm to use (hashingAlgorithm)
* check out the [README](README.md) for more details
* updated dependencies
* 0.0.1
* initial release
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,20 @@ module.exports = {
Enjoy!

## Options
Not implemented yet.

Currently, the SHA-256 algorithm is used by default. In the future, you'll be able to choose which version of SHA to use.
### hashingAlgorithm: use a different hashing algorithm
The SHA-256 hashing algorithm is used by default. If you want, you can use any of the supported algorithms supported by sha.js: https://www.npmjs.com/package/sha.js

To override the algorithm, just provide the name of the one you want to use to the plugin:

``` javascript
...
plugins: [
new WebpackSHAHash({
hashingAlgorithm: "sha512"
})
]
```

## Contributing
Take a look at the project's open [issues](https://github.com/dsebastien/webpack-sha-hash/issues) and [milestones](https://github.com/dsebastien/webpack-sha-hash/milestones).
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@
"check-style": "jscs .",
"check-quality": "jshint",
"lint": "npm run check-style && npm run check-quality",
"pretest": "npm run build",
"test": "jasmine-node --captureExceptions spec",
"prebuild": "npm run clean",
"build": "babel src -d dist",
"compile": "npm run build",
"start": "npm run build",
"setup": "npm install --no-optional",
"prepublish": "npm run build && npm run test && npm run lint"
"prepublish": "npm run test && npm run lint"
}
}
58 changes: 56 additions & 2 deletions spec/WebpackSHASpec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

const WebpackSHAHash = require("../dist/webpack_sha_hash");
const path = require("path");
const rimraf = require("rimraf");
const webpack = require("webpack");
Expand All @@ -12,7 +13,60 @@ describe("WebpackSHAHash", () => {
rimraf(OUTPUT_DIR, done);
});

it("Compile without plugin", (done) => {
it("Should not complain if no options are passed", (done) => {
new WebpackSHAHash();
done();
});

it("Should not complain if an empty options object is passed", (done) => {
new WebpackSHAHash({});
done();
});

it("Should complain if an unknown option is passed", (done) => {
try{
new WebpackSHAHash({
dummyOptionThatShouldNeverExist: true
});
expect(false).toBe(true, "An exception should have been thrown!"); //
} catch(e) {
expect(e).not.toBeNull();
}

done();
});

it("Should use sha256 as default hashing algorithm", (done) => {
const plugin = new WebpackSHAHash();

expect(plugin.hashingAlgorithm).toBe("sha256");
done();
});

it("Should use the passed hashing algorithm if valid", (done) => {
let algorithm = "sha512";
const plugin = new WebpackSHAHash({
hashingAlgorithm: algorithm
});

expect(plugin.hashingAlgorithm).toBe(algorithm);
done();
});

it("Should fail if an unknown hashing algorithm is passed", (done) => {
try{
new WebpackSHAHash({
hashingAlgorithm: "__if_this_one_exists_then_lol__"
});
expect(false).toBe(true, "An exception should have been thrown!"); //
} catch(e) {
expect(e).not.toBeNull();
}

done();
});

it("Should compile without plugin", (done) => {
webpack({
entry: {
entry: path.join(FIXTURES, "entry.js")
Expand All @@ -34,7 +88,7 @@ describe("WebpackSHAHash", () => {
});
});

it("Compile twice without plugin", (done) => {
it("Should compile twice without plugin", (done) => {
var config = {
entry: {
entry: path.join(FIXTURES, "entry.js")
Expand Down
30 changes: 27 additions & 3 deletions src/webpack_sha_hash.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
"use strict";

const createHash = require("sha.js");
const sha256 = createHash("sha256");

class WebpackSHAHash {
/**
* Parse the options and initialize the plugin.
* @param options the options if any
*/
constructor(options = {}) {
// TODO handle options
// Hashing algorithm
if(!options.hasOwnProperty("hashingAlgorithm")){
options.hashingAlgorithm = "sha256"; // default
}

this.hashingAlgorithm = options.hashingAlgorithm;

try{
this.hashingAlgorithmInstance = 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 -----------");
}

// remove the option from the object once processed (so that we can identify if unknown options have been passed)
delete options.hashingAlgorithm;

// Check if unknown options were passed (i.e., fail fast and be helpful)
for(const prop in options){
if(options.hasOwnProperty(prop)) {
throw new Error("You have passed an unknown option to the WebpackSHAHash plugin: " + prop);
}
}
}

compareModules(a, b) {
Expand Down Expand Up @@ -39,7 +63,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 = sha256.update(source, "utf8");
const hash = this.hashingAlgorithmInstance.update(source, "utf8");
const calculatedChunkHash = hash.digest("hex");

chunkHash.digest = () => {
Expand Down

0 comments on commit 8bff214

Please sign in to comment.