Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is exactly the importLoaders option of css-loader in Webpack 4?

Tags:

I've started learning Webpack 4 since a month. Most things I wanted to do is working great but this importLoaders option of css-loader is still a mystery for me. Its official documentation is poor and I haven't found any well explained writing about it.

My use case is very close to the one presented in the documentation:

{   test: /\.s?css$/,   use: [     ExtractCssChunks.loader,     {       loader: 'css-loader',       options: {         importLoaders: 2, // 0 => no loaders (default); 1 => postcss-loader; 2 => postcss-loader, sass-loader         import: true, // is true by default, but should I use instead false here???         url: false // let postcss do it       }     },     'postcss-loader',     'sass-loader'   ] } 

And my vendors.scss for example with different kind of imports:

@charset 'UTF-8';  // Google fonts @import url('https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700|Dosis:200,400,500,600');  //FontAwesome (from node_modules) @import "~@fortawesome/fontawesome-svg-core/styles";  // Site theme @import "theme/index"; 

Basically I want sass-loader to do its usual job and postcss to do some manipulation with image files.

So when and why should I use 0/1/2/n for importLoaders option?
How is it affected to my @imports above?

Could someone explain it to me more detailed like in the docs?
Thanks in advance.

like image 521
szegheo Avatar asked Sep 27 '18 20:09

szegheo


People also ask

What does CSS loader do 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.

How do I add CSS to webpack config?

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 does CSS loader do?

CSS Loader being a front-end component is defined as an npm Module that collects all the CSS files referenced in the working application to help webpack and consolidate into a string. This compiles an application of a particular resource as a JavaScript module (CSS to JS file).


1 Answers

After more searching it turned out that I'm not the only one confused about how to use this option correctly. Issues from the GitHub repo of css-loader:

https://github.com/webpack-contrib/css-loader/issues/765

Also see @guidobouman excellent explanation here:
https://github.com/webpack-contrib/css-loader/issues/228#issuecomment-312885975

So this answers my question (quoted literally):

importLoaders only has effect on unresolved @imports. So when using postCSS with nextCSS (no @import resolver) you'll want to set importLoaders. This way nextCSS will also be applied to imported .css files. But when using sass, it already handles the @import statements, so no importLoaders is required.

So, this only happens when css-loader finds an unresolved @import. When using sass-loader for example; All imports are resolved (and concatenated) before css-loader even gets the chance to look for an @import.

like image 69
szegheo Avatar answered Oct 22 '22 17:10

szegheo