ctf0 / laravel-mix-versionhash

Auto append hash to file instead of using virtual one
MIT License
61 stars 18 forks source link

versioning css content #26

Closed josuelrocha closed 4 years ago

josuelrocha commented 4 years ago

Hello,

Is it possible to make this plugin also add filename versioning for images, fonts and etc that are called inside css?

ctf0 commented 4 years ago

am not sure if its possible, does the original version() do that ?

josuelrocha commented 4 years ago

am not sure if its possible, does the original version() do that ?

yes, but hash is in query string and not in filename

ctf0 commented 4 years ago

hashing the css content file names means that

i think its doable but in some cases it might take forever to finish the build

josuelrocha commented 4 years ago

I forked Laravel-mix and modified it to have the hash in the file name.

Take a look: https://github.com/JosuelRocha/laravel-mix/commit/5b955246a8386e296114a7c5144039995eacfff0

I just changed the webpack rules!! I also made the changes straight through the webpack.mix.js file and it worked perfectly too, check out below

mix.webpackConfig({
    module: {
        rules: [
            {
                test: /(\.(png|jpe?g|gif|webp)$|^((?!font).)*\.svg$)/,
                loaders: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: path => {
                                if (!/node_modules|bower_components/.test(path)) {
                                    return (
                                        Config.fileLoaderDirs.images +
                                        '/[name].[hash:8].[ext]'
                                    );
                                }

                                return (
                                    Config.fileLoaderDirs.images +
                                    '/vendor/' +
                                    path
                                        .replace(/\\/g, '/')
                                        .replace(
                                            /((.*(node_modules|bower_components))|images|image|img|assets)\//g,
                                            ''
                                        ) +
                                    '?[hash]'
                                );
                            },
                            publicPath: Config.resourceRoot
                        }
                    },

                    {
                        loader: 'img-loader',
                        options: Config.imgLoaderOptions
                    }
                ]
            },
            {
                test: /(\.(woff2?|ttf|eot|otf)$|font.*\.svg$)/,
                loader: 'file-loader',
                options: {
                    name: path => {
                        if (!/node_modules|bower_components/.test(path)) {
                            return Config.fileLoaderDirs.fonts + '/[name].[hash:8].[ext]';
                        }

                        return (
                            Config.fileLoaderDirs.fonts +
                            '/vendor/' +
                            path
                                .replace(/\\/g, '/')
                                .replace(
                                    /((.*(node_modules|bower_components))|fonts|font|assets)\//g,
                                    ''
                                ) +
                            '?[hash]'
                        );
                    },
                    publicPath: Config.resourceRoot
                }
            },
            {
                test: /\.(cur|ani)$/,
                loader: 'file-loader',
                options: {
                    name: '[name].[hash:8].[ext]',
                    publicPath: Config.resourceRoot
                }
            },
        ],
    },
});

so I believe your plugin just rewrite the rules for webpack as in the examples I mentioned, and so your plugin will be complete, hashed in the filename for everyone.

ctf0 commented 4 years ago

awesome, can u make a PR ?

josuelrocha commented 4 years ago

done

28

ctf0 commented 4 years ago

awesome, will continue the conversation there