Media Query does not work with CSS / Style loader and Webpack3

I use css-loaderit style-loaderfor my CSS too, but all media queries don't work. I use "webpack": "^3.4.1", "css-loader": "^0.28.4"and "style-loader": "^0.18.2".

This is my webpack configuration:

const ExtractTextPlugin = require('extract-text-webpack-plugin')

rules: [{
  test: /\.css$/,
  use: [{
    loader: 'style-loader'
  }, {
    loader: 'css-loader',
    options: {
      modules: true,
      localIdentName: '[name]-[local]-[hash:base64:6]',
      camelCase: true
    }
  }]
}]
...
plugins: [
  new ExtractTextPlugin({
    filename: 'style.[chunkhash:6].css',
    allChunks: true
  })
]

My css file looks something like this:

.container{
  position: relative;
  margin: 30px 15px;
  padding: 50px 15px;
  background: #fff;
}
@media (max-width: 768px) {
  .container{
    background: #fbfbfb;
  }
}

and I import this CSS file into code Reactas follows:

import styles from './Component.css'
+4
source share
2 answers

try using this code

.container{
  position: relative;
  margin: 30px 15px;
  padding: 50px 15px;
  background: #fff;
}
@media only screen and (max-width:768px){
  .container{
    background: #c00;
  }
}
<div class="container">
content her
</div>
Run codeHide result
+1
source

I think the problem is that you declared ExtractTextPlugin, but use the css and style loader instead.

Take a look at this setting for reference:

plugins

 plugins: [
    new ExtractTextPlugin({ 
      filename: "css/bundle.css", 
      allChunks: true,
      disable: process.env.NODE_ENV !== 'production'
    }),
  ]

Loader

  {
    test: /\.css$/,
    loader: ExtractTextPlugin.extract({
      fallback: 'style-loader',
      use: 'css-loader?importLoaders=1'
    })
  }

ExtractTextPlugin webpack-dev-, ETP .

0

Source: https://habr.com/ru/post/1682722/


All Articles