Webpack OnBuild Plugin
The `on-build-webpack` package provides a Webpack plugin designed to execute a callback function immediately after a build completes. Published as version `0.1.0` in October 2014, this package is considered abandoned and is not actively maintained. Its functionality is extremely basic, offering a single post-build hook, which contrasts sharply with the extensive `compiler.hooks` API introduced in modern Webpack versions (v4+). This plugin uses an outdated Webpack API (`compiler.plugin('done', ...)`) that has been replaced by the Tapable hook system (`compiler.hooks.done.tap(...)`). Consequently, it is incompatible with Webpack v4 and newer. Current best practice is to use Webpack's native plugin system directly via `compiler.hooks.done.tap` within a custom plugin.
Common errors
-
TypeError: compiler.plugin is not a function
cause Using `on-build-webpack` with Webpack v4 or higher. The `compiler.plugin` method was removed and replaced by `compiler.hooks` in Webpack's new Tapable API.fixUpgrade your custom plugin to use `compiler.hooks.done.tap` (or other relevant hooks) instead of the deprecated `compiler.plugin` method. Refer to Webpack's official documentation on Compiler Hooks. -
ERR_REQUIRE_ESM
cause Attempting to `require` this CommonJS-only package in an ES module context, or vice-versa, when Node.js is configured for strict ESM.fixEnsure that your `webpack.config.js` is a CommonJS module (`.js` or `.cjs` with `type: "commonjs"` in `package.json`) if you intend to `require` this package. For modern Webpack, prefer creating a new plugin using ESM and Webpack's `compiler.hooks` API. -
Webpack build fails without an explicit error message related to on-build-webpack, or the callback function is never executed.
cause The plugin is so old that its internal workings might be fundamentally incompatible with even slightly newer versions of Webpack, leading to silent failures or unexpected behavior in the plugin lifecycle.fixGiven the package's age and abandoned status, it's strongly recommended to replace it with a modern, custom plugin utilizing `compiler.hooks.done.tap` for reliable post-build execution.
Warnings
- breaking This plugin is incompatible with Webpack v4, v5, and later versions. It uses an older plugin API (`compiler.plugin`) which has been removed in favor of the new Tapable hook system (`compiler.hooks`).
- breaking The `on-build-webpack` package is abandoned. It was last published in 2014 as version 0.1.0 and has seen no maintenance since. This means it will not receive updates for Webpack compatibility, bug fixes, or security patches.
- gotcha This plugin is CommonJS-only and cannot be imported using ES module syntax (`import`). Attempting to do so in a modern Webpack configuration that expects ESM will result in errors.
Install
-
npm install on-build-webpack -
yarn add on-build-webpack -
pnpm add on-build-webpack
Imports
- WebpackOnBuildPlugin
import WebpackOnBuildPlugin from 'on-build-webpack'; // OR import { WebpackOnBuildPlugin } from 'on-build-webpack';const WebpackOnBuildPlugin = require('on-build-webpack');
Quickstart
const path = require('path');
const WebpackOnBuildPlugin = require('on-build-webpack');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
plugins: [
new WebpackOnBuildPlugin(function(stats) {
// The 'stats' object contains information about the build.
// For modern Webpack, 'stats.hasErrors()' and 'stats.hasWarnings()' are useful.
// For this old plugin, the structure of 'stats' might differ.
if (stats.hasErrors()) {
console.error('Webpack build failed with errors!');
console.error(stats.toString({ colors: true }));
} else {
console.log('Webpack build completed successfully!');
}
}),
],
// Note: This configuration is for demonstration of the plugin's original usage.
// It is unlikely to work with modern Webpack (v4+).
// For modern Webpack, directly use compiler.hooks.done.tap.
};