7
7
} from "astro" ;
8
8
import { glob } from "glob" ;
9
9
import * as semver from "semver" ;
10
- import { parse , relative , type ParsedPath } from "node:path/posix" ;
10
+ import { join , parse , relative , type ParsedPath } from "node:path/posix" ;
11
11
import { fileURLToPath } from "node:url" ;
12
12
import { packages } from "../../../package-lock.json" ;
13
13
@@ -76,8 +76,7 @@ export default function (extensions: string[]): AstroIntegration {
76
76
77
77
let filesInPages : Map <
78
78
string ,
79
- {
80
- parsedPath : ParsedPath ;
79
+ Pick < ParsedPath , "dir" | "ext" | "name" > & {
81
80
route : string ;
82
81
collidedPath ?: string ;
83
82
}
@@ -93,49 +92,54 @@ export default function (extensions: string[]): AstroIntegration {
93
92
94
93
insertPageExtensions ( options , extensions ) ;
95
94
96
- const paths = ( await globPages ( pagesDir ) ) . map ( ( path ) => ( {
97
- path,
98
- parsedPath : parse ( path ) ,
99
- } ) ) ;
100
- const assetsInPages = paths . filter ( ( { parsedPath } ) =>
101
- assetExtensionsSet . has ( parsedPath . ext )
95
+ filesInPages = new Map (
96
+ (
97
+ await glob ( "**/*" , {
98
+ cwd : pagesDir ,
99
+ nodir : true ,
100
+ absolute : false ,
101
+ posix : true ,
102
+ } )
103
+ ) . map ( ( path ) => {
104
+ const parsedPath = parse ( path ) ;
105
+ return [
106
+ path ,
107
+ {
108
+ ...parsedPath ,
109
+ route : join ( "/" , parsedPath . dir , parsedPath . name ) ,
110
+ } ,
111
+ ] ;
112
+ } )
102
113
) ;
103
114
104
- filesInPages = new Map (
105
- paths . map ( ( { path, parsedPath } ) => [
106
- path ,
107
- { parsedPath, route : `/${ parsedPath . dir } /${ parsedPath . name } ` } ,
108
- ] )
115
+ const assetsInPages = new Map (
116
+ filesInPages
117
+ . entries ( )
118
+ . filter ( ( [ _ , { ext } ] ) => assetExtensionsSet . has ( ext ) )
109
119
) ;
110
120
111
121
const sameNamePage = ( { dir, name } : Pick < ParsedPath , "dir" | "name" > ) =>
112
122
pageExtensions . reduce < string | undefined > ( ( acc , ext ) => {
113
123
if ( acc !== undefined ) return acc ;
114
- const path = `${ dir } / ${ name } ${ ext } ` ;
124
+ const path = join ( dir , `${ name } ${ ext } ` ) ;
115
125
return filesInPages . has ( path ) ? path : undefined ;
116
126
} , undefined ) ;
117
127
118
- for ( const { path, parsedPath } of assetsInPages ) {
119
- const collidedPath = sameNamePage ( parsedPath ) ;
120
- if ( collidedPath ) {
121
- filesInPages . set ( path , {
122
- collidedPath,
123
- ...filesInPages . get ( path ) ! ,
124
- } ) ;
125
- }
126
- }
128
+ assetsInPages . values ( ) . forEach ( ( file ) => {
129
+ file . collidedPath = sameNamePage ( file ) ;
130
+ } ) ;
127
131
128
132
options . updateConfig ( {
129
133
redirects : Object . fromEntries (
130
134
assetsInPages
131
- . filter ( ( { path } ) => {
132
- const { route , collidedPath } = filesInPages . get ( path ) ! ;
133
- return (
134
- ( options . config . redirects [ route ] ?? collidedPath ) === undefined
135
- ) ;
136
- } )
137
- . map ( ( { path } ) => [
138
- filesInPages . get ( path ) ! . route ,
135
+ . values ( )
136
+ . filter (
137
+ ( file ) =>
138
+ ( options . config . redirects [ file . route ] ?? file . collidedPath ) ===
139
+ undefined
140
+ )
141
+ . map ( ( { route } ) => [
142
+ route ,
139
143
{ status : 410 , destination : "/" } as unknown as RedirectConfig ,
140
144
] )
141
145
) ,
@@ -177,12 +181,3 @@ function insertPageExtensions(options: {}, extensions: string[]) {
177
181
}
178
182
) . addPageExtension ?.( extensions ) ;
179
183
}
180
-
181
- async function globPages ( pagesDir : string ) : Promise < string [ ] > {
182
- return await glob ( "**/*" , {
183
- cwd : pagesDir ,
184
- nodir : true ,
185
- absolute : false ,
186
- posix : true ,
187
- } ) ;
188
- }
0 commit comments