# esbuild-server

> **⚡️ Fast, lightweight and powerful development server for esbuild ⚡️**

Latest version **0.3.0** (published 2023-05-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install esbuild-server
pnpm add esbuild-server
yarn add esbuild-server
bun add esbuild-server
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2023-05-12 |
| First published | 2022-03-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=14 |
| Dependencies | 0 |
| Unpacked size | 23.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 39 |
| Author | Joel Arvidsson |
| Maintainers | oblador |
| Keywords | esbuild, dev-server, development |

## Links

- npm: https://www.npmjs.com/package/esbuild-server
- Repository: https://github.com/oblador/esbuild-server
- Homepage: https://github.com/oblador/esbuild-server#readme
- Issues: https://github.com/oblador/esbuild-server/issues
- npm.io page: https://npm.io/package/esbuild-server

## 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

- 0.3.0 (latest) — 2023-05-12
- 0.2.1 — 2023-04-26
- 0.2.0 — 2023-04-15
- 0.1.2 — 2022-10-23
- 0.1.1 — 2022-06-21
- 0.1.0 — 2022-03-20

## README

# esbuild-server

**⚡️ Fast, lightweight and powerful development server for esbuild ⚡️**

- Zero dependencies besides esbuild
- API proxy support
- Live reload
- SPA support through History API fallback
- Fully typed with TypeScript

## Installation

```bash
npm install --save-dev esbuild esbuild-server
```

## Usage

Create a new file for your dev server:

```js
// dev-server.js
require('esbuild-server')
  .createServer(
    {
      bundle: true,
      entryPoints: ['src/app.js'],
    },
    {
      static: 'public',
    }
  )
  .start();
```

Assuming you have an `index.html` file in the `public` folder you can now run the server with `node dev-server.js`.

See `example` folder for examples.

## API

### createServer(esbuildOptions, serverOptions)

#### `esbuildOptions`

Options passed to [esbuild Build API](https://esbuild.github.io/api/#build-api). If not specified `watch` defaults to `true` to enable continous build and live reload, and similarly if no type of output option is specified `outdir` is set to a temporary directory.

#### `serverOptions`

| Option                   | Description                                                                                                                                 | Default |
|--------------------------|---------------------------------------------------------------------------------------------------------------------------------------------| ------- |
| **`static`**             | Path to your static assets folder, should contain an `index.html` file.                                                                     | _None_  |
| **`port`**               | Port number to listen for requests on.                                                                                                      | `8080`  |
| **`historyApiFallback`** | For Single Page Apps using the HTML5 History API, the index.html page is served instead of 404 responses.                                   | `false` |
| **`injectLiveReload`**   | Inject snippet to automatically reload the page when file changes are detected.                                                             | `true`  |
| **`open`**               | Open the browser after server had been started. Set to a string to open a particular path.                                                  | `false` |
| **`proxy`**              | Proxy certain paths to a separate API backend when you want to serve API requests on the same domain. Pass a function for dynamic rewrites. | `{}`    |
| **`onProxyRewrite`**     | Callback function when a proxy rewrite happens, useful for logging or altering the response.                                                | _None_  |
| **`http`**               | http options.                                                                                                                               | _None_  |
| **`https`**              | https options.                                                                                                                              | _None_  |

## Proxying

### Static

```js
{
  proxy: {
    '/api': 'http://localhost:3000'
  }
}
```

A request to `/api/users` will now proxy the request to `http://localhost:3000/api/users`. If you want to rewrite the base use dynamic approach instead:

### Dynamic

```js
{
  proxy: (path) => {
    if (path.startsWith('/api')) {
      return path.replace(/^\/api/, 'http://localhost:3000');
    }
  };
}
```

A request to `/api/users` will now proxy the request to `http://localhost:3000/users`.

### Modifying the response

```js
{
  onProxyRewrite: (proxyRes, localUrl, proxyUrl) => {
    console.log(`Proxying ${localUrl} to ${proxyUrl}`);
    proxyRes.headers['x-my-custom-header'] = 'yep';
    return proxyRes;
  };
}
```

## Https

```js
{
    https: {
        key: fs.readFileSync('server.key'),
        cert: fs.readFileSync('server.crt'),
    }
}
```

## Live reload

If you want more control over where the live reload script is injected you can place it manually with:

```html
<script src="/esbuild-livereload.js" async></script>
```

## License

MIT © Joel Arvidsson 2022 – present

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