Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No server-sent event if servedir is undefined #2827

Closed
yzrmn opened this issue Jan 15, 2023 · 1 comment
Closed

No server-sent event if servedir is undefined #2827

yzrmn opened this issue Jan 15, 2023 · 1 comment

Comments

@yzrmn
Copy link

yzrmn commented Jan 15, 2023

Hi :)

I had some weird behavior with the new context() API when using watch() and serve() and without defining servedir.
Although this happened by mistake it still caused quite the confusion.

Consider:

export async function serveHelper(
    buildOptions: BuildOptions,
    serveOptions: ServeOptions,
    watchOptions?: WatchOptions
): Promise<void> {
    const contextOptions = { ...buildOptions };

    if (watchOptions !== undefined) {
        if (contextOptions.format === "iife") {
            contextOptions.footer = { js: FOOTER_LIVE_RELOAD_IIFE };
        } else {
            contextOptions.footer = { js: FOOTER_LIVE_RELOAD_DEFAULT };
        }
    }

    const ctx = await context(contextOptions);

    if (watchOptions !== undefined) {
        ctx.watch(watchOptions);
    }

    await ctx.serve(serveOptions);
}

serveHelper(
    {
        bundle: true,
        entryPoints: ["./src/index.ts", "./src/index.html"],
        format: "esm",
        loader: { ".html": "copy" },
        outdir: "./dist",
    },
    {
        host: "127.0.0.1",
        port: 8001,
    },
    {}
);

It seems to do the right thing and serves the files. It also correctly watches the files and rebuilds them. However, it does not send the server event to trigger a live reload when using:

new EventSource('/esbuild').addEventListener('change', () => location.reload())

If servedir is set to ./dist everything works as expected and the server event is sent.

@evanw
Copy link
Owner

evanw commented Jan 16, 2023

Thanks very much for the report. Looks like I'm missing test coverage for this. I'll fix this in the next release.

@evanw evanw closed this as completed in f12d93c Jan 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants