├── .editorconfig ├── .gitignore ├── .jshintignore ├── .jshintrc ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── example ├── dest │ └── index.html ├── js │ ├── app.js │ └── module.js └── webpack.config.js ├── index.d.ts ├── index.js ├── package.json └── spec ├── WebpackChunkHashSpec.js └── fixtures ├── entry.js ├── module1.js └── module2.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | coverage 3 | dist 4 | node_modules 5 | *.log 6 | -------------------------------------------------------------------------------- /.jshintignore: -------------------------------------------------------------------------------- 1 | /example 2 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | // From JSHint Default Configuration File 3 | // See http://jshint.com/docs/ for more details 4 | 5 | "maxerr" : 50, // {int} Maximum error before stopping 6 | 7 | // Enforcing 8 | "bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.) 9 | "camelcase" : false, // true: Identifiers must be in camelCase 10 | "curly" : true, // true: Require {} for every new block or scope 11 | "eqeqeq" : true, // true: Require triple equals (===) for comparison 12 | "forin" : false, // true: Require filtering for..in loops with obj.hasOwnProperty() 13 | "freeze" : true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc. 14 | "immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());` 15 | "indent" : 2, // {int} Number of spaces to use for indentation 16 | "latedef" : false, // true: Require variables/functions to be defined before being used 17 | "newcap" : false, // true: Require capitalization of all constructor functions e.g. `new F()` 18 | "noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee` 19 | "noempty" : true, // true: Prohibit use of empty blocks 20 | "nonbsp" : true, // true: Prohibit "non-breaking whitespace" characters. 21 | "nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment) 22 | "plusplus" : false, // true: Prohibit use of `++` & `--` 23 | "quotmark" : false, // Quotation mark consistency: 24 | // false : do nothing (default) 25 | // true : ensure whatever is used is consistent 26 | // "single" : require single quotes 27 | // "double" : require double quotes 28 | "undef" : true, // true: Require all non-global variables to be declared (prevents global leaks) 29 | "unused" : true, // true: Require all defined variables be used 30 | "strict" : true, // true: Requires all functions run in ES5 Strict Mode 31 | "maxparams" : false, // {int} Max number of formal params allowed per function 32 | "maxdepth" : false, // {int} Max depth of nested blocks (within functions) 33 | "maxstatements" : false, // {int} Max number statements per function 34 | "maxcomplexity" : false, // {int} Max cyclomatic complexity per function 35 | "maxlen" : false, // {int} Max number of characters per line 36 | 37 | // Relaxing 38 | "asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons) 39 | "boss" : false, // true: Tolerate assignments where comparisons would be expected 40 | "debug" : false, // true: Allow debugger statements e.g. browser breakpoints. 41 | "eqnull" : false, // true: Tolerate use of `== null` 42 | "es5" : false, // true: Allow ES5 syntax (ex: getters and setters) 43 | "esnext" : false, // true: Allow ES.next (ES6) syntax (ex: `const`) 44 | "moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features) 45 | // (ex: `for each`, multiple try/catch, function expression…) 46 | "evil" : false, // true: Tolerate use of `eval` and `new Function()` 47 | "expr" : false, // true: Tolerate `ExpressionStatement` as Programs 48 | "funcscope" : false, // true: Tolerate defining variables inside control statements 49 | "globalstrict" : false, // true: Allow global "use strict" (also enables 'strict') 50 | "iterator" : false, // true: Tolerate using the `__iterator__` property 51 | "lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block 52 | "laxbreak" : false, // true: Tolerate possibly unsafe line breakings 53 | "laxcomma" : false, // true: Tolerate comma-first style coding 54 | "loopfunc" : true, // true: Tolerate functions being defined in loops 55 | "multistr" : false, // true: Tolerate multi-line strings 56 | "noyield" : false, // true: Tolerate generator functions with no yield statement in them. 57 | "notypeof" : false, // true: Tolerate invalid typeof operator values 58 | "proto" : false, // true: Tolerate using the `__proto__` property 59 | "scripturl" : false, // true: Tolerate script-targeted URLs 60 | "shadow" : false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;` 61 | "sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation 62 | "supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;` 63 | "validthis" : false, // true: Tolerate using this in a non-constructor function 64 | 65 | // Environments 66 | "browser" : true, // Web Browser (window, document, etc) 67 | "browserify" : false, // Browserify (node.js code in the browser) 68 | "couch" : false, // CouchDB 69 | "devel" : true, // Development/debugging (alert, confirm, etc) 70 | "dojo" : false, // Dojo Toolkit 71 | "jasmine" : false, // Jasmine 72 | "jquery" : false, // jQuery 73 | "mocha" : true, // Mocha 74 | "mootools" : false, // MooTools 75 | "node" : true, // Node.js 76 | "nonstandard" : false, // Widely adopted globals (escape, unescape, etc) 77 | "prototypejs" : false, // Prototype and Scriptaculous 78 | "qunit" : false, // QUnit 79 | "rhino" : false, // Rhino 80 | "shelljs" : false, // ShellJS 81 | "worker" : false, // Web Workers 82 | "wsh" : false, // Windows Scripting Host 83 | "yui" : false, // Yahoo User Interface 84 | 85 | "globals" : { 86 | "expect": false, 87 | "describe": false, 88 | "ddescribe": false, 89 | "beforeEach": false, 90 | "it": false, 91 | "iit": false 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | coverage 3 | dist 4 | example 5 | node_modules 6 | spec 7 | *.log 8 | .* 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | 3 | language: node_js 4 | node_js: 5 | - "0.12" 6 | - "iojs" 7 | - "4" 8 | - "5" 9 | - "6" 10 | - "7" 11 | - "8" 12 | - "10" 13 | - "11" 14 | 15 | os: 16 | - linux 17 | 18 | install: 19 | - travis_retry npm install 20 | 21 | script: 22 | - uname -a 23 | - node --version 24 | - npm --version 25 | # linting 26 | - npm run lint 27 | # testing 28 | - npm run test 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Alex Indigo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # webpack-chunk-hash 2 | 3 | Plugin to replace a standard webpack chunk hashing with custom (md5) one. 4 | 5 | _Note: It's a clone of [webpack-md5-hash](https://www.npmjs.com/package/webpack-md5-hash) plugin, but without sorting provided chunks (unobtrusive), 6 | and using native crypto module (performance)._ 7 | 8 | ## Install 9 | 10 | ``` 11 | npm install --save-dev webpack-chunk-hash 12 | ``` 13 | 14 | ## Example 15 | 16 | Just add this plugin as usual. 17 | 18 | ```javascript 19 | 20 | // webpack.config.js 21 | 22 | var WebpackChunkHash = require('webpack-chunk-hash'); 23 | 24 | module.exports = { 25 | // ... 26 | output: { 27 | filename: '[name].[chunkhash].js', 28 | chunkFilename: '[name].[chunkhash].js', 29 | }, 30 | plugins: [ 31 | new WebpackChunkHash({algorithm: 'md5'}) // 'md5' is default value 32 | ] 33 | }; 34 | 35 | ``` 36 | 37 | ## Options 38 | 39 | ``` 40 | // a callback to add more content to the resulting hash 41 | additionalHashContent: function(chunk) { return 'your additional content to hash'; } 42 | // which algorithm to use (https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm) 43 | algorithm: 'md5' 44 | // which digest to use (https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding) 45 | digest: 'hex' 46 | ``` 47 | 48 | ## License 49 | 50 | WebpackChunkHash plugin is released under the [MIT](License) license. 51 | -------------------------------------------------------------------------------- /example/dest/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /example/js/app.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | require.ensure('./module', function (require) { 4 | var module = require('./module'); 5 | console.log(module); 6 | }); -------------------------------------------------------------------------------- /example/js/module.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = 'Hello World!!!'; -------------------------------------------------------------------------------- /example/webpack.config.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var WebpackChunkHash = require('../'); 4 | var path = require('path'); 5 | 6 | 7 | var src = path.join(__dirname, '/js') 8 | , dest = path.join(__dirname, '/dest') 9 | ; 10 | 11 | module.exports = { 12 | context: src, 13 | entry: { 14 | app: './app.js' 15 | }, 16 | output: { 17 | path: dest, 18 | filename: '[name].js', 19 | chunkFilename: '[chunkhash].[id].chunk.js' 20 | }, 21 | plugins: [ 22 | new WebpackChunkHash() 23 | ] 24 | }; 25 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | // Type definitions for webpack-chunk-hash 0.4.0 2 | // Project: https://github.com/alexindigo/webpack-chunk-hash 3 | // Definitions by: Cristian Lorenzo 4 | 5 | import { Plugin } from "webpack"; 6 | 7 | declare class WebpackChunkHash extends Plugin { 8 | constructor(options?: WebpackChunkHash.WebpackChunkHashPluginOptions); 9 | } 10 | 11 | export = WebpackChunkHash; 12 | 13 | declare namespace WebpackChunkHash { 14 | interface WebpackChunkHashPluginOptions { 15 | /** 16 | * A callback to add more content to the resulting hash 17 | */ 18 | additionalHashContent?: (chunk: any) => string; 19 | /** 20 | * Which algorithm to use. Defaults to 'md5'. 21 | * See https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm 22 | */ 23 | algorithm?: string; 24 | /** 25 | * Which digest to use. Defaults to 'hex'. 26 | * See https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding 27 | */ 28 | digest?: "hex" | "latin1" | "base64"; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var crypto = require('crypto'); 2 | 3 | module.exports = WebpackChunkHash; 4 | 5 | function WebpackChunkHash(options) 6 | { 7 | options = options || {}; 8 | 9 | this.algorithm = options.algorithm || 'md5'; 10 | this.digest = options.digest || 'hex'; 11 | this.additionalHashContent = options.additionalHashContent || function() { return ''; }; 12 | } 13 | 14 | WebpackChunkHash.prototype.apply = function(compiler) 15 | { 16 | var _plugin = this; 17 | 18 | var compilerPlugin; 19 | var compilationPlugin; 20 | if (compiler.hooks) { 21 | compilerPlugin = function (fn) 22 | { 23 | compiler.hooks.compilation.tap('WebpackChunkHash', fn); 24 | }; 25 | compilationPlugin = function (compilation, fn) 26 | { 27 | compilation.hooks.chunkHash.tap('WebpackChunkHash', fn); 28 | } 29 | } else { 30 | compilerPlugin = function (fn) 31 | { 32 | compiler.plugin('compilation', fn); 33 | }; 34 | compilationPlugin = function (compilation, fn) 35 | { 36 | compilation.plugin('chunk-hash', fn); 37 | } 38 | } 39 | 40 | compilerPlugin(function(compilation) 41 | { 42 | compilationPlugin(compilation, function(chunk, chunkHash) 43 | { 44 | var modules; 45 | if (chunk.modulesIterable) { 46 | modules = Array.from(chunk.modulesIterable, getModuleSource); 47 | } else if (chunk.mapModules) { 48 | modules = chunk.mapModules(getModuleSource); 49 | } else { 50 | modules = chunk.modules.map(getModuleSource); 51 | } 52 | var source = modules.sort(sortById).reduce(concatenateSource, '') 53 | , hash = crypto.createHash(_plugin.algorithm).update(source + _plugin.additionalHashContent(chunk)) 54 | ; 55 | 56 | chunkHash.digest = function(digest) 57 | { 58 | return hash.digest(digest || _plugin.digest); 59 | }; 60 | }); 61 | }); 62 | }; 63 | 64 | // helpers 65 | 66 | function sortById(a, b) 67 | { 68 | return a.id - b.id; 69 | } 70 | 71 | function getModuleSource(module) 72 | { 73 | return { 74 | id : module.id, 75 | source: (module._source || {})._value || '', 76 | dependencies: (module.dependencies || []).map(function(d){ return d.module ? d.module.id : ''; }) 77 | }; 78 | } 79 | 80 | function concatenateSource(result, module) 81 | { 82 | return result + '#' + module.id + ':' + module.source + '$' + (module.dependencies.join(',')); 83 | } 84 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webpack-chunk-hash", 3 | "version": "0.6.0", 4 | "description": "Plugin to replace a standard webpack chunk hashing with custom (md5) one.", 5 | "main": "index.js", 6 | "scripts": { 7 | "lint": "jshint", 8 | "pretest": "rimraf dist", 9 | "test": "jasmine-node --captureExceptions spec" 10 | }, 11 | "pre-commit": [ 12 | "lint", 13 | "test" 14 | ], 15 | "repository": { 16 | "type": "git", 17 | "url": "git@github.com:alexindigo/webpack-chunk-hash.git" 18 | }, 19 | "keywords": [ 20 | "webpack", 21 | "hash", 22 | "plugin", 23 | "md5" 24 | ], 25 | "author": "Alex Indigo ", 26 | "license": "MIT", 27 | "dependencies": { 28 | "@types/webpack": "^3.0.0 || ^4.0.0" 29 | }, 30 | "devDependencies": { 31 | "argparse": "^1.0.4", 32 | "jasmine-node": "^1.14.5", 33 | "jshint": "^2.8.0", 34 | "rimraf": "^2.5.4", 35 | "webpack": "^1.13.3" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /spec/WebpackChunkHashSpec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var path = require('path'), 4 | rimraf = require('rimraf'), 5 | webpack = require('webpack'), 6 | fs = require('fs'); 7 | 8 | var OUTPUT_DIR = path.join(__dirname, '../dist'), 9 | FIXTURES = path.join(__dirname, './fixtures'); 10 | 11 | describe('WebpackMd5Hash', function () { 12 | 13 | beforeEach (function (done) { 14 | rimraf(OUTPUT_DIR, done); 15 | }); 16 | 17 | it('Compile without plugin', function (done) { 18 | webpack({ 19 | entry: { 20 | entry: path.join(FIXTURES, 'entry.js') 21 | }, 22 | output: { 23 | path: OUTPUT_DIR, 24 | filename: '[name]-bundle.js', 25 | chunkFilename: '[chunkhash].[id].chunk.js' 26 | } 27 | }, function (err, stats) { 28 | expect(err).toBeFalsy(); 29 | expect(stats.compilation.errors).toEqual([]); 30 | expect(stats.compilation.warnings).toEqual([]); 31 | var outputDir = fs.readdirSync(OUTPUT_DIR); 32 | expect(outputDir.length).toEqual(2); 33 | done(); 34 | }); 35 | }); 36 | 37 | it('Compile twice without plugin', function (done) { 38 | var config = { 39 | entry: { 40 | entry: path.join(FIXTURES, 'entry.js') 41 | }, 42 | output: { 43 | path: OUTPUT_DIR, 44 | filename: '[name]-bundle.js', 45 | chunkFilename: '[chunkhash].[id].chunk.js' 46 | } 47 | }; 48 | webpack(config, function (err, stats) { 49 | expect(err).toBeFalsy(); 50 | expect(stats.compilation.errors).toEqual([]); 51 | expect(stats.compilation.warnings).toEqual([]); 52 | var outputDir = fs.readdirSync(OUTPUT_DIR); 53 | expect(outputDir.length).toEqual(2); 54 | webpack(config, function (err, stats) { 55 | expect(err).toBeFalsy(); 56 | expect(stats.compilation.errors).toEqual([]); 57 | expect(stats.compilation.warnings).toEqual([]); 58 | var outputDir = fs.readdirSync(OUTPUT_DIR); 59 | expect(outputDir.length).toEqual(2); 60 | done(); 61 | }); 62 | }); 63 | }); 64 | }); 65 | -------------------------------------------------------------------------------- /spec/fixtures/entry.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | require.ensure(["./module1", "./module2"], function() { 4 | 5 | }); 6 | -------------------------------------------------------------------------------- /spec/fixtures/module1.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | "My module1"; 4 | -------------------------------------------------------------------------------- /spec/fixtures/module2.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | "My module 2"; 4 | --------------------------------------------------------------------------------