Webpack 4: SCSS to CSS into separate file

前端 未结 2 397
名媛妹妹
名媛妹妹 2021-02-07 19:27

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
2条回答
  •  抹茶落季
    2021-02-07 19:47

    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.

提交回复
热议问题