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

esbuild.serve does not emit any URLs / ports, even with logLevel: 'info' #2393

Closed
fregante opened this issue Jul 17, 2022 · 2 comments · Fixed by #2816
Closed

esbuild.serve does not emit any URLs / ports, even with logLevel: 'info' #2393

fregante opened this issue Jul 17, 2022 · 2 comments · Fixed by #2816

Comments

@fregante
Copy link

The ClI works

 npx esbuild --serve --bundle index.js --outdir=a

 > Local:   http://127.0.0.1:8000/
 > Network: http://10.9.8.45:8000/

The API doesn't output anything:

❯ WATCH=true node esbuild.config.mjs
// nothing
//esbuild.config.mjs
import process from 'node:process';
import esbuild from 'esbuild';

const serve = {
	servedir: 'public',
};
const build = {
	entryPoints: ['index.js'],
	outdir: 'public',
	logLevel: 'info',
};

if (process.env.WATCH === 'true') {
	await esbuild.serve(serve, build);
} else {
	await esbuild.build(build);
}

Any config to make it work? Or is this functionality missing?

@evanw
Copy link
Owner

evanw commented Jul 17, 2022

The CLI just calls the public API like you are doing, and then pretty-prints the returned address and port:

esbuild/pkg/cli/cli_impl.go

Lines 1320 to 1355 in f730c03

// If this is "0.0.0.0" or "::", list all relevant IP addresses
if ip := net.ParseIP(result.Host); ip != nil && ip.IsUnspecified() {
if addrs, err := net.InterfaceAddrs(); err == nil {
for _, addr := range addrs {
if addr, ok := addr.(*net.IPNet); ok && (addr.IP.To4() != nil) == (ip.To4() != nil) && !addr.IP.IsLinkLocalUnicast() {
hosts = append(hosts, addr.IP.String())
}
}
}
}
// Otherwise, just list the one IP address
if len(hosts) == 0 {
hosts = append(hosts, result.Host)
}
// Determine the host kinds
kinds := make([]string, len(hosts))
maxLen := 0
for i, host := range hosts {
kind := "Network"
if ip := net.ParseIP(host); ip != nil && ip.IsLoopback() {
kind = "Local"
}
kinds[i] = kind
if len(kind) > maxLen {
maxLen = len(kind)
}
}
// Pretty-print the host list
for i, kind := range kinds {
sb.WriteString(fmt.Sprintf("\n > %s:%s %shttp://%s/%s",
kind, strings.Repeat(" ", maxLen-len(kind)), colors.Underline,
net.JoinHostPort(hosts[i], fmt.Sprintf("%d", result.Port)), colors.Reset))
}

You are welcome to print it yourself if you'd like, using whatever formatting you want. The host and port are returned from serve here:

esbuild/lib/shared/types.ts

Lines 238 to 244 in f730c03

/** Documentation: https://esbuild.github.io/api/#serve-return-values */
export interface ServeResult {
port: number;
host: string;
wait: Promise<void>;
stop: () => void;
}

This is documented here: https://esbuild.github.io/api/#serve-return-values

@fregante
Copy link
Author

Thank you for the solution, but that feels like I’m rewriting core functionality and it’s not super straightforward either https://thewebdev.info/2022/02/26/how-to-get-local-ip-address-in-node-js/

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

Successfully merging a pull request may close this issue.

2 participants