Skip to content

Commit 0892de8

Browse files
committed
rewrite internal process using Maps and derived iterators
1 parent 61d776e commit 0892de8

File tree

1 file changed

+36
-41
lines changed

1 file changed

+36
-41
lines changed

src/lib/ignoreassets/index.ts

+36-41
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "astro";
88
import { glob } from "glob";
99
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";
1111
import { fileURLToPath } from "node:url";
1212
import { packages } from "../../../package-lock.json";
1313

@@ -76,8 +76,7 @@ export default function (extensions: string[]): AstroIntegration {
7676

7777
let filesInPages: Map<
7878
string,
79-
{
80-
parsedPath: ParsedPath;
79+
Pick<ParsedPath, "dir" | "ext" | "name"> & {
8180
route: string;
8281
collidedPath?: string;
8382
}
@@ -93,49 +92,54 @@ export default function (extensions: string[]): AstroIntegration {
9392

9493
insertPageExtensions(options, extensions);
9594

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+
})
102113
);
103114

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))
109119
);
110120

111121
const sameNamePage = ({ dir, name }: Pick<ParsedPath, "dir" | "name">) =>
112122
pageExtensions.reduce<string | undefined>((acc, ext) => {
113123
if (acc !== undefined) return acc;
114-
const path = `${dir}/${name}${ext}`;
124+
const path = join(dir, `${name}${ext}`);
115125
return filesInPages.has(path) ? path : undefined;
116126
}, undefined);
117127

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+
});
127131

128132
options.updateConfig({
129133
redirects: Object.fromEntries(
130134
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,
139143
{ status: 410, destination: "/" } as unknown as RedirectConfig,
140144
])
141145
),
@@ -177,12 +181,3 @@ function insertPageExtensions(options: {}, extensions: string[]) {
177181
}
178182
).addPageExtension?.(extensions);
179183
}
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

Comments
 (0)