Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack 4: SCSS to CSS into separate file

Tags:

webpack

I would like to use Webpack 4 to transpile on one side my ES6 Javascript separately from my Sass:

  • src/js/index.js → static/js/index.js
  • src/css/style.scss → static/css/style.css

Currently my webpack configuration seems to correctly transpile the javascript into a bundle.js but I cannot get my SCSS to transpile to CSS correctly.

I would definitely try to debug somehow but since I'm very ignorant on Webpack internals I'm not sure how to do it.

Following my webpack.config.js:

const ExtractTextPlugin = require('extract-text-webpack-plugin');
const path = require('path');

module.exports = {

  mode: 'development',

  entry: {
    bundle: './src/js/index.js',
  },

  output: {
    filename: '[name].js',
    path: path.resolve('static/js')
  },

  module: {
    rules: [{
      test: /\.scss$/,
      use: ExtractTextPlugin.extract({
        fallback: 'style-loader',
        use: [
          { loader: "css-loader" },     
          { 
            loader: "sass-loader",
            options: {
              includePaths: [
                path.resolve("./src/css")
              ]
            }
          },
        ]
      }),
    }]
  },

  plugins: [
    new ExtractTextPlugin({
      filename: path.resolve('static/css/style.css')
    })
  ],
}
like image 526
Francesco Meli Avatar asked Apr 12 '18 14:04

Francesco Meli


People also ask

Can I use SCSS in CSS file?

scss files, Sass can import plain old . css files. The only rule is that the import must not explicitly include the . css extension, because that's used to indicate a plain CSS @import .

Can webpack process CSS files?

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 a CSS file in 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.


2 Answers

Try mini-css-extract-plugin for webpack v4.

I created a separate webpack config file that looks like ...

const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  entry: './server/styles/styles.scss',
  output: {
    path: path.resolve(__dirname, 'public')
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: "styles.css"
    })
  ],
  module: {
    rules: [
      {
        test: /\.s?css$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'sass-loader'
        ]
      }
    ]
  }
}

Not sure why, but it's also generating a javascript file with the required css bundle.

like image 186
Ritwik Avatar answered Oct 21 '22 06:10

Ritwik


extract-text-webpack-plugin does not play well with webpack 4.

According to Michael Ciniawsky:

extract-text-webpack-plugin reached a point where maintaining it become too much of a burden and it’s not the first time upgrading a major webpack version was complicated and cumbersome due to issues with it

mini-css-extract-plugin is here to overcome those issues.

see here for more information about this topic

like image 32
pyran1208 Avatar answered Oct 21 '22 05:10

pyran1208