Sunday, 29 September 2019

AngularJS + webpack how to hash template htmls

I have migrated our AngularJS application to use webpack - before it used gulp. in the gulp version I have used rev plugin to rev all the files (css,js and html) however in the webpack mode , I cannot find a way to add hash to the html templates - which cause issues as the browser serve old html files. How can it be fixed? below is by webpack conf file

const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const OpenBrowserPlugin = require('open-browser-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const RemoteServer = process.env.REMOTE_SERVER;
const appEnv = process.env.NODE_ENV || 'development';
const isProduction = appEnv === 'production';

const patterns = require('../server/src/main/resources/regex.json');

const appPath = path.join(__dirname, 'app');
const buildPath = path.join(__dirname, 'artifacts');

const config = {
    entry: [path.join(appPath, 'index.js')],

    output: {
        path: buildPath,
        filename: '[name].[hash].js',
        chunkFilename: '[name].[hash].js'
    },

resolve: {
    modules: ['node_modules', appPath],
    alias: {
        'ui-select-css': path.resolve('./node_modules/ui-select/dist/select.css'),
        fonts: path.resolve(__dirname, 'assets/fonts')
    }
},

module: {
    rules: [
        {
            test: /\.js$/,
            exclude: /node_modules/,
            loader: 'eslint-loader',
            options: {
                emitWarning: true,
                quiet: true
            }
        },
        {
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: /node_modules/
        },
        {
            test: /\.css$/,
            use: [
                MiniCssExtractPlugin.loader,
                'css-loader',
                'resolve-url-loader'
            ]
        },
        {
            test: /\.less$/,
            use: [{
                loader: 'style-loader'
            }, {
                loader: 'css-loader', options: {
                    url: false,
                    sourceMap: true
                }
            }, {
                loader: 'less-loader', options: {
                    relativeUrls: false,
                    sourceMap: true
                }
            }]
        },
        {
            test: /\.(jpe?g|png|gif)(\?.*)?$/i,
            use: [{
                loader: 'file-loader',
                options: {name: '[path][name].[hash].[ext]'}
            }]
        },
        {
            test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
            use: {
                loader: 'file-loader',
                options: {
                    name: '[name].[ext]',
                    outputPath: 'fonts/'
                }
            }

        },
        {
            test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
            use: [
                {
                    loader: 'url-loader',
                    options: {limit: 10000, mimetype: 'application/octet-stream'}
                }
            ]
        },
        {
            test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
            use: [
                {
                    loader: 'file-loader'
                }
            ]
        },
        {
            test: /\.svg$/i,
            loader: 'raw-loader'
        },
        {
            test: require.resolve('angular'),
            use: [
                {loader: 'expose-loader', options: 'angular'},
            ]
        },
        {
            test: require.resolve('jquery'),
            use: [
                {loader: 'expose-loader', options: '$'},
                {loader: 'expose-loader', options: 'jQuery'},
            ]
        },
        {
            test: require.resolve('lodash'),
            use: [
                {loader: 'expose-loader', options: '_'},
            ]
        },
        {
            test: require.resolve('moment'),
            use: [
                {loader: 'expose-loader', options: 'moment'},
            ]
        },
        {
            test: /\.html$/,
            use: [{
                loader: 'raw-loader',
                options: {name: '[path][name].[hash].[ext]'}
            }]
        }
    ]
},

plugins: [
    new HtmlWebpackPlugin({
        template: path.join(appPath, 'index.html')
    }),

    new webpack.DefinePlugin({
        INJECT_REGEX_HERE: JSON.stringify(patterns)
    }),

    new CopyWebpackPlugin([
        {from: 'app/images', to: 'assets/images'},
        {from: 'app/fonts', to: 'assets/fonts'},
        {from: 'app/templates', to: 'assets/templates'},
        {from: 'app/silent-callback.html', to: 'silent-callback.html'},
        {from: 'node_modules/font-awesome/css', to: 'assets/font-awesome/css'},
        {from: 'node_modules/font-awesome/fonts', to: 'assets/font-awesome/fonts'},
        {from: 'node_modules/angular-ui-grid/fonts', to: 'assets/fonts'},
        {from: 'node_modules/d3/d3.min.js', to: 'assets/d3'}
    ]),

    new MiniCssExtractPlugin({
        filename: '[name].[hash].css',
        chunkFilename: '[id].[hash].css'
    }),

    new OpenBrowserPlugin({url: 'http://localhost:1337'})
],

devtool: isProduction ? 'source-map' : 'inline-source-map',

devServer: {
    port: 1337
},

optimization: {
    splitChunks: {
        cacheGroups: {
            commons: {
                test: /[\\/]node_modules[\\/]/,
                name: 'vendors',
                chunks: 'all'
            }
        }
    }
}

};

if (RemoteServer) {
     console.log('running with remote server', RemoteServer);
     config.devServer.proxy = {
    '/occm/*': 'http://' + RemoteServer
    };
 }

if (isProduction) {
config.plugins.push(
    new CleanWebpackPlugin(buildPath)
    );
}

module.exports = config;


from AngularJS + webpack how to hash template htmls

No comments:

Post a Comment