|
| 1 | +# esbuild-server |
| 2 | + |
| 3 | +**⚡️ Fast, lightweight and powerful development server for esbuild ⚡️** |
| 4 | + |
| 5 | +- Zero dependencies besides esbuild |
| 6 | +- API proxy support |
| 7 | +- Live reload |
| 8 | +- SPA support through History API fallback |
| 9 | +- Fully typed with TypeScript |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +```bash |
| 14 | +npm install --save-dev esbuild esbuild-server |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +Create a new file for your dev server: |
| 20 | + |
| 21 | +```js |
| 22 | +// dev-server.js |
| 23 | +require('esbuild-server') |
| 24 | + .createServer( |
| 25 | + { |
| 26 | + bundle: true, |
| 27 | + entryPoints: ['src/app.js'], |
| 28 | + }, |
| 29 | + { |
| 30 | + static: 'public', |
| 31 | + } |
| 32 | + ) |
| 33 | + .start(); |
| 34 | +``` |
| 35 | + |
| 36 | +Assuming you have an `index.html` file in the `public` folder you can now run the server with `node dev-server.js`. |
| 37 | + |
| 38 | +See `example` folder for examples. |
| 39 | + |
| 40 | +## API |
| 41 | + |
| 42 | +### createServer(esbuildOptions, serverOptions) |
| 43 | + |
| 44 | +#### `esbuildOptions` |
| 45 | + |
| 46 | +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. |
| 47 | + |
| 48 | +#### `serverOptions` |
| 49 | + |
| 50 | +| Option | Description | Default | |
| 51 | +| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------- | ------- | |
| 52 | +| **`static`** | Path to your static assets folder, should contain an `index.html` file. | _None_ | |
| 53 | +| **`port`** | Port number to listen for requests on. | `8080` | |
| 54 | +| **`historyApiFallback`** | For Single Page Apps using the HTML5 History API, the index.html page is served instead of 404 responses. | `false` | |
| 55 | +| **`injectLiveReload`** | Inject snippet to automatically reload the page when file changes are detected. | `true` | |
| 56 | +| **`open`** | Open the browser after server had been started. Set to a string to open a particular path. | `false` | |
| 57 | +| **`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. | `{}` | |
| 58 | +| **`onProxyRewrite`** | Callback function when a proxy rewrite happens, useful for logging or altering the response. | _None_ | |
| 59 | + |
| 60 | +## Proxying |
| 61 | + |
| 62 | +### Static |
| 63 | + |
| 64 | +```js |
| 65 | +{ |
| 66 | + proxy: { |
| 67 | + '/api': 'http://localhost:3000' |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +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: |
| 73 | + |
| 74 | +### Dynamic |
| 75 | + |
| 76 | +```js |
| 77 | +{ |
| 78 | + proxy: (path) => { |
| 79 | + if (path.startsWith('/api')) { |
| 80 | + return path.replace(/^\/api/, 'http://localhost:3000'); |
| 81 | + } |
| 82 | + }; |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +A request to `/api/users` will now proxy the request to `http://localhost:3000/users`. |
| 87 | + |
| 88 | +### Modifying the response |
| 89 | + |
| 90 | +```js |
| 91 | +{ |
| 92 | + onProxyRewrite: (proxyRes, localUrl, proxyUrl) => { |
| 93 | + console.log(`Proxying ${localUrl} to ${proxyUrl}`); |
| 94 | + proxyRes.headers['x-my-custom-header'] = 'yep'; |
| 95 | + return proxyRes; |
| 96 | + }; |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Live reload |
| 101 | + |
| 102 | +If you want more control over where the live reload script is injected you can place it manually with: |
| 103 | + |
| 104 | +```html |
| 105 | +<script src="/esbuild-livereload.js" async></script> |
| 106 | +``` |
| 107 | + |
| 108 | +## License |
| 109 | + |
| 110 | +MIT © Joel Arvidsson 2022 – present |
0 commit comments