1- import { BunPlugin , fileURLToPath , pathToFileURL } from "bun" ;
2- import { readdir , unlink } from "node:fs/promises" ;
1+ import { BunPlugin , Glob , fileURLToPath , pathToFileURL } from "bun" ;
2+ import { unlink } from "node:fs/promises" ;
33import { basename , join } from "node:path" ;
44
55function escapeRegExp ( string : string ) {
66 return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ; // $& means the whole matched string
77}
88
9- // Some bug in bun cause use FileSystemRouter with bun.build got Error: Unexpected
10- async function * walkDir ( path : string ) : AsyncGenerator < string > {
11- const dirents = await readdir ( path , { withFileTypes : true } ) ;
12- for ( const dirent of dirents ) {
13- const finalPath = join ( path , dirent . name ) ;
14- if ( dirent . isDirectory ( ) ) {
15- yield * walkDir ( finalPath ) ;
16- } else {
17- yield finalPath ;
18- }
9+ async function * glob (
10+ path : string ,
11+ pattern = "**/*.*"
12+ ) : AsyncGenerator < string > {
13+ const glob = new Glob ( pattern ) ;
14+ for await ( const name of glob . scan ( { cwd : path , onlyFiles : true } ) ) {
15+ yield join ( path , name ) ;
1916 }
2017}
2118
@@ -40,7 +37,7 @@ export async function build({
4037} ) {
4138 const entrypoints = [ join ( baseDir , hydrate ) ] ;
4239 const absPageDir = join ( baseDir , pageDir ) ;
43- for await ( const path of walkDir ( absPageDir ) ) {
40+ for await ( const path of glob ( absPageDir ) ) {
4441 entrypoints . push ( path ) ;
4542 }
4643 const result = await Bun . build ( {
@@ -99,7 +96,7 @@ export async function build({
9996 ] ,
10097 } ) ;
10198 if ( result . success ) {
102- for await ( const path of walkDir ( join ( baseDir , buildDir ) ) ) {
99+ for await ( const path of glob ( join ( baseDir , buildDir ) ) ) {
103100 if ( result . outputs . every ( ( x ) => x . path !== path ) ) {
104101 await unlink ( path ) ;
105102 }
0 commit comments