2
foo.js
foo.css
bar.css

foo.js includes foo.css.
And foo.css includes bar.css.

As result, I have bundle with bar.css. I want to exclude it. I am also using MiniCssExtractPlugin since I nee to separate css and js files.

What I already tried:

  • IgnorePlugin. It removes bar, but then MiniCssExtractPlugin fails because of it.
  • null-loader for bar.css ( I tried both test with regexp and include/exclude). It simply doesn't work (it could be that I miss something.
  • Two oneOf rules for /\.css$/: for with null-loader and another with everything else.

Nothing works.

What is the correct way to achieve it?

My webpack.config.js

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

module.exports = {
  plugins: [
    new MiniCssExtractPlugin({ }),
    new webpack.IgnorePlugin(/bar\.css$/)
  ],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
          },
          'css-loader',
        ],
      },
    ],
  },
};

And the output

ERROR in ./src/foo.css
Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
Error: Cannot find module '-!../node_modules/css-loader/dist/cjs.js!./bar.css'

It seems that MiniCssExtractPlugin uses css-loader explicitly, so I can't skip module loading with configuration.

7
  • Can you include your webpack.config.js please? Commented Oct 9, 2019 at 0:25
  • can you try new webpack.IgnorePlugin(/bar\.css$/); Commented Oct 9, 2019 at 0:30
  • Possible duplicate of Exclude some css files in webpack Commented Oct 9, 2019 at 0:30
  • @DacreDenny I added it. Commented Oct 9, 2019 at 1:14
  • @DerekLawrence I tried, please check updated version of question Commented Oct 9, 2019 at 1:14

1 Answer 1

1

Solution is to use NormalModuleReplacementPlugin to replace bad file with empty one

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

const pluginToIgnore = /bad_css\.css$/;
module.exports = {
    plugins: [
        new MiniCssExtractPlugin({}),
        new webpack.NormalModuleReplacementPlugin(
            pluginToIgnore,
            '!./empty.css'
        )
    ],
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader']
            },
        ],
    },
};
Sign up to request clarification or add additional context in comments.

1 Comment

trying to do the same but replacing scss files but its not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.