Webpack Remove Empty Scripts Plugin

1.1.1 · active · verified Tue Apr 21

webpack-remove-empty-scripts is a Webpack plugin designed to eliminate superfluous empty JavaScript files generated by Webpack 5, particularly when an `entry` point consists solely of style assets (e.g., SCSS or CSS files). This issue frequently occurs even when `mini-css-extract-plugin` correctly processes and extracts styles into dedicated CSS files, leaving behind an empty `.js` file for each style entry. The plugin automatically detects and removes these redundant outputs, ensuring a cleaner and more efficient build directory. The current stable version is `1.1.1`, and the project demonstrates an active maintenance cadence with minor releases and patches occurring every few months. Its primary utility lies in streamlining the build output for Webpack 5 projects that heavily utilize CSS preprocessors and separate style extraction.

Common errors

Warnings

Install

Imports

Quickstart

This configuration demonstrates how to integrate `webpack-remove-empty-scripts` into a Webpack 5 setup, ensuring that empty JavaScript files are not generated for style-only entry points when using `mini-css-extract-plugin`.

import RemoveEmptyScriptsPlugin from 'webpack-remove-empty-scripts';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import path from 'path';

export default {
  entry: {
    main: './src/index.js',
    styles: './src/styles.scss', // This will generate an empty JS file without the plugin
    legacyCss: './src/legacy.css',
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js',
    clean: true, // Clean the output directory before emit.
  },
  module: {
    rules: [
      {
        test: /\.(scss|css)$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'sass-loader'
        ],
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: { presets: ['@babel/preset-env'] }
        }
      }
    ],
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: '[name].css',
    }),
    new RemoveEmptyScriptsPlugin({
      // Optional: Set to true for more detailed output during build
      verbose: process.env.NODE_ENV === 'development',
      // Optional: If you need to revert to v0.8.2-0.8.4 behavior for specific compatibility
      // stage: RemoveEmptyScriptsPlugin.STAGE_AFTER_PROCESS_PLUGINS,
    }),
  ],
  devtool: 'source-map',
  mode: process.env.NODE_ENV === 'production' ? 'production' : 'development'
};

view raw JSON →