Webpack - CSS not applicable

I decided to try it in Webpack 2. I am trying to bundle js and css.

The problem is that CSS does not apply elements to the page (present in the embedded file).

This is the structure of the application:

app
  |-styles.css
  |-app.js
build
  |-bundle.js
index.html

webpack configuration file:

var path = require('path');

module.exports = {
    entry: './app/app.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'build')
    },
    module: {
       rules: [
          {
            test: /\.css$/, 
            use: 'css-loader'
          }
      ]
   }
}

index.html

<html>
<head>
   <script src="./build/bundle.js"></script>
</head>
<body>
    <div class="abc"> hello </div>
</body>

app.js:

require('./styles.css');
console.log('js loaded!');

When I run the build command, getting this output:

[0] ./app/styles.css 240 bytes {0} [built] [1] ./~/css-loader/lib/css-base.js 1.51 kB {0} [built] [2] ./app/app.js 148 bytes {0} [built]

I also see that css is included in the bundle.js file

exports.push([module.i, ".abc {\r\n    width: 300px;\r\n    height: 100px;\r\n    background-color: red;\r\n}", ""]);

If I include the css file in html, it works fine, so I think there is no spelling error in CSS. I spent a lot of time trying to figure it out. Is there something I am missing?

+4
source share
1 answer

CSS css-loader. style-loader, CSS DOM .

use: [ 'style-loader', 'css-loader' ]
+6

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


All Articles