All Projects → scinos → Webpack Plugin Hash Output

scinos / Webpack Plugin Hash Output

Licence: isc
Plugin to replace webpack chunkhash with an md5 hash of the final file conent.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Webpack Plugin Hash Output

Prepack Webpack Plugin
A webpack plugin for prepack.
Stars: ✭ 1,054 (+723.44%)
Mutual labels:  webpack, plugin
Award
⚙基于react的服务端渲染框架
Stars: ✭ 91 (-28.91%)
Mutual labels:  webpack, plugin
Craco Alias
A craco plugin for automatic aliases generation for Webpack and Jest
Stars: ✭ 56 (-56.25%)
Mutual labels:  webpack, plugin
Webpack Alioss Plugin
阿里 oss-webpack 自动上传插件
Stars: ✭ 35 (-72.66%)
Mutual labels:  webpack, plugin
Fork Ts Checker Webpack Plugin
Webpack plugin that runs typescript type checker on a separate process.
Stars: ✭ 1,343 (+949.22%)
Mutual labels:  webpack, plugin
Hash Buster
Crack hashes in seconds.
Stars: ✭ 981 (+666.41%)
Mutual labels:  hash, md5
Serverless Plugin Webpack
Serverless Plugin Webpack
Stars: ✭ 72 (-43.75%)
Mutual labels:  webpack, plugin
Slinky
A light-weight, responsive, mobile-like navigation menu plugin
Stars: ✭ 649 (+407.03%)
Mutual labels:  webpack, plugin
Webapp Webpack Plugin
[DEPRECATED] use favicons-webpack-plugin instead
Stars: ✭ 127 (-0.78%)
Mutual labels:  webpack, plugin
Hashcobra
HashCobra Hash Cracking tool.
Stars: ✭ 96 (-25%)
Mutual labels:  hash, md5
Webpack Hashed Chunkids
a plugin to help webpack to generate unique chunk id based on unique module id
Stars: ✭ 15 (-88.28%)
Mutual labels:  webpack, hash
Merkle
Node.js module implementing Merkle tree algorithm
Stars: ✭ 123 (-3.91%)
Mutual labels:  hash, md5
Feflow
🚀 A command line tool aims to improve front-end engineer workflow and standard, powered by TypeScript.
Stars: ✭ 942 (+635.94%)
Mutual labels:  webpack, plugin
Dotenv Webpack
A secure webpack plugin that supports dotenv and other environment variables and only exposes what you choose and use.
Stars: ✭ 1,022 (+698.44%)
Mutual labels:  webpack, plugin
Skpm
💎📦 A utility to build and publish Sketch plugins
Stars: ✭ 890 (+595.31%)
Mutual labels:  webpack, plugin
Digestif
Simple hash algorithms in OCaml
Stars: ✭ 69 (-46.09%)
Mutual labels:  hash, md5
Openhashtab
📝 File hashing and checking shell extension
Stars: ✭ 599 (+367.97%)
Mutual labels:  hash, md5
Jquery.localscroll
Animated anchor navigation made easy with jQuery
Stars: ✭ 624 (+387.5%)
Mutual labels:  hash, plugin
Cypress Webpack Preprocessor
Cypress preprocessor for bundling JavaScript via webpack
Stars: ✭ 93 (-27.34%)
Mutual labels:  webpack, plugin
Wordless
All the power of Pug, Sass, Coffeescript and WebPack in your WordPress theme. Stop writing themes like it's 1998.
Stars: ✭ 1,374 (+973.44%)
Mutual labels:  webpack, plugin

webpack-plugin-hash-output

Build Status

Plugin to replace webpack chunkhash with an md5 hash of the final file conent.

Installation

With npm

npm install webpack-plugin-hash-output --save-dev

With yarn

yarn add webpack-plugin-hash-output --dev

Why?

There are other webpack plugins for hashing out there. But when they run, they don't "see" the final form of the code, because they run before plugins like webpack.optimize.UglifyJsPlugin. In other words, if you change webpack.optimize.UglifyJsPlugin config, your hashes won't change, creating potential conflicts with cached resources.

