G
G
ge2021-01-31 07:53:02
webpack
ge, 2021-01-31 07:53:02

How to minify multiple CSS with Webpack without concatenating them?

I have a bunch of CSS files that I want to minify. I can combine all CSS into one, but I don't need to. Only some files should be merged. Google did not give a clear answer, I did not find the answer in the documentation. It should

look like this:

main.css , codehilite.css --> style.min.css
light.css --> light.min.css
dark.css --> dark.min.css

one and the others don't. In general, I'm looking for a way to combine different groups of files into different final files.

I have the following config:

webpack.config.js

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');

module.exports = {
    entry: {
        'bundle.js': [
            path.resolve(__dirname, 'src/index.js')
        ]
    },
    output: {
        filename: '[name]',
        path: path.resolve(__dirname, 'build'),
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader'],
            },
            {
                test: /\.(woff|woff2|eot|ttf|svg)$/,
                use: {loader: 'url-loader',},
            }
        ],
    },
    optimization: {
        minimize: true,
        minimizer: [
            new CssMinimizerPlugin({
                minimizerOptions: {
                    preset: [
                        'default',
                    {
                        discardComments: { removeAll: true },
                    },
                    ],
                },
            }),
        ],
    },
    plugins: [
    new MiniCssExtractPlugin({
      filename: 'style.min.css'
    })
  ]
};

I import the CSS itself in the src/index.js file and they are glued into one file in the specified order:
index.js (the whole file is here)

import './css/main.css';
import './css/codehilite.css';
import './css/light.css';
import './css/dark.css';


I would appreciate any help or hints :3

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VegasChickiChicki, 2021-01-31
@gedev

You can create separate points and import the necessary css files into them.

entry: {
     'bundle.js': [
         path.resolve(__dirname, 'src/index.js')
     ],
     'light.js': [
         path.resolve(__dirname, 'src/light.js')
     ],
     'dark.js': [
         path.resolve(__dirname, 'src/dark.js')
     ],
 },

The output should be 3 files:
bundle.css, light.css, dark.css
It should work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question