webpack-exclude-assets-plugin
raw JSON → 0.1.1 verified Sat Apr 25 auth: no javascript maintenance
Webpack 4 plugin to exclude assets from the output bundle based on a path RegExp pattern. Version 0.1.1 is the latest stable release with no further updates since 2018. It is designed to remove unwanted JS files generated alongside extracted CSS/Stylus files when using extract plugins like mini-css-extract-plugin. Lightweight alternative to manual asset filtering in webpack configuration, but only compatible with webpack 4 and does not support webpack 5. Ideal for projects that use style entry points and want to eliminate empty JS output files. Last published 6 years ago; no active maintenance.
Common errors
error TypeError: Cannot read properties of undefined (reading 'length') ↓
cause The plugin expects the compilation.assets object to have named keys, but it may be empty or undefined if used asynchronously.
fix
Ensure that the plugin is added to the plugins array and that compilation is not deferred. Use synchronous loaders.
error Error: ExcludeAssetsPlugin: path must be a string or an array of strings ↓
cause Passed a RegExp object instead of a string pattern.
fix
Convert the RegExp to a string: path: ['^js\\/css\\.*\\.js$']
error TypeError: excludeAssetsPlugin is not a constructor ↓
cause Using named import incorrectly (e.g., const { ExcludeAssetsPlugin } = require(...)).
fix
Use default require: const ExcludeAssetsPlugin = require('webpack-exclude-assets-plugin').default;
Warnings
breaking Plugin does not work with webpack 5. It relies on internal webpack 4 hooks (compilation.hooks.optimizeAssets) that are removed in webpack 5. ↓
fix Use webpack 4 or find an alternative such as 'webpack-files-archive-plugin' or custom plugin for webpack 5.
gotcha The `path` option expects regex strings, not actual RegExp objects. The plugin throws if you pass a RegExp literal. ↓
fix Pass a string or array of strings representing the regex pattern. Example: path: ['^js\\/css\\.*\\.js$']
gotcha The plugin does not handle file extensions with dashes or dots correctly in some regex patterns due to missing escaping. ↓
fix Manually escape special regex characters in the path patterns (e.g., use '\\.' instead of '.').
deprecated Package has not been updated since 2018 and is considered deprecated for new projects. ↓
fix Migrate to webpack 5 native asset filtering using 'webpack.IgnorePlugin' or 'output.clean' option.
Install
npm install webpack-exclude-assets-plugin yarn add webpack-exclude-assets-plugin pnpm add webpack-exclude-assets-plugin Imports
- ExcludeAssetsPlugin wrong
import ExcludeAssetsPlugin from 'webpack-exclude-assets-plugin';correctconst ExcludeAssetsPlugin = require('webpack-exclude-assets-plugin'); - ExcludeAssetsPlugin wrong
const { ExcludeAssetsPlugin } = require('webpack-exclude-assets-plugin');correctimport ExcludeAssetsPlugin = require('webpack-exclude-assets-plugin'); - ExcludeAssetsPlugin wrong
const ExcludeAssetsPlugin = require('webpack-exclude-assets-plugin');correctconst ExcludeAssetsPlugin = require('webpack-exclude-assets-plugin').default;
Quickstart
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ExcludeAssetsPlugin = require('webpack-exclude-assets-plugin').default;
module.exports = {
entry: {
app: path.resolve(__dirname, 'js/app.js'),
'css/app': path.resolve(__dirname, 'css/app.css')
},
output: {
path: path.join(__dirname, 'dist'),
filename: 'js/[name].js'
},
plugins: [
new MiniCssExtractPlugin({ filename: '[name].css' }),
new ExcludeAssetsPlugin({
path: ['^js\\/css\\.*\\.js$']
})
],
module: {
rules: [
{
test: /\.css$/,
exclude: /node_modules/,
use: [MiniCssExtractPlugin.loader, 'css-loader']
}
]
},
resolve: { extensions: ['.css'] }
};