GithubHelp home page GithubHelp logo

mkraynov / content-hashed-module-ids-webpack-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mmmeff/content-hashed-module-ids-webpack-plugin

0.0 0.0 0.0 204 KB

A webpack plugin that allows you to share common chunks between different webpack builds

License: MIT License

JavaScript 100.00%

content-hashed-module-ids-webpack-plugin's Introduction

content-hashed-module-ids-webpack-plugin

npm

This plugin generates module ID from unique hashes based on the file contents of the module. Unlike Webpack's HashedModuleIdsPlugin, which generates hashes from the relative import path of the module.

You can use this plugin to guarantee that multiple webpack bundles on a page do not clash their module IDs and overwrite modules when loading chunk assets. This is pivotal to achieving long-term caching across disparate builds.

Furthermore, this plugin gives you the ability to share chunks between multiple webpack bundles on a page so long as they use the same versions, and when they do not, the page will load both and continue working as expected.

Install

npm i -D content-hashed-module-ids-webpack-plugin

Configuration

This plugin takes the same options used by Webpack's HashedModuleIdsPlugin.

Plugin usage

webpack.config.js

const ContentHashedModuleIdsPlugin = require('content-hashed-module-ids-webpack-plugin');

module.exports = {
    // ...
    plugins: [
        new ContentHashedModuleIdsPlugin({
            // HashedModuleIdsPlugin options
        })
    ]
    // ...
}

Recommended configuration for long-term caching and multi-bundle chunk-sharing

This configuration is heavily based on the article The 100% correct way to split your chunks with webpack.

const ContentHashedModuleIdsPlugin = require('content-hashed-module-ids-webpack-plugin');

const isDev = process.env.NODE_ENV !== 'production';

module.exports = {
    context: __dirname,
    entry: path.resolve(__dirname, './index.js'),
    output: {
        filename: '[name].[chunkhash].js',
        path: path.join(__dirname, './dist'),
        publicPath: './static/',
        library: 'SharedChunkBundles' // Groups all similarly built packages into the same library
    },
    plugins: [
        new ContentHashedModuleIdsPlugin() // Guarantees that all moduleIds under the SharedChunkBundles library are unique
    ],
    optimization: {
        runtimeChunk: 'single',
        splitChunks: {
            chunks: 'all',
            maxInitialRequests: Infinity,
            minSize: 0,
            cacheGroups: {
                vendors: {
                    // splits out all node_modules into chunks by name
                    test: /[\\/]node_modules[\\/]/,
                    name: (module) => {
                        const pName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
                        return `npm.${pName.replace('@', '')}`;
                    }
                }
            }
        }
    },
    mode: isDev ? 'development' : 'production',
    devtool: isDev ? 'eval' : undefined,
}

License

MIT

content-hashed-module-ids-webpack-plugin's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.