Page styles break when I change styles in Chrome DevTools with Webpack HMR
Asked Answered
C

2

13

I have a strange problem:

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 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.

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

Thank you in advance!

P.S. Also I use SASS with 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'
  }
}
Clementina answered 23/1, 2018 at 17:46 Comment(5)
there are no screenshots @EvgeniyNovalovBirl
@TheOneWhoMade sorry, I added screenshotClementina
Have you tried the answers given here: #49373217Udder
It's hard to tell without seeing where your SCSS is generated i.e. Vue SFC vs require in main entry. But I see your missing vue-style-loader in your webpack.base.conf.js - should be the first item in the use array. Hope that helpsKenzi
I was having the same issue in a project where I was using the Nuxt framework and it turned out to be an issue caused by source maps. I disabled the scss source maps and I was able to use the dev tools without any problems again.Cichlid
P
10

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

css: {
    loaderOptions: {
        scss: {
            sourceMap: false
        }
    }
}
Pemberton answered 15/7, 2020 at 15:30 Comment(4)
Thanks. WARNING: Be sure to double check that if you are using .scss files, which most people are, the sass property should be scss. I was scratching my head wondering why this wasn't working at firstJeroldjeroma
Please tell me there's a nuxt js equivalent to this - I tried adding it to the build property in nuxt.config.js but that didn't work :(Chalcanthite
Nevermind, found the nuxt equivalent - build:{loaders:{scss:{sourceMap:false},},} . Source: https://mcmap.net/q/816128/-issue-with-dev-tools-style-editing-in-nuxt-jsChalcanthite
Thanks @OzzyTheGiant, makes sense, I updated the answer so that the property is "scss" instead of "sass".Pemberton
M
2

I have the same problem with NUXT project. reinstall of sass-loader / sass in the last version was solve the problem.

Misadventure answered 22/8, 2021 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.