码迷,mamicode.com
首页 > Web开发 > 详细

[Webpack 2] Hashing with Webpack for long term caching

时间:2016-06-22 23:17:24      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:

Leveraging the browser cache is an important part of page load performance. A great way to utilize this cache is by versioning your resources. In this lesson, learn how to use Webpack’s hashing feature so you can take advantage of long term caching of your assets.

 

Install: 

npm install -D html-webpack-plugin

 

Webpack.config.js

const {resolve} = require(‘path‘)
const HtmlWebpackPlugin = require(‘html-webpack-plugin‘)
const isTest = process.env.NODE_ENV === "test";
module.exports = env => {
  return {
    entry: ‘./js/app.js‘,
    output: {
      filename: ‘bundle.[chunkhash].js‘,
      path: resolve(__dirname, ‘dist‘),
      pathinfo: true,
    },
    context: resolve(__dirname, ‘src‘),
    devtool: env.prod ? ‘source-map‘ : ‘eval‘,
    module: {
      loaders: [
        {test: /\.js$/, loader: ‘babel!eslint‘, exclude: /node_modules/},
        {test: /\.css$/, loader: ‘style!css‘},
      ],
    },
    plugins:[
      new HtmlWebpackPlugin(
            {
              template: ‘./index.html‘
            }
        )
    ]
  }
}

 

Remove bundle.js in index.html

Then in the browser you can see the bundle file has hash name. So everytime, you change the source code, it will update automatically

技术分享

[Webpack 2] Hashing with Webpack for long term caching

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5608706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!