webpack-spawn-plugin

raw JSON →
0.3.0 verified Sat Apr 25 auth: no javascript maintenance

A webpack plugin that runs child_process.spawn during compilation, useful for running shell commands or starting processes as part of the build pipeline. The latest stable version is 0.3.0, released infrequently with last update in 2018. It supports Webpack 4+ hooks via the 'when' option (default 'done') and offers a 'persistent' flag to avoid respawning on recompilation. Compared to alternatives like webpack-shell-plugin or webpack-exec-plugin, this one directly uses Node's spawn for more control over stdio and process handling.

error TypeError: SpawnPlugin is not a constructor
cause Using CommonJS require without proper default import handling.
fix
Use const SpawnPlugin = require('webpack-spawn-plugin').default; or enable esModuleInterop in tsconfig.
error Error: Cannot find module 'webpack-spawn-plugin'
cause Package not installed or not in node_modules.
fix
Run npm install --save-dev webpack-spawn-plugin.
breaking Version 0.2.0 updated to new Webpack API (hooks). If using Webpack <4, the plugin will not work.
fix Use webpack 4+ or stick with v0.1.x for older webpack versions.
gotcha The plugin uses child_process.spawn synchronously under the hood; long-running processes may block the build.
fix Use 'persistent: true' to avoid respawning on rebuilds, but beware that the process may linger.
deprecated The 'persistent' option added in v0.2.1 may cause zombie processes if not handled correctly.
fix Manually manage the child process reference and kill it when done.
npm install webpack-spawn-plugin
yarn add webpack-spawn-plugin
pnpm add webpack-spawn-plugin

Shows basic usage: import the plugin, create a new instance with command and arguments, and add to plugins array.

import SpawnPlugin from 'webpack-spawn-plugin';

export default {
  entry: './src/index.js',
  output: { filename: 'bundle.js' },
  plugins: [
    new SpawnPlugin('node', ['-e', "console.log('Hello from webpack-spawn-plugin!'); process.exit(0);"], { persistent: false }),
  ],
};