Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack not loading css

Tags:

This is my first time trying to set up Webpack, so I'm sure I'm missing something here.

I am trying to load my PostCSS files with Webpack, using the ExtractTextPlugin to generate a css file into "dist". The problem is Webpack does not seem to pick up the css files. They are under "client/styles", but I've tried moving them to "shared", with the same result.

I ran Webpack with the --display-modules option, and verified that no css files display there.

I have tried running it without the extract text plugin, and the result is the same: no CSS is built into bundle.js.

Here is my prod config:

var ExtractTextPlugin = require('extract-text-webpack-plugin'); var path = require('path');  module.exports = {   entry: [     './client'   ],   resolve: {     modulesDirectories: ['node_modules', 'shared'],     extensions: ['', '.js', '.jsx', '.css']   },   output: {     path: path.join(__dirname, 'dist'),     filename: 'bundle.js',     chunkFilename: '[id].js',     publicPath: '/'   },   module: {     loaders: [       {         test:    /\.jsx?$/,         exclude: /node_modules/,         loaders: ['babel']       },        {         test: /\.css?$/,         loader: ExtractTextPlugin.extract(           'style-loader',           'css-loader!postcss-loader'         ),         exclude: /node_modules/       }     ]   },   plugins: [     new ExtractTextPlugin('[name].css')   ],   postcss: (webpack) => [     require('postcss-import')({ addDependencyTo: webpack, path: ['node_modules', 'client'] }),     require('postcss-url')(),     require('precss')(),     require('postcss-fontpath')(),     require('autoprefixer')({ browsers: [ 'last 2 versions' ] })   ] }; 

And here's an example of my main css file: @import 'normalize.css/normalize';

/* Variables */ @import 'variables/colours';  /* Mixins */  /* App */  /* Components */  body {   background-color: $black; } 

Would anyone have an idea on why this is happening? Am I missing something?

Thank you

like image 840
Claudia Avatar asked Jan 23 '16 11:01

Claudia


People also ask

How do I load CSS into webpack?

To be able to use CSS in your webpack app, you need to set up a new loader. Out-of-the-box, webpack only understands Javascript and JSON. With a loader, you can translate another type of file to a format that webpack understands and can work with. There are many webpack loaders and each loader has a specific purpose.

How do I bundle CSS with webpack?

By default, Webpack will inline your CSS as Javascript tags that injects the CSS into the page. This sounds strange but it lets you do hot reloading in development. In production you extract them to a file using the ExtractTextPlugin, and require it with a normal link tag.

What is style-loader in webpack?

Loaders are the node-based utilities built for webpack to help webpack to compile and/or transform a given type of resource that can be bundled as a javascript module. css-loader is the npm module that would help webpack to collect CSS from all the css files referenced in your application and put it into a string.


1 Answers

So, after three hours of hitting my head against the wall, I finally got it. I hope this will help someone in the future.

All I needed to do was to add './client/styles/main.css' to the entry points. Yey.

like image 161
Claudia Avatar answered Nov 05 '22 16:11

Claudia