Skip to content

Commit

Permalink
Expose urlHostname option
Browse files Browse the repository at this point in the history
See #114
  • Loading branch information
jgoz committed May 21, 2024
1 parent 153cf75 commit b23f695
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changeset/modern-eggs-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@jgoz/esbuild-plugin-livereload': minor
---

Expose urlHostname option

Fixes #114
5 changes: 3 additions & 2 deletions packages/esbuild-plugin-livereload/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ Note that this will have no effect for Node programs.
| Name | Type | Default | Description |
| ---- | ---- | ------- | ----------- |
| fullReloadOnCssUpdates | `boolean` | `false` | Instead of hot-reloading CSS files, trigger a full page reload when CSS is updated. |
| host | `string` | `127.0.0.1` | Host that the livereload server will run on. |
| host | `string` | `127.0.0.1` | Host that the livereload server will run on.<br><br>Setting this value to '0.0.0.0' will allow external connections, e.g., when running the livereload server on a different system from the connecting web browser. This setup likely requires setting `urlHostname` to the either the IP address or local DNS name of the livereload system. |
| port | `number` | `53099` | Port that the livereload server will run on. |
| urlHostname | `string` | - | Hostname to use when connecting to the livereload server.<br><br>This option might be useful when running the livereload server on a different system from the connecting web browser.<br><br>Defaults to the value specified in `host`. |
<!-- end -->
<!-- prettier-ignore-end -->

Expand All @@ -69,7 +70,7 @@ from esbuild, the page will be sent a reload request.
| ---- | ---- | ------- | ----------- |
| errorSource (*) | `string` | - | Key to use when identifying these errors and warnings. Previous results will be overwritten for the same `errorSource`. |
| msg (*) | `ClientMessage` | - | Object containing errors and warnings from the given source |
| connectedClients | `Set<ServerResponse<IncomingMessage>>` | `clients` | Set of long-lived server responses representing clients currently connected to the livereload server. Only required if you are implementing your own livereload server. |
| connectedClients | `Set<ServerResponse<IncomingMessage>>` | `clients` | Set of long-lived server responses representing clients currently connected to the livereload server. Only required if you are implementing your own livereload server. |
<!-- end -->
<!-- prettier-ignore-end -->

Expand Down
28 changes: 25 additions & 3 deletions packages/esbuild-plugin-livereload/src/livereload-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,25 @@ export interface LivereloadPluginOptions {
/**
* Host that the livereload server will run on.
*
* Setting this value to '0.0.0.0' will allow external
* connections, e.g., when running the livereload server
* on a different system from the connecting web browser.
* This setup likely requires setting `urlHostname` to the
* either the IP address or local DNS name of the livereload system.
*
* @default 127.0.0.1
*/
host?: string;

/**
* Hostname to use when connecting to the livereload server.
*
* This option might be useful when running the livereload
* server on a different system from the connecting web browser.
*
* Defaults to the value specified in `host`.
*/
urlHostname?: string;
}

/**
Expand All @@ -72,8 +88,8 @@ export interface LivereloadPluginOptions {
* @returns - An esbuild plugin that enables livereload.
*/
export function livereloadPlugin(options: LivereloadPluginOptions = {}): Plugin {
const { port = 53099, host = '127.0.0.1' } = options;
const baseUrl = `http://${host}:${port}/`;
const { port = 53099, host = '127.0.0.1', urlHostname = host } = options;
const baseUrl = `http://${urlHostname}:${port}/`;

return {
name: 'livereload-plugin',
Expand All @@ -82,7 +98,13 @@ export function livereloadPlugin(options: LivereloadPluginOptions = {}): Plugin
const bannerTemplate = await fsp.readFile(require.resolve('../banner.js'), 'utf-8');
const banner = bannerTemplate.replace(/{baseUrl}/g, baseUrl);

await createLivereloadServer({ basedir, host, port, onSSE: res => clients.add(res) });
await createLivereloadServer({
basedir,
host,
port,
urlHostname,
onSSE: res => clients.add(res),
});

build.initialOptions.banner ??= {};
if (build.initialOptions.banner.js) {
Expand Down
5 changes: 3 additions & 2 deletions packages/esbuild-plugin-livereload/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface LivereloadServerOptions {
basedir: string;
port: number;
host: string;
urlHostname?: string;
onSSE: (res: ServerResponse) => void;
}

Expand All @@ -24,13 +25,13 @@ export type LivereloadRequestHandler = (req: IncomingMessage, res: ServerRespons
export async function createLivereloadRequestHandler(
options: LivereloadServerOptions,
): Promise<LivereloadRequestHandler> {
const { port, host, onSSE, basedir } = options;
const { port, host, onSSE, basedir, urlHostname = host } = options;

const distFiles = await fs.promises.readdir(__dirname);

return function handleLivereloadRequest(req, res): boolean {
if (!req.url) return false;
const url = new URL(req.url, `http://${host}:${port}/`);
const url = new URL(req.url, `http://${urlHostname}:${port}/`);

if (url.pathname === '/esbuild') {
onSSE(
Expand Down

0 comments on commit b23f695

Please sign in to comment.