# spawn-server-webpack-plugin

> Webpack plugin for automatically starting a node server from memory after building.

Latest version **6.2.3** (published 2024-05-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install spawn-server-webpack-plugin
pnpm add spawn-server-webpack-plugin
yarn add spawn-server-webpack-plugin
bun add spawn-server-webpack-plugin
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.2.3 |
| Published | 2024-05-16 |
| First published | 2017-04-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 22.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Dylan Piercey |
| Maintainers | dylanpiercey |
| Keywords | webpack, spawn-server-webpack-plugin, server |

## Links

- npm: https://www.npmjs.com/package/spawn-server-webpack-plugin
- Repository: https://github.com/DylanPiercey/spawn-server-webpack-plugin
- Issues: https://github.com/DylanPiercey/spawn-server-webpack-plugin/issues
- npm.io page: https://npm.io/package/spawn-server-webpack-plugin

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ^2.0.1
- [exit-hook](https://npm.io/package/exit-hook.md) ^2.2.0

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 6.2.3 (latest) — 2024-05-16
- 6.2.2 — 2022-06-14
- 6.2.1 — 2022-04-05
- 6.2.0 — 2022-01-18
- 6.1.0 — 2021-09-09
- 6.0.0 — 2021-09-09
- 5.0.6 — 2021-02-09
- 5.0.5 — 2021-02-02
- 5.0.4 — 2020-11-19
- 5.0.3 — 2020-11-16
- 5.0.2 — 2020-09-10
- 5.0.1 — 2020-09-10
- 5.0.0 — 2020-09-09
- 4.0.5 — 2020-08-07
- 4.0.4 — 2020-07-24
- … 32 more at https://npm.io/package/spawn-server-webpack-plugin/versions

## README

# Spawn Server (Webpack Plugin)

Webpack plugin for Node builds that will automatically load the build into memory when watching and restart the server on consecutive builds.

# Installation

#### Npm

```console
npm install spawn-server-webpack-plugin
```

#### webpack-dev-server version

`webpack-dev-server` >= 4 requires v6 of this module.
`webpack-dev-server` <= 3 requires v5 of this module.

# Example Config

```javascript
const webpack = require("webpack");
const SpawnServerPlugin = require("spawn-server-webpack-plugin");
const spawnedServer = new SpawnServerPlugin({
  args: [
    "--inspect-brk",
    "-r", "some-file.js"
  ]
});

// Build webpack config.
const config = {
  target: "node",
  externals: [/^[^./!]/], // Trick to exclude node modules.
  entry: "./myfile.js",
  plugins: [
    // Support inline sourcemaps.
    new webpack.BannerPlugin({
      banner: 'require("source-map-support").install({ hookRequire: true })',
      raw: true
    }),
    // Use the plugin.
    spawnedServer
  ],
  output: {
    libraryTarget: "commonjs2",
    path: "dist"
  }
};

// Start webpack and trigger watch mode.
webpack(config).watch({ ignore: /node_modules/ }, (err, stats) => {
  // The built node server will start running in the background.
});

// Special events (listening and closing)
spawnedServer.on("listening", (address) => {
  this.address === { port: ..., ip: ... }
  this.listening === true
});

spawnedServer.on("closing", () => {
  this.address === null
  this.listening === false
});
```

### Using with webpack-dev-server

To automatically proxy a WebpackDevServer to the active spawned server (and to ensure that requests wait during server rebuilds) you can add the config exposed under `spawnedServerInstance.devServerConfig` into your `devServer` webpack options.

```js
const configs = [
  {...}, // Browser build
  {...} // Server build (included spawn-server-plugin)
];

new DevServer(webpack(configs), {
  // Set your custom options, then spread in the spawned server config
  ...spawnedServer.devServerConfig
}).listen(8081);

// This is approximately the same as:

new DevServer(webpack(configs), {
  ...,
  // Setup proxy to the actual server.
  proxy: { "**": { target: "http://localhost:8080" } },
  // Ensure webpack waits for server build before reloading.
  setup (app) {
    app.use((req, res, next) => {
      if (spawnedServer.listening) next()
      else spawnedServer.once("listening", next)
    })
  }
}).listen(8081);
```

You can also add this configuration in the same way into the `webpack.config.js` file under the `devServer` option.

### Multiple entry points

Often with server side bundling you will have a single entry point for your server (and thus webpack) which works perfectly with this plugin.
If you need to use multiple entrypoints for your webpack config for the server then this plugin will look for an output file for the `main` entry. You can override this to use a different entry name via the `mainEntry` option to this plugin.

```js
const spawnedServer = new SpawnServerPlugin({
  mainEntry: "index"
});

module.exports = {
  ...,
  entry: {
    index: "./src/index",
    other: "./src/other"
  },
  ...,
  plugins: [
    spawnedServer
  ]
};
```

#### Dynamic Server Port

Using the `devServerConfig` will automatically set `process.env.PORT = 0`. This allows for the spawned server to start on the next available port if you use this environment variable as the port option when listening.

#### Process with multiple servers

By default this plugin will wait for the first http server to be listening and make that information available as the `address`. You can optionally provide a `waitForAppReady: true` option when instanciating the plugin and use `process.send({ event: "app-ready", address: server.address() })` within your process to signal which server should be referenced.

### Contributions

- Use `npm test` to run tests.

Please feel free to create a PR!

---
_Source: https://npm.io/package/spawn-server-webpack-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
