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

13
我有一个奇怪的问题:
我正在使用Webpack(带有Vue-CLI)+ HMR。当我尝试在DevTools中更改样式时,我的页面本身会更改样式-它会删除其中一些样式(如下图所示)。
我知道问题在于Hot Reload 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'
  }
}

没有屏幕截图 @EvgeniyNovalov - user9107868
@TheOneWhoMade 抱歉,我添加了截图。 - Evgeniy Novalov
你尝试过这里给出的答案吗:https://dev59.com/gVUM5IYBdhLWcg3wEM_G - Kordonme
很难在没有看到你的 SCSS 生成位置的情况下进行判断,例如 Vue SFC vs require in main entry。但是我发现你的 webpack.base.conf.js 中缺少了 vue-style-loader - 应该是 use 数组中的第一项。希望这可以帮到你。 - jaredrethman
我在使用Nuxt框架的一个项目中遇到了同样的问题,后来发现这是由源映射引起的问题。我禁用了scss的源映射,然后再次顺利地使用了开发工具。 - Jair Reina
2个回答

10
设置 `vue.config.js` 中 sass 的 `loaderOptions`,将 `sourceMap` 设为 `false` 可以解决此问题。
css: {
    loaderOptions: {
        scss: {
            sourceMap: false
        }
    }
}

1
谨慎注意:如果您正在使用.scss文件(大多数人都是这样),则sass属性应该是scss。一开始我也在纳闷为什么这个不起作用。谢谢。 - OzzyTheGiant
请问是否有nuxt js的等效方法 - 我尝试将其添加到nuxt.config.js中的“build”属性中,但没有成功:( - SeriousLee
2
没事了,找到了 Nuxt 的等效方式 - build:{loaders:{scss:{sourceMap:false},},}。来源:https://dev59.com/AlQJ5IYBdhLWcg3wf2Ed#58945358 - SeriousLee
谢谢@OzzyTheGiant,我明白了,我已经更新了答案,属性现在是"scss"而不是"sass"。 - maesk

2

我在NUXT项目中也遇到了同样的问题。重新安装最新版本的sass-loader / sass解决了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接