每次看到更改时,我都必须清除缓存并刷新

时间:2018-08-28 20:45:07

标签: javascript webpack

webpack.config.js

const path = require('path')
const config = require('config')
const CopyWebpackPlugin = require('copy-webpack-plugin')


module.exports = {
    entry: './public/clientMain.js',
    node: { 
        fs: 'empty' 
    },
    output: {
        filename: 'app-v' + config.VERSION + '.js',
        libraryTarget: 'var',
        library: 'start',
        path: path.resolve(__dirname, './build/js')
    },
    module: {
        loaders: [{
            test: /\.js$/,
            exclude: /node_modules/,
            loader: 'babel-loader'
        }]
    },
    watchOptions: {
        ignored: /node_modules/,
    },   
    plugins: [
        new CopyWebpackPlugin([
          { from: 'public/assets', to: '../assets' }
        ])
    ]
}
package.json 中的

"scripts": {
   "start": "concurrently \"node ./node_modules/webpack/bin/webpack.js -d --watch\" \"nodemon ./server/serverMain.js \" \"node ./node_modules/http-server/bin/http-server -p 8080\""

},

问题是每次我对一个js文件进行更改时,都必须清除浏览器中的“缓存”,然后刷新页面以查看更改! (Chrome中的 Hard Reload 或ofc Empty Cache and Hard Reload

我猜这是不正常的吗?那么我该如何解决呢?

1 个答案:

答案 0 :(得分:1)

正确的方法是在输出文件名中添加哈希。

 output: {
        filename: 'app-v.[hash].' + config.VERSION + '.js',
    },