@@ -8,7 +8,7 @@ import net from 'node:net';
88import os from 'node:os' ;
99import { env } from 'node:process' ;
1010import license from 'rollup-plugin-license' ;
11- import { type Plugin , type PluginOption , defineConfig , loadEnv } from 'vite' ;
11+ import { type Plugin , type PluginOption , type UserConfig , defineConfig , loadEnv } from 'vite' ;
1212import devtoolsJson from 'vite-plugin-devtools-json' ;
1313import mkcert from 'vite-plugin-mkcert' ;
1414
@@ -157,55 +157,55 @@ async function getServerConfig(mode: string, useLocalRedirect: boolean) {
157157}
158158
159159async function ensurePortBindable ( host : string , port : number ) : Promise < void > {
160- return new Promise ( ( resolve ) => {
161- const server = net . createServer ( ) ;
162- server . once ( 'error' , ( err : NodeJS . ErrnoException ) => {
163- if ( err . code === 'EACCES' ) {
164- const platform = os . platform ( ) ;
165- let fix : string ;
166-
167- if ( platform === 'linux' ) {
168- fix = [
169- `Node.js needs permission to serve HTTPS on port ${ port } .\n` ,
170- 'Option 1 (Recommended): Set up a reverse proxy' ,
171- ' Use Nginx or Caddy to proxy traffic to your Node server.' ,
172- ' This is more secure and follows best practices.\n' ,
173- 'Option 2 (Quick fix): Grant Node.js permission to bind to privileged ports' ,
174- chalk . blue . bold ( ` sudo setcap 'cap_net_bind_service=+ep' $(which node)\n` ) ,
175- chalk . yellow . bold (
176- ' ⚠️ Security note: This allows Node to bind to ANY port below 1024.'
177- ) ,
178- chalk . yellow . bold ( ' Only use this in trusted development environments.' ) ,
179- ] . join ( '\n' ) ;
180- } else if ( platform === 'darwin' ) {
181- fix = [
182- `Node.js needs permission to serve HTTPS on port ${ port } .\n` ,
183- 'Fix: Run the dev server with sudo, or set up a reverse proxy.' ,
184- ] . join ( '\n' ) ;
185- } else {
186- fix = `Node.js does not have permission to bind to port ${ port } .\nTry running with elevated privileges or use a reverse proxy.` ;
187- }
188-
189- console . log (
190- boxen ( fix , {
191- padding : 1 ,
192- margin : 1 ,
193- borderStyle : 'round' ,
194- borderColor : 'yellow' ,
195- title : `Port ${ port } Permission Denied` ,
196- titleAlignment : 'center' ,
197- } )
198- ) ;
199- process . exit ( 1 ) ;
160+ const { promise, resolve } = Promise . withResolvers < void > ( ) ;
161+ const server = net . createServer ( ) ;
162+ server . once ( 'error' , ( err : NodeJS . ErrnoException ) => {
163+ if ( err . code === 'EACCES' ) {
164+ const platform = os . platform ( ) ;
165+ let fix : string ;
166+
167+ if ( platform === 'linux' ) {
168+ fix = [
169+ `Node.js needs permission to serve HTTPS on port ${ port } .\n` ,
170+ 'Option 1 (Recommended): Set up a reverse proxy' ,
171+ ' Use Nginx or Caddy to proxy traffic to your Node server.' ,
172+ ' This is more secure and follows best practices.\n' ,
173+ 'Option 2 (Quick fix): Grant Node.js permission to bind to privileged ports' ,
174+ chalk . blue . bold ( ` sudo setcap 'cap_net_bind_service=+ep' $(which node)\n` ) ,
175+ chalk . yellow . bold (
176+ ' ⚠️ Security note: This allows Node to bind to ANY port below 1024.'
177+ ) ,
178+ chalk . yellow . bold ( ' Only use this in trusted development environments.' ) ,
179+ ] . join ( '\n' ) ;
180+ } else if ( platform === 'darwin' ) {
181+ fix = [
182+ `Node.js needs permission to serve HTTPS on port ${ port } .\n` ,
183+ 'Fix: Run the dev server with sudo, or set up a reverse proxy.' ,
184+ ] . join ( '\n' ) ;
185+ } else {
186+ fix = `Node.js does not have permission to bind to port ${ port } .\nTry running with elevated privileges or use a reverse proxy.` ;
200187 }
201- // For other errors (e.g. EADDRINUSE), let Vite handle them
202- resolve ( ) ;
203- } ) ;
204- server . once ( 'listening' , ( ) => {
205- server . close ( ( ) => resolve ( ) ) ;
206- } ) ;
207- server . listen ( port , host ) ;
188+
189+ console . log (
190+ boxen ( fix , {
191+ padding : 1 ,
192+ margin : 1 ,
193+ borderStyle : 'round' ,
194+ borderColor : 'yellow' ,
195+ title : `Port ${ port } Permission Denied` ,
196+ titleAlignment : 'center' ,
197+ } )
198+ ) ;
199+ process . exit ( 1 ) ;
200+ }
201+ // For other errors (e.g. EADDRINUSE), let Vite handle them
202+ resolve ( ) ;
203+ } ) ;
204+ server . once ( 'listening' , ( ) => {
205+ server . close ( ( ) => resolve ( ) ) ;
208206 } ) ;
207+ server . listen ( port , host ) ;
208+ return promise ;
209209}
210210
211211export default defineConfig ( async ( { command, mode, isPreview } ) => {
@@ -215,17 +215,20 @@ export default defineConfig(async ({ command, mode, isPreview }) => {
215215 // If we are running locally, ensure that local.{PUBLIC_SITE_URL} resolves to localhost, and then use mkcert to generate a certificate
216216 const useLocalRedirect = isLocalServe && ! isProduction && ! isTruthy ( env . CI ) ;
217217
218- return defineConfig ( {
218+ return {
219219 build : {
220- target : 'es2024' ,
220+ rolldownOptions : {
221+ output : {
222+ legalComments : 'none' ,
223+ } ,
224+ treeshake :
225+ mode === 'production'
226+ ? { manualPureFunctions : [ 'console.log' , 'console.debug' , 'console.trace' ] }
227+ : undefined ,
228+ } ,
221229 } ,
222230 plugins : getPlugins ( useLocalRedirect ) ,
223231 server : await getServerConfig ( mode , useLocalRedirect ) ,
224232 test : { include : [ 'src/**/*.{test,spec}.{js,ts}' ] } ,
225- esbuild : {
226- legalComments : 'none' ,
227- drop : mode === 'production' ? [ 'debugger' ] : [ ] ,
228- pure : mode === 'production' ? [ 'console.log' , 'console.debug' , 'console.trace' ] : [ ] ,
229- } ,
230- } ) ;
233+ } satisfies UserConfig ;
231234} ) ;
0 commit comments