Skip to content
This repository was archived by the owner on Feb 1, 2020. It is now read-only.
This repository was archived by the owner on Feb 1, 2020. It is now read-only.

Contenthash with MiniCssExtractPlugin does not change even if content has changed #65

@arneee

Description

@arneee

I'm using the contenthash in the filenames to allow long term caching:
https://webpack.js.org/guides/caching/

Unfortunately if the content is changed by the PurgeCss plugin only, for example since I have removed a usage of an class or I'm using a class which hasn't been used before, the contenthash does not change. This leads to old files being served to the users and therefore a breaking app.

The content hash changes as soon as I do an actual modification of the CSS files. My guess would be that the contenthash is calculated to early?

webpack config:

const path = require('path');
const glob = require('glob-all')
const ManifestPlugin = require('webpack-manifest-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CleanWebpackPlugin = require('clean-webpack-plugin'); //installed via npm
const PurgecssPlugin = require('purgecss-webpack-plugin');
const webpack = require('webpack');

// the path(s) that should be cleaned
let pathsToClean = [
    'dist'
];

// the clean options to use
let cleanOptions = {
    root:     '..................../public',
    verbose:  true,
    dry:      false
};

function collectWhitelistPatterns() {
    return [/^pcr-/];
}

module.exports = {

    plugins: [
        new CleanWebpackPlugin(pathsToClean, cleanOptions),
        new ManifestPlugin(),
        new PurgecssPlugin({
            paths: glob.sync(['./src-js/**/*', './templates/**/*'],  { nodir: true }),
            whitelistPatterns: collectWhitelistPatterns,
        }),
        new MiniCssExtractPlugin({
            filename: "[name]-[contenthash].css",
            chunkFilename: "[id]-[contenthash].css"
        }),
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery'
        })
    ],

    entry: {
        one: './src-js/index-one.js',
        two: './src-js/index-two.js',
        three: './src-js/index-three.js',
    },

    output: {
        path: path.resolve(__dirname, 'public/dist'),
        filename: dev?'[name]-min.js':'[name]-[contenthash]-min.js',
        publicPath: "/dist/"
    },

    module: {
        rules: [
            {
                test: /\.m?js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        "presets": [
                            [
                                "@babel/preset-env",
                                {
                                    "useBuiltIns": "entry",
                                    "targets": "> 0.25%, not dead",
                                }
                            ]
                        ],
                        "plugins": ["transform-remove-console"]
                    }
                }
            },
            {
                test: /\.(sa|sc|c)ss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                    'sass-loader',
                ],
            },
            {
                test: /\.(png|svg|jpg|gif)$/,
                use: [
                    'file-loader'
                ]
            }
        ]
    },
    externals: {
        jquery: 'jQuery'
    },
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions