@@ -4,7 +4,7 @@ import process from "node:process";
44import { fileURLToPath } from "node:url" ;
55
66import chokidar from "chokidar" ;
7- import { context } from "esbuild" ;
7+ import { type BuildResult , context } from "esbuild" ;
88import svelte from "esbuild-svelte" ;
99import { sveltePreprocess } from "svelte-preprocess" ;
1010
@@ -28,17 +28,30 @@ const filename = fileURLToPath(import.meta.url);
2828const outdir = join ( dirname ( filename ) , ".." , "src" , "fava" , "static" ) ;
2929const entryPoints = [ join ( dirname ( filename ) , "src" , "app.ts" ) ] ;
3030
31+ async function cleanup_outdir ( result : BuildResult < { metafile : true } > ) {
32+ // Clean all files in outdir except the ones from this build and favicon.ico
33+ const to_keep = new Set (
34+ Object . keys ( result . metafile . outputs ) . map ( ( p ) => basename ( p ) ) ,
35+ ) ;
36+ to_keep . add ( "favicon.ico" ) ;
37+ const outdir_files = await readdir ( outdir ) ;
38+ for ( const to_delete of outdir_files . filter ( ( f ) => ! to_keep . has ( f ) ) ) {
39+ console . log ( `Cleaning up '${ to_delete } '` ) ;
40+ await unlink ( join ( outdir , to_delete ) ) ;
41+ }
42+ }
43+
3144/**
3245 * Build the frontend using esbuild.
3346 * @param dev - Whether to generate sourcemaps and watch for changes.
3447 */
35- async function run_build ( dev : boolean ) {
48+ async function run_build ( dev : boolean , watch : boolean ) {
3649 const ctx = await context ( {
3750 entryPoints,
3851 outdir,
3952 format : "esm" ,
4053 bundle : true ,
41- // splitting: true, - not used yet
54+ splitting : true ,
4255 metafile : true ,
4356 conditions : dev ? [ "development" ] : [ "production" ] ,
4457 external : [ "fs/promises" , "module" ] , // for web-tree-sitter
@@ -59,36 +72,28 @@ async function run_build(dev: boolean) {
5972 sourcemap : true ,
6073 target : "esnext" ,
6174 } ) ;
62- console . log ( "starting build" ) ;
63- const result = await ctx . rebuild ( ) ;
64-
65- // Clean all files in outdir except the ones from this build and favicon.ico
66- const to_keep = new Set (
67- Object . keys ( result . metafile . outputs ) . map ( ( p ) => basename ( p ) ) ,
75+ console . log (
76+ `starting build, dev=${ dev . toString ( ) } , watch=${ watch . toString ( ) } ` ,
6877 ) ;
69- to_keep . add ( "favicon.ico" ) ;
70- const outdir_files = await readdir ( outdir ) ;
71- for ( const to_delete of outdir_files . filter ( ( f ) => ! to_keep . has ( f ) ) ) {
72- console . log ( "Cleaning up " , to_delete ) ;
73- await unlink ( join ( outdir , to_delete ) ) ;
74- }
75-
78+ const result = await ctx . rebuild ( ) ;
79+ await cleanup_outdir ( result ) ;
7680 console . log ( "finished build" ) ;
7781
78- if ( ! dev ) {
82+ if ( ! watch ) {
7983 await ctx . dispose ( ) ;
8084 } else {
8185 console . log ( "watching for file changes" ) ;
8286 const rebuild = debounce ( ( ) => {
8387 console . log ( "starting rebuild" ) ;
84- ctx . rebuild ( ) . then (
85- ( ) => {
88+ ctx
89+ . rebuild ( )
90+ . then ( async ( result ) => cleanup_outdir ( result ) )
91+ . then ( ( ) => {
8692 console . log ( "finished rebuild" ) ;
87- } ,
88- ( err : unknown ) => {
93+ } )
94+ . catch ( ( err : unknown ) => {
8995 console . error ( err ) ;
90- } ,
91- ) ;
96+ } ) ;
9297 } , 200 ) ;
9398 chokidar
9499 . watch ( [ "src" , "css" ] , {
@@ -106,8 +111,9 @@ const is_main = resolve(process.argv[1] ?? "") === filename;
106111
107112if ( is_main ) {
108113 const watch = process . argv . includes ( "--watch" ) ;
114+ const dev = process . argv . includes ( "--dev" ) ;
109115
110- run_build ( watch ) . catch ( ( e : unknown ) => {
116+ run_build ( dev , watch ) . catch ( ( e : unknown ) => {
111117 console . error ( e ) ;
112118 } ) ;
113119}
0 commit comments