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

标签 css webpack vue.js google-chrome-devtools webpack-hmr

我有一个奇怪的问题:

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

我知道问题出在热重装Webpack中,因为保留了某些Vue组件样式,而某些样式已删除。因此,我无法更改侧边栏中的样式,并且每次都必须重新加载页面才能恢复样式。

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

先感谢您!

P.S.我也将SASS与SASS-Loader一起使用。

enter image description here

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'
  }
}

最佳答案

解决此问题的一种方法是将sourceMap中的Sass falseloaderOptions设置为vue.config.js:

css: {
    loaderOptions: {
        sass: {
            sourceMap: false
        }
    }
}

关于css - 当我使用Webpack HMR在Chrome DevTools中更改样式时,页面样式会中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48407862/

相关文章:

jquery - css/jquery - 将覆盖添加到页面的一部分

javascript - 为什么 Range.getBoundingClientRect() 会为 textarea 内的范围返回 0?

webpack - 使用 Webpack 导入 JavaScript 文件

javascript - 将 Play Framework 和 Twirl 模板与 Webpack 和 React 集成

html - 如何使 vue.js 组件中 div 框的背景图像透明?

html - 视差滚动效果有效,但我的图像不会固定到两侧和页面顶部

html - 使textarea填充表格中整个单元格的方法

javascript - 在 Vuex 中存储 JavaScript 对象并对其进行修改 - 概念

javascript - 未知插件中的警告 : imageminSvgo. 您是否忘记安装插件?

javascript - 403(CORS 未启用或未找到此请求的匹配规则。)[VueJs]