Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Webpack 4热模块替换仅适用于样式表_Webpack - Fatal编程技术网

Webpack 4热模块替换仅适用于样式表

Webpack 4热模块替换仅适用于样式表,webpack,Webpack,我开始学习Webpack4,无法管理HMR工作。 我的结构如下: >dist index.html bundle.js >src >img >css style.scss >modules someModule.js index.html index.js 在package.json中,我有: "script": { "devStart": "webpack-dev-server --mode developme

我开始学习Webpack4,无法管理HMR工作。 我的结构如下:

>dist
  index.html
  bundle.js
>src
  >img
  >css
    style.scss
  >modules
    someModule.js
  index.html
  index.js
在package.json中,我有:

"script": {
    "devStart": "webpack-dev-server --mode development --open"
},
"devDependencies": {
    "@babel/core": "^7.4.0",
    "@babel/preset-env": "^7.4.2",
    "babel-loader": "^8.0.5",
    "css-loader": "^2.1.1",
    "file-loader": "^3.0.1",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^3.2.0",
    "mini-css-extract-plugin": "^0.5.0",
    "node-sass": "^4.11.0",
    "sass-loader": "^7.1.0",
    "style-loader": "^0.23.1",
    "webpack": "^4.29.4",
    "webpack-cli": "^3.2.3",
    "webpack-dev-server": "^3.2.1"
  }
在webpack.config.js中:

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'bundle.js',
        path: path.join(__dirname, 'dist')
    },
    devServer: {
        contentBase: path.join(__dirname, 'src'),
        port: 8888,
        inline: true,
        hot: true
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env']
                    }
                }
            },
            {
                test: /\.html$/,
                use: {
                    loader: 'html-loader',
                    options: {
                        minimize: true
                    }
                }
            },
            {
                test: /\.scss$/,
                use: [
                    { loader: 'style-loader' },
                    { loader: 'css-loader' },
                    { loader: 'sass-loader' }
                ]
            },
            {
                test: /\.(png|svg|jpg|jpeg|gif)$/,
                use: {
                    loader: 'file-loader'
                }
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html'
        }),
        new MiniCssExtractPlugin({
            filename: "[name].css",
            chunkFilename: "[id].css"
        }),
        new webpack.HotModuleReplacementPlugin()
    ]
};
我在index.js中导入样式,只在该文件envoke HMR中进行更改。 当我更改index.html时,我必须手动重新加载,并且当我更改模块或index.js文件时,会导致完全重新加载

当我将hotOnly设置为true时,会出现错误:

Ignored an update to unaccepted module ./src/index.js
[HMR] The following modules couldn't be hot updated: (They would need a full reload!)