Webpack 4 splitChunks generating multiple css

Viewed 6503

I'm trying to generate multiple css using webpack 4 min-css-extract-plugin and splitChunks plugin.

Here is my webpack config.

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

module.exports = [{
  entry: './index.js',
  output: {
    filename: '[name].js',
    path: path.resolve(path.join(__dirname, "./dist")),
  },
  optimization: {
    splitChunks: {
      cacheGroups: {
        vendor: false,
        commons: {
          name: 'commons',
          test: /.styl$/,
          chunks: 'all',
          enforce: true,
          minChunks: 1,
        },
        englishStyle: {
          name: 'styles_en',
          test: (c) => {
            return c.type.match(/mini-css-extract-plugin/) && c._identifier.indexOf('_ar') === -1;
          },
          chunks: 'all',
          priority: 1,
          enforce: true,
        },
        arabicStyles: {
          name: 'styles_ar',
          test: (c) => {
            return c.type.match(/mini-css-extract-plugin/) && c._identifier.indexOf('_ar') !== -1;
          },
          priority: 1,
          chunks: 'all',
          enforce: true,
        }
      }
    }
  },
  module: {
    rules: [
      {
        test: /\.styl$/,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader",
          "stylus-loader"
        ],
      },
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[name].css"
    })
  ]
}]

and here is my css file structure.

common.styl
  ...

style.styl 
  @import common.styl
  ...

style_ar.styl
  @import common.styl
  ...

index.js
 import styles from style.styl
 import styles from style_ar.styl

The above configuration is generating only two files styles_ar.css and style.css. with common content in both the files.

How do I generate a separate a file for common file?

If I give priority to commons cacheGroup It will generate only one file commons.styl.

1 Answers

This is a kind of "negative" answer but if you will point me that I miss something - I will drop it.

1) css though MiniCssExtractPlugin is a kind of trick that people uses to do not create gulp file for generating css files - they want just to keep everithing in one place (and write less code). why this is possible - all is ok - but do not forget this is still a trick.

Is this your motivation? Or in real life you need to include highly specific webpack plugins into css production process? I'm not talking about autoprefixer - you can use post-css from gulp also. But something really webpack specific? May be some webpack runtime features?

2) This is not a good idea to "force" a trick to be something more than trick.

Note: we are not cover the case when you want to create "one mixed bundle for css and js" - that means use css-loader without MiniCssExtractPlugin - it would be a totally different story.

So the answer is: use stylus directly to generate a chunked css you want.

Or try "more heavy tricks": https://github.com/faceyspacey/extract-css-chunks-webpack-plugin

Related