简体   繁体   English

Webpack HMR无法加载CSS

[英]webpack HMR not loading css

A simple webpack configuration but not working : 一个简单的webpack配置,但是不起作用:

var path = require("path");
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  module: {
    rules: [
      {
        test:/\.css$/,
        use:['style-loader','css-loader']
      }
    ]
  },
    plugins: [
    new HtmlWebpackPlugin({
        hash: true,
        filename: 'index.html',
        title: 'My Awesome application',
        myPageHeader: 'Hello World',
        template: './src/index.html',
      })
    ],
    devServer: {
      port: 8000,
      hot: true
    },

};

When I run with "hot : false" it works, my CSS is loaded, but with "hot: true" I can see my html but the style is not loaded... 当我使用“ hot:false”运行时,它可以加载我的CSS,但是使用“ hot:true”,我可以看到我的html,但是样式尚未加载...

"webpack": "^4.16.0",
"webpack-cli": "^3.0.8",
"webpack-dev-server": "^3.1.4"

If I build with webpack and then open dist/index.html in my browser, it works too. 如果我使用webpack构建,然后在浏览器中打开dist / index.html,它也可以正常工作。

In index.js I imported my css like that : 在index.js中,我像这样导入了CSS:

import './styles/app.css';

~

我删除了hot: true并在脚本中添加了--hot选项,命令行版本自动添加了插件。

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

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