webpack sass-loader not generating a css file

前端 未结 2 812
我在风中等你
我在风中等你 2020-12-13 00:15

I can\'t figure out how to render a css file with the webpack sass-loader.

Here\'s what my webpackconfig.js looks like:

module.exports = {
  context:         


        
相关标签:
2条回答
  • 2020-12-13 00:56

    Using TypeScript and coming across similar issue, I discovered that the 'scss' file import should be in the index file that Webpack is starting at.

    Import in index.ts rather than in an inner module.

    0 讨论(0)
  • 2020-12-13 01:04

    You are using the style-loader, which, by default, embeds your CSS in Javascript and injects it at runtime.

    If you want real CSS files instead of CSS embedded in your Javascript, you should use the ExtractTextPlugin.

    A basic config would be:

    1. Add var ExtractTextPlugin = require('extract-text-webpack-plugin'); to the top of your Webpack config file.

    2. Add the following to your Webpack config:

      plugins: [
          new ExtractTextPlugin('[name].css'),
      ]
      
    3. Change your SASS loader config to the following:

      {
          test: /\.scss$/,
          loader: ExtractTextPlugin.extract(
              'style-loader', // backup loader when not building .css file
              'css-loader!sass-loader' // loaders to preprocess CSS
          )
      }
      

    What this does is extract all CSS it can find in your bundle to a separate file. The name will be based on your entrypoint name, which in your case will result in javascript.css (from the entry part of your config).

    The ExtractTextPlugin.extract-loader is used by the plugin to find the CSS in your code and put it in separate files. The first parameter you give it is the loader it should fall back to if it encounters files in an async module, for example. Generally this is pretty much always style-loader. The second parameter tells the plugin what loaders to use to process the CSS, in this case css-loader and sass-loader, but things like postcss-loader are often used too.

    More info on building your CSS with Webpack can be found here: https://webpack.github.io/docs/stylesheets.html#separate-css-bundle

    0 讨论(0)
提交回复
热议问题