Webpack sass where is the css file

36,949

Solution 1

By default, the style-loader inlines the compiled css into your bundle, which are added to the head of the page with the output file e.g. bundle.js. Using the extract-text-webpack-plugin you can remove the compiled css from the bundle, and export it to a separate file.

First - wrap your loader in the plugin:

 loaders: [{
  test: /\.scss$/,
  loader: ExtractTextPlugin.extract(
    "style",
    "css!sass")
 }]
},

Then tell the plugin what to call the file it generates:

plugins: [
    new ExtractTextPlugin("app.css")
 ]

Include this file in your HTML normally.

Solution 2

If you want a separate CSS file when using Webpack, you need to use the extract-text-webpack-plugin.

Share:
36,949
Admin
Author by

Admin

Updated on July 08, 2022

Comments

  • Admin
    Admin almost 2 years

    I am using web pack with sass loader like this:

    module.exports = {
      module: {
        loaders: [
          {
            test: /\.scss$/,
            loader: "style!css!sass"
          }
        ]
      }
    };
    

    But i see the styles apply to the style tag, where is the generate css file?

  • TetraDev
    TetraDev about 8 years
    Be warned that this kills Hot Module Replacement functionality
  • adam-beck
    adam-beck almost 8 years
    Yes this will kill HMR but don't forget you don't need to do this every time. You could only run this plugin in a production webpack configuration.
  • angry kiwi
    angry kiwi almost 8 years
    @adc do we really need style loader in this case? it could be just loader: ExtractTextPlugin.extract( "css!sass") }]
  • adam-beck
    adam-beck almost 8 years
    I don't think that's how the ExtractText plugin works. You would still need to include the style loader.
  • shicky
    shicky almost 8 years
    I have bunch of scss files. Is there a way for me to create bunch of css files from them?
  • Paweł
    Paweł almost 7 years
    @adc You said include file in html normally, what if I use extractTextPlugin in production mode and style-loder in dev mode? is there any way to add <link href="extracted.css"> tag automatically in production mode?