The main difference is that webpack-plugin-hash-output runs in the last compilation step. So any change in webpack or any other plugin that actually changes the output, will be "seen" by this plugin, and therefore that change will be reflected in the hash.

It will generate files like entry.<hash>.js, where <hash> is always a hash of the content (well, a subset of). Example:

$ md5 entry.32f1718dd08eccda2791.js

MD5 (entry.32f1718dd08eccda2791.js) = 32f1718dd08eccda2791ff7ed466bd98

All other assets (common files, manifest files, HTML output...) will use the new md5 hash to reference the asset.

Compatibility

Requires webpack >=4

Usage

Just add this plugin as usual.

// webpack.config.js
const HashOutput = require('webpack-plugin-hash-output');

module.exports = {
    // ...
    output: {
        //...
        filename: '[name].[chunkhash].js',
    },
    plugins: [
        new HashOutput(options)
    ]
};

Sourcemap support

This plugin partially supports sourcemaps. When a chunk hash is recomputed, it will update the name of the chunk in the chunks's sourcemap, but it won't recompute the name of the hash file. This has the side effect that the name of the sourcemap will differ from the name of the chunk. Example:

Before:

// app.<oldhash>.js

// ...code...
//# sourceMappingURL=entry.<oldhash>.js.map
// app.<oldhash>.js.map

// ...
"file":"app.<oldhash>.js"
// ...

After:

// app.<newhash>.js

// ...code...
//# sourceMappingURL=entry.<oldhash>.js.map
// app.<oldhash>.js.map

// ...
"file":"app.<newhash>.js"
// ...

We can't fully support sourcemaps (i.e. recomute sourcemap hash) because the circular reference: we can't compute sourcemap hash without computing the file hash first, and we can't compute the file hash without the sourcemap hash.

Interaction with other plugins

Because this plugin modifies the name of the assets, it break other plugins that also operate on the name of the assets if the order of the plugins is not correct. For plugin-webpack-hash-output to work, it has to be the first plugin to run in the emit phase. Inside the same phase, the order of the plugins is determined by the order in which they appear in webpack's config option plugins.

A specific example of this is html-webpack-plugin: plugin-webpack-hash-output must be listed before html-webpack-plugin. Example:

plugins: [
    new HashOutput(...),
    new HtmlWebpackPlugin(...),
]

When the webpack compilation starts, this plugin will check if there are other plugins that might conflict with the output and display a warning. It is recommended you fix the order of the plugins unless you really know what you are doing:

[WARNING] There are other plugins configured that might interfere with webpack-plugin-hash-output.
For this plugin to work correctly, it should be the first plugin in the "emit" phase. Please
be sure that the rest of the plugins run after webpack-plugin-hash-output (ensure they are listed
after webpack-plugin-hash-output in the 'plugins' option in your webpack config).

Plugins that could interfere with webpack-plugin-hash-output:
 * HtmlWebpackPlugin

Options

Note: Use Webpack's own hash output options to configure hash function and length.

options.validateOutput

boolean, defaults to false.

After webpack has compiled and generated all the assets, checks that the hash of the content is included in the file. If it is not, it will throw an error and the webpack process will fail.

options.validateOutputRegex

regex, defaults to /^.*$/

When validating the output (see options.validateOutput), only evaluate hashes for files matching this regexp. Useful for skipping source maps or other output. Example:

module.exports = {
    entry: {
        main: './src/app.js',
    },
    output: {
        filename: 'assets/[name].[chunkhash].js',
    },
    plugins: [
        new HashOutput({
            validateOutput: true,
            // Check that md5(assets/main.<hash>.js) === <hash>, but doesn't check fragments/app.html
            validateOutputRegex: /^assets\/.*\.js$/,
        }),
        new HtmlWebpackPlugin({
            filename: 'fragments/app.html',
            chunks: ['main'],
        }),
    ]
};

Contributions

Running tests

Tests can be run by:

yarn test

After running the tests, you might want to run yarn run clean to clean up temp files generated by the tests.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].