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" ;
3
3
import { basename , join } from "node:path" ;
4
4
5
5
function escapeRegExp ( string : string ) {
6
6
return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ; // $& means the whole matched string
7
7
}
8
8
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 ) ;
19
16
}
20
17
}
21
18
@@ -40,7 +37,7 @@ export async function build({
40
37
} ) {
41
38
const entrypoints = [ join ( baseDir , hydrate ) ] ;
42
39
const absPageDir = join ( baseDir , pageDir ) ;
43
- for await ( const path of walkDir ( absPageDir ) ) {
40
+ for await ( const path of glob ( absPageDir ) ) {
44
41
entrypoints . push ( path ) ;
45
42
}
46
43
const result = await Bun . build ( {
@@ -99,7 +96,7 @@ export async function build({
99
96
] ,
100
97
} ) ;
101
98
if ( result . success ) {
102
- for await ( const path of walkDir ( join ( baseDir , buildDir ) ) ) {
99
+ for await ( const path of glob ( join ( baseDir , buildDir ) ) ) {
103
100
if ( result . outputs . every ( ( x ) => x . path !== path ) ) {
104
101
await unlink ( path ) ;
105
102
}
0 commit comments