npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

filter-chunk-webpack-plugin

v3.0.0

Published

Webpack plugin that filters chunks from output

Readme

filter-chunk-webpack-plugin

Filter and exclude assets from webpack output based on flexible pattern rules

npm Build Status Coverage Status PRs Welcome

This webpack plugin allows you to filter assets from the final output before they are emitted to disk. Assets are still processed and bundled normally - only the final file emission is prevented. This is useful for:

  • Removing source maps from production builds
  • Excluding specific file types from output
  • Conditional asset filtering based on custom logic
  • Creating builds with different asset configurations

Installation

npm install filter-chunk-webpack-plugin --save-dev

Requirements

| Requirement | Version | | ----------- | ------- | | Node.js | >= 20 | | webpack | >= 5 |

Usage

ESM (Recommended)

import FilterChunkWebpackPlugin from 'filter-chunk-webpack-plugin';

CommonJS

const FilterChunkWebpackPlugin = require('filter-chunk-webpack-plugin');

Basic Example

import FilterChunkWebpackPlugin from 'filter-chunk-webpack-plugin';

export default {
  // ... webpack config
  plugins: [
    new FilterChunkWebpackPlugin({
      rules: [
        {
          patterns: '*.map',
        },
      ],
    }),
  ],
};

Configuration

Plugin Options

| Option | Type | Default | Description | | ------- | -------------------------- | ----------- | --------------------------------------------------------------- | | mode | 'exclude' \| 'include' | 'exclude' | Filter mode: exclude matched assets or include only matched | | rules | Rule[] | [] | Array of rules defining which assets to filter | | debug | boolean | false | Enable debug logging to see filtered assets |

Mode

The mode option determines how rules are applied:

  • exclude (default): Assets matching any rule are removed from output. Rules are processed as a pipeline - each rule filters from the remaining assets.
  • include: Only assets matching at least one rule are kept in output. Rules are combined with OR logic (union) - an asset is kept if any rule matches it.

Rule Properties

| Property | Type | Required | Description | | ---------- | ---------------------------- | -------- | ----------------------------------------------------- | | patterns | Pattern \| Pattern[] | Yes | Pattern(s) to match against asset filenames | | test | RegExp | No | Pre-filter to limit which assets the rule applies to | | label | string | No | Label for debug output to identify the rule |

Pattern Types

Patterns can be one of three types:

| Type | Example | Description | | ---------- | ------------------------------------ | ------------------------------------- | | string | '*.map', 'assets/**/*.png' | Glob pattern using picomatch syntax | | RegExp | /\.map$/, /^vendor\..+\.js$/ | Regular expression | | function | (filename, asset) => boolean | Custom function (can be async) |

Examples

Filter Source Maps

Remove all .map files from output:

new FilterChunkWebpackPlugin({
  rules: [
    {
      patterns: '*.map',
    },
  ],
});

Filter Multiple File Types

Remove multiple file types using an array of patterns:

new FilterChunkWebpackPlugin({
  rules: [
    {
      patterns: ['*.map', '*.txt', 'LICENSE*'],
    },
  ],
});

Using Regular Expressions

Filter using regex patterns:

new FilterChunkWebpackPlugin({
  rules: [
    {
      patterns: /\.map$/,
    },
    {
      patterns: /^vendor\..+\.js$/,
    },
  ],
});

Custom Function Filter

Use a function for complex filtering logic:

new FilterChunkWebpackPlugin({
  rules: [
    {
      patterns: (filename, asset) => {
        // Filter assets larger than 1MB
        return asset.size() > 1024 * 1024;
      },
    },
  ],
});

Async Function Filter

Functions can also be async:

new FilterChunkWebpackPlugin({
  rules: [
    {
      patterns: async (filename, asset) => {
        const content = asset.source();
        // Perform async validation
        return await someAsyncCheck(content);
      },
    },
  ],
});

Scoped Filtering with Test

Use test to limit which files a rule applies to:

new FilterChunkWebpackPlugin({
  rules: [
    {
      // Only apply to files in the assets directory
      test: /^assets\//,
      patterns: ['*.png', '*.jpg'],
    },
    {
      // Only apply to JavaScript files
      test: /\.js$/,
      patterns: /^vendor\./,
    },
  ],
});

Debug Mode

Enable debug mode to see which assets are being filtered:

new FilterChunkWebpackPlugin({
  debug: true,
  rules: [
    {
      label: 'source-maps',
      patterns: '*.map',
    },
    {
      label: 'large-assets',
      patterns: (filename, asset) => asset.size() > 500000,
    },
  ],
});

Debug output:

[FilterChunkWebpackPlugin] Filtered: main.js.map (source-maps)
[FilterChunkWebpackPlugin] Filtered: large-image.png (large-assets)
[FilterChunkWebpackPlugin] Summary: 2 of 10 assets filtered

Multiple Rules

Combine multiple rules for complex filtering:

new FilterChunkWebpackPlugin({
  rules: [
    // Remove all source maps
    {
      patterns: '*.map',
    },
    // Remove specific vendor chunks
    {
      patterns: /^vendor-legacy\./,
    },
    // Remove large images from CSS assets directory
    {
      test: /^css\/assets\//,
      patterns: (filename, asset) => {
        return /\.(png|jpg)$/.test(filename) && asset.size() > 100000;
      },
    },
  ],
});

Include Mode

Use mode: 'include' to keep only assets that match your rules (opposite of the default exclude behavior):

// Keep only JavaScript and CSS files, remove everything else
new FilterChunkWebpackPlugin({
  mode: 'include',
  rules: [
    {
      patterns: '*.js',
    },
    {
      patterns: '*.css',
    },
  ],
});

With include mode, rules use OR logic (union) - an asset is kept if it matches any rule:

// Keep JS, CSS, and specific image assets
new FilterChunkWebpackPlugin({
  mode: 'include',
  rules: [
    {
      label: 'scripts',
      patterns: '*.js',
    },
    {
      label: 'styles',
      patterns: '*.css',
    },
    {
      label: 'icons',
      test: /^assets\/icons\//,
      patterns: '*.svg',
    },
  ],
});

Debug output for include mode shows kept assets instead of filtered:

[FilterChunkWebpackPlugin] Kept: main.js (scripts)
[FilterChunkWebpackPlugin] Kept: styles.css (styles)
[FilterChunkWebpackPlugin] Summary: Kept 2 of 5 assets (3 removed)

Migration from v2

Breaking Changes

  1. Node.js 20+ required - Dropped support for Node.js < 20
  2. webpack 5+ required - Dropped support for webpack 4
  3. New configuration format - The patterns and select options have been replaced with a rules array

Configuration Migration

v2 (old):

new FilterChunkWebpackPlugin({
  patterns: ['*.map', 'assets/*'],
  select: false,
});

v3 (new):

new FilterChunkWebpackPlugin({
  rules: [
    {
      patterns: ['*.map', 'assets/*'],
    },
  ],
});

Key Differences

| v2 | v3 | | ------------------------- | ----------------------------------------- | | patterns (top-level) | rules[].patterns | | select: false (exclude) | mode: 'exclude' (default) | | select: true (include) | mode: 'include' | | multimatch syntax | picomatch syntax (mostly compatible) |

Migrating select: true

If you were using select: true in v2 to keep only matching files:

v2:

new FilterChunkWebpackPlugin({
  patterns: ['*.js', '*.css'],
  select: true,
});

v3:

new FilterChunkWebpackPlugin({
  mode: 'include',
  rules: [
    {
      patterns: ['*.js', '*.css'],
    },
  ],
});

Pattern Compatibility

Most glob patterns work the same way. However, if you were using advanced multimatch features, check the picomatch documentation for any differences.

License

filter-chunk-webpack-plugin is MIT licensed