[Webpack 2] Hashing with Webpack for long term caching

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

时间: 2024-12-25 13:29:07

[Webpack 2] Hashing with Webpack for long term caching的相关文章

Webpack系列:在Webpack+Vue中如何将对后端的http请求转到https的后端服务器中?

在上一篇文章(Webpack系列:在Webpack+Vue开发中如何调用tomcat的后端服务器的接口?)我们介绍了如何将对于webpack-dev-server的数据请求转发到后端服务器上,这在大部分情况下就够用了. 然后现在问题又来了,在生产环境下接口一般采用https协议,如果我们要把数据请求转发到生产服务器上怎么办? 首先会想是不是把上一篇博文中提到的proxyTable改成https就可以了,如下:     proxyTable: {                '/appserve

[Webpack] Use the Webpack Dashboard to Monitor Webpack Operations

Learn how to use the new Webpack Dashboard from Formidable Labs to display a pretty, useful output for monitoring the status of your webpack builds. This command line tool replaces the frequently unhelpful large text dump that webpack generates with

入门webpack(浓缩学习webpack经过)

熟话说浓缩就是精华,哈哈,所以就这么简单粗暴的介绍下吧,写下的都是精华. 已经不是第一次听说webpack,但是我的起步有点晚,现在才看.开门见山~~ 1 1.什么是webpack? webpack是当下最热门的前端资源模块化管理和打包工具(就是一个打包器),可以将许多松散的模块按照依赖和规则打包成符合生产环境部署的前端资源,还可以将需要加载的模块进行代码分离,等到实际需要的时候再进行异步加载.通过loader的转换,任何资源都可以视作模块,如COMMONJS模块 ,AMD模块,JS CSS ,

安装webpack后,执行webpack -v命令时报错:SyntaxError: Block-sc

安装webpack后,执行webpack -v命令时报错如下: [[email protected] ~]# webpack -v /usr/local/node-v4.4.7-linux-x64/lib/node_modules/webpack/bin/webpack.js:3 let webpackCliInstalled = false; ^^^ SyntaxError: Block-scoped declarations (let, const, function, class) not

Webpack的使用指南-Webpack的常用解决方案

说是解决方案实际上更像是webpack的插件索引. 写这一篇的目的是为了形成一个所以,将来要用时直接来查找即可. 1.自动构建HTML,可压缩空格,可给引用的js加版本号或随机数:html-webpack-plugin 解决方案:使用插件 html-webpack-plugin webpack.config.js如下: module.exports = {entry: './src/app.js', output: { path: __dirname + '/dist', filename: '

webpack 3.1 升级webpack 4.0

webpack 3.1 升级webpack 4.0 为了提升打包速度以及跟上主流技术步伐,前段时间把项目的webpack 升级到4.0版本以上 webpack 官网:https://webpack.js.org/ 正常操作升级webpack 检查node.npm 版本,该升级的升级 卸载webpack 旧版本,安装最新稳定版本 wepack.webpack cli 遇到loader 报错,升级各种loader,其中vue-loader 参考vue loader 官网说明 删除 ExtractTe

[Webpack 2] Validate your Webpack config with webpack-validator

It’s quite common to make a mistake while developing your webpack configuration. A simple typo can cost you hours of development time. With webpack-validator, you can save yourself a ton of time by validating that your webpack configuration is free o

.1-浅析webpack源码之webpack.cmd

此系列随时可能断更,毕竟我是解释型源码分析-- 尝试看过Spring的源码,有点烧脑,所以还是重回JS吧! 在配置完环境变量后,可以通过webpack指令进行打包,需要知道的是,如果当前路径存在webpack.config.js文件,会被默认指定为配置JS文件 官网原文如下:If a webpack.config.js is present, the webpack command picks it up by default 也就是说直接执行webpack指令会默认执行webpack webp

The way of Webpack learning (I.) -- Configure Webpack from zero(从零开始配置webpack)

学习之路基于webpack3.10.0,webpack4.0之后更新. 一:开始前的配置 1.初始化项目,其实就是新建一个package.json文件,后面的命令依赖里面的配置项. npm init 2.修改npm script定义的任务,新增一项. "scripts": { "start": "webpack --config webpack.config.js" } 3.安装webpack npm i -D [email protected]