当我使用 Webpack HMR 在 Chrome DevTools 中更改样式时,页面样式会中断

Posted

技术标签:

【中文标题】当我使用 Webpack HMR 在 Chrome DevTools 中更改样式时,页面样式会中断【英文标题】:Page styles break when I change styles in Chrome DevTools with Webpack HMR 【发布时间】:2018-07-02 15:15:04 【问题描述】:

我有一个奇怪的问题:

我正在使用 Webpack(带有 Vue-CLI)+ HMR。 当我尝试在 DevTools 中更改浏览器中的样式时,我的页面本身会更改样式 - 它会删除其中的一些样式(下面的屏幕截图)。

我知道问题出在 Hot Reload Webpack 中,因为一些 Vue-Components 样式仍然存在,而一些被删除了。因此,我无法更改侧边栏中的样式,并且每次都必须重新加载页面才能使样式恢复原状。

下面添加了我的 package.json 和 webpack.base.conf.js。

提前谢谢你!

附:我也将 SASS 与 SASS-Loader 一起使用。

package.json


  "private": true,
  "scripts": 
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "build": "node build/build.js"
  ,
  "dependencies": 
    "bootstrap": "^4.0.0",
    "desandro-classie": "^1.0.1",
    "desandro-get-style-property": "^1.0.4",
    "draggabilly": "^2.1.1",
    "jquery": "^3.2.1",
    "jquery-parallax.js": "^1.5.0",
    "popper.js": "^1.12.9",
    "vue": "^2.5.2",
    "vue-router": "^3.0.1"
  ,
  "devDependencies": 
    "autoprefixer": "^7.1.2",
    "babel-core": "^6.22.1",
    "babel-helper-vue-jsx-merge-props": "^2.0.3",
    "babel-loader": "^7.1.1",
    "babel-plugin-syntax-jsx": "^6.18.0",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-plugin-transform-vue-jsx": "^3.5.0",
    "babel-preset-env": "^1.3.2",
    "babel-preset-stage-2": "^6.22.0",
    "chalk": "^2.0.1",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^0.28.0",
    "extract-text-webpack-plugin": "^3.0.0",
    "file-loader": "^1.1.4",
    "friendly-errors-webpack-plugin": "^1.6.1",
    "html-webpack-plugin": "^2.30.1",
    "imports-loader": "^0.7.1",
    "modernizr-webpack-plugin": "^1.0.6",
    "node-notifier": "^5.1.2",
    "node-sass": "^4.7.2",
    "optimize-css-assets-webpack-plugin": "^3.2.0",
    "ora": "^1.2.0",
    "portfinder": "^1.0.13",
    "postcss-import": "^11.0.0",
    "postcss-loader": "^2.0.8",
    "postcss-url": "^7.2.1",
    "rimraf": "^2.6.0",
    "sass-loader": "^6.0.6",
    "semver": "^5.3.0",
    "shelljs": "^0.7.6",
    "uglifyjs-webpack-plugin": "^1.1.1",
    "url-loader": "^0.5.8",
    "vue-loader": "^13.3.0",
    "vue-style-loader": "^3.0.1",
    "vue-template-compiler": "^2.5.2",
    "webpack": "^3.6.0",
    "webpack-bundle-analyzer": "^2.9.0",
    "webpack-dev-server": "^2.9.1",
    "webpack-merge": "^4.1.0"
  ,
  "engines": 
    "node": ">= 6.0.0",
    "npm": ">= 3.0.0"
  ,
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]

webpack.base.conf.js

'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')
const ModernizrWebpackPlugin = require('modernizr-webpack-plugin')
const webpack = require('webpack')

let modernizrConfig = 
  "options": [
    "prefixed",
    // "prefixedCSS",
    // "testStyles",
    "testAllProps",
    "testProp",
    "html5shiv",
    "domPrefixes"
  ]


function resolve (dir) 
  return path.join(__dirname, '..', dir)


module.exports = 
  context: path.resolve(__dirname, '../'),
  entry: 
    app: './src/main.js'
  ,
  output: 
    path: config.build.assetsRoot,
    filename: '[name].js',
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath
  ,
  resolve: 
    extensions: ['.js', '.vue', '.json'],
    alias: 
      'vue$': 'vue/dist/vue.esm.js',
      '@': resolve('src'),
    
  ,
  module: 
    rules: [
      
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      ,
      
        test: /\.js$/,
        loader: 'babel-loader',
        include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
      ,
      
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: 
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        
      ,
      
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'url-loader',
        options: 
          limit: 10000,
          name: utils.assetsPath('media/[name].[hash:7].[ext]')
        
      ,
      
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: 
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        
      ,
      
        test: /\.scss$/,
        use: [
          'style-loader',
          'css-loader',
          'sass-loader'
        ]
      ,
      
        test: /[\/\\]node_modules[\/\\]some-module[\/\\]index\.js$/,
        loader: "imports-loader?this=>window"
      
    ]
  ,
  plugins: [
    new ModernizrWebpackPlugin(modernizrConfig),
    new webpack.ProvidePlugin(
      Draggabilly: 'draggabilly',
      $: "jquery",
      jQuery: "jquery",
      "window.jQuery": "jquery"
    )
  ],
  node: 
    // prevent webpack from injecting useless setImmediate polyfill because Vue
    // source contains it (although only uses it if it's native).
    setImmediate: false,
    // prevent webpack from injecting mocks to Node native modules
    // that does not make sense for the client
    dgram: 'empty',
    fs: 'empty',
    net: 'empty',
    tls: 'empty',
    child_process: 'empty'
  

【问题讨论】:

没有截图@EvgeniyNovalov @TheOneWhoMade 抱歉,我添加了截图 您是否尝试过这里给出的答案:***.com/questions/49372717/… 如果不查看 SCSS 是在哪里生成的,例如 Vue SFC 与 main 条目中的 require,很难判断。但是我在您的webpack.base.conf.js 中看到您缺少的vue-style-loader - 应该是使用数组中的第一项。希望有帮助 我在使用 Nuxt 框架的项目中遇到了同样的问题,结果证明是源映射引起的问题。我禁用了 scss 源映射,我能够再次使用开发工具而没有任何问题。 【参考方案1】:

我对 NUXT 项目有同样的问题。在上一个版本中重新安装 sass-loader / sass 解决了这个问题。

【讨论】:

【参考方案2】:

解决此问题的一种方法是将sourceMap 设置为false 用于vue.config.js 中的sass loaderOptions

css: 
    loaderOptions: 
        sass: 
            sourceMap: false
        
    

【讨论】:

谢谢。警告:请务必仔细检查,如果您使用的是.scss 文件(大多数人都在使用),那么 sass 属性应该是 scss。我在挠头,想知道为什么一开始这不起作用

以上是关于当我使用 Webpack HMR 在 Chrome DevTools 中更改样式时,页面样式会中断的主要内容,如果未能解决你的问题,请参考以下文章

React + Webpack HMR 正在刷新页面(不是热加载)

未捕获的错误:[HMR] 热模块更换已禁用

Webpack hmr: __webpack_hmr 404 not found

HTML Webpack 插件和 HMR

Webpack - [HMR] 和 [WDS] 触发两次

Webpack HMR 原理解析