当我使用Webpack HMR在Chrome DevTools中更改样式时,页面样式会中断 [英] Page styles break when I change styles in Chrome DevTools with Webpack HMR

查看:90
本文介绍了当我使用Webpack HMR在Chrome DevTools中更改样式时,页面样式会中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题:

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

I'm using Webpack (with Vue-CLI) + HMR. When I try to change styles in the browser in DevTools, then my page itself changes the styles - it removes some of them (screenshots below).

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

I understand that the problem is in the Hot Reload Webpack, because some Vue-Components styles remain, and some are deleted. So I can not change the styles in the sidebar and I have to reload the page every time to get the styles back in place.

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

Below is added my package.json and webpack.base.conf.js.

谢谢!

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

P.S. Also I use SASS with SASS-Loader.

{
  "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



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 false 来表示 vue.config.js中的无聊的 loaderOptions

One way to fix this is setting sourceMap to false for the sass loaderOptions in vue.config.js:

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

这篇关于当我使用Webpack HMR在Chrome DevTools中更改样式时,页面样式会中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