如何配置preact开发服务器使用不同的地址?



默认情况下,标准preact设置中的开发服务器运行在http://0.0.0.0:8080或您当前的IPv4地址上:

You can view the application in browser.
Local:            http://0.0.0.0:8080
On Your Network:  http://192.168.2.105:8080

两者都不是很有用。0.0.0.0是无法提供任何服务的东西(至少在macOS上,我在所有浏览器(Chrome, Brave, FF, Safari)中获得ERR_EMPTY_RESPONSE,当前的IP地址是动态的。另外,我想使用https。

如何更改开发服务器提供应用程序内容的地址?

解决方案不是preact.config.js,正如我所想的,而是preact-cli,它接受一些参数来配置web服务器:

$ preact watch
--src              Specify source directory  (default src)
--cwd              A directory to use instead of $PWD  (default .)
--esm              Builds ES-2015 bundles for your code  (default false)
--clear            Clear the console (default true)
--sw               Generate and attach a Service Worker  (default false)
--babelConfig      Path to custom Babel config (default .babelrc)
--json             Generate build stats for bundle analysis
--https            Run server with HTTPS protocol
--key              Path to PEM key for custom SSL certificate
--cert             Path to custom SSL certificate
--cacert           Path to optional CA certificate override
--prerender        Pre-render static content on first run
--prerenderUrls    Path to pre-rendered routes config  (default prerender-urls.json)
--template         Path to custom HTML template (default 'src/template.html')
--refresh          Enables experimental preact-refresh functionality
-c, --config       Path to custom CLI config  (default preact.config.js)
-H, --host         Set server hostname  (default 0.0.0.0)
-p, --port         Set server port  (default 8080)
-h, --help         Displays this message

这样我就可以在package.json中为preact创建合适的命令:

"scripts": {
"build": "preact build",
"dev": "preact watch -H localhost -p 3000 --clear false",
},

最新更新