Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css Loader vs Style Loader Vs Sass-Loader

I was little confused in differentiating sass-loader and css-loader while using import statement. As per my knowledge css loader resolve import statment(@import) and style-loader works on injecting style dynamically on your page. I am also using sass-loader in my app. So I was confused on sass-loader functionality. Does it also resolve the import statement along with converting sass to css. In that case I don't need css-loader if I am using only sass files on my project because that job will be done by sass-loader. Am I right here?. Could anyone please highlight on this. Any help would be much appreciated

like image 926
Bharat Sewani Avatar asked May 11 '17 10:05

Bharat Sewani


People also ask

What is css-loader and style loader?

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. And then style-loader would take the output string generated by the above css-loader and put it inside the <style> tags in the index. html file.

What is sass-loader?

sass-loader is a loader for Webpack for compiling SCSS/Sass files. style-loader injects our styles into our DOM. css-loader interprets @import and @url() and resolves them. mini-css-extract-plugin extracts our CSS out of the JavaScript bundle into a separate file, essential for production builds.

What is the use of style loader?

The style loader takes CSS and actually inserts it into the page so that the styles are active on the page.

Does webpack compile SCSS?

New! Save questions or answers and organize your favorite content. Learn more.


1 Answers

The sass-loader will resolve the @import statements and include the imported Sass in the resulting CSS, hence the resulting CSS will probably not have any import statements. But the css-loader does not just handle imports. The three loaders you mention, do very different things and they are meant to be used together, although there are other loaders you could use, which would give a similar result.

  1. sass-loader - Transforms Sass to CSS. (Input: Sass, Output: CSS)
  2. css-loader - Transforms CSS to a JavaScript module. (Input: CSS, Output: JavaScript)
  3. style-loader - Injects the CSS, that is exported by the JavaScript module, into a <style> tag at runtime. (Input: JavaScript, Output: JavaScript).

Note: The output of style-loader is only relevant if you are using CSS modules, which passes on the object that maps class names to local identifiers.

You can't use sass-loader without any loader that transforms CSS to JavaScript, but you could potentially use raw-loader, which just reads in the content of the module as a string, but then you might lose some transformations that the css-loader does (e.g. CSS modules or minification).

like image 160
Michael Jungo Avatar answered Sep 24 '22 14:09

Michael Jungo