简体   繁体   English

当 watch 设置为 true 时,Webpack 不会干净地运行

[英]Webpack doesn't run clean when watch is set to true

This is my webpack.config.js file:这是我的 webpack.config.js 文件:

const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const config = {
    entry: {
        bundle: './javascript/index.js'
    },
    output: {
        path: path.resolve(__dirname, 'build'),
        filename: '[name].[chunkhash].js'
    },
    module: {
        rules: [
            {
                use: 'babel-loader',
                test: /\.js$/,
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            },
            {
                test: /\.(jpe?g|png|gif|svg)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 40000
                        }
                    },
                    'image-webpack-loader'
                ]
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin('style.css'),
        new HtmlWebpackPlugin({
            template: 'src/index.html'
        })
    ],
    watch: true
};

module.exports = config;

As you can tell from the last line I'm setting the watch option to true.从最后一行可以看出,我将 watch 选项设置为 true。 In addition, I'm using chunkhash to generates a new javascript file when I make a change to any of my javascript files.此外,当我对任何 javascript 文件进行更改时,我正在使用 chunkhash 生成一个新的 javascript 文件。 However, it is not running my rinraf clean command when the watch option is set to 'true'.但是,当 watch 选项设置为“true”时,它不会运行我的 rinraf clean 命令。

Here is a portion of my package.json file that:这是我的 package.json 文件的一部分:

{
    "name": "budgety",
    "version": "1.0.0",
    "description": "Budget app",
    "main": "app.js",
    "scripts": {
      "clean": "rimraf build",
      "build": "npm run clean && webpack"
    },
.
.
.

Why is this happening?为什么会这样?

My goal is to:我的目标是:

  1. Have my compiled javascript be updated after I update any of my javascript files, so I don't need to run 'npm run build' every single time I make a change to my js files.在我更新我的任何 javascript 文件后更新我编译的 javascript,所以我不需要每次对我的 js 文件进行更改时都运行“npm run build”。

  2. Clean the old javascript 'hashed' file which used be taken care of by 'rimraf' but for some reason it isn't cleaning the new hashed javascript files in watch mode.清除由 'rimraf' 处理的旧 javascript '散列' 文件,但由于某种原因,它不会在监视模式下清除新的散列 javascript 文件。

The watch mode works in a way that it only recompiles the files that were changed.监视模式的工作方式是仅重新编译已更改的文件。 That's why, normally, during the watch mode the hash prefixes are not enabled (because the files are changed nearly every minute which makes it more complicated to track the changed hashes etc).这就是为什么通常在监视模式下不启用哈希前缀的原因(因为文件几乎每分钟都在更改,这使得跟踪更改的哈希等变得更加复杂)。 In other words one should have a dev and prod environments that will behave slightly differently.换句话说,应该有一个devprod环境,它们的行为会略有不同。

Eg you need to pass an argument, see here how and then use them in your config file:例如,您需要传递一个参数,请参阅此处,然后在您的配置文件中使用它们:

 filename: env.withHashPrefixes ? '[name].[chunkhash].js' : '[name].js'

Now you will not need to clean anything because the filenames are always the same现在你不需要清理任何东西,因为文件名总是一样的

Original answer原答案

It does and it will not run your rimraf command because the watch happens inside of the webpack ind it has no idea what you did run outside of it.它会并且不会运行你的 rimraf 命令,因为 watch 发生在 webpack 内部,它不知道你在它之外运行了什么。

Use clean-webpack-plugin which is as easy as使用clean-webpack-plugin这么简单

plugins: [
  new CleanWebpackPlugin('build')
]

I've experienced the same problem that my assets in /assets/ folder were cleaned and not rebuilt when enabling output.clean .我遇到了同样的问题,我在/assets/文件夹中的/assets/在启用output.clean时被清理而不是重建。

I've worked around this by ignoring /assets/ from cleaning in webpack.config.js .我通过在webpack.config.js忽略/assets/来解决这个webpack.config.js However, it's not the perfect solution as obsolete assets would remain in the folder.但是,这不是完美的解决方案,因为过时的资产会保留在文件夹中。

output: {
  clean: {
    keep: /assets\//,
  },
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM