Skip to content

Commit

Permalink
fix: allow using endpoints under src/routes
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-lefebvre committed Apr 18, 2024
1 parent 1248d50 commit 6159cd0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-rice-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrolicious/i18n": patch
---

Allows using endpoints under `src/routes`
77 changes: 44 additions & 33 deletions package/src/routing/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
InjectedRoute,
} from "astro";
import { addPageDir } from "astro-pages";
import { AstroError } from "astro/errors";
import { withLeadingSlash } from "ufo";
import { normalizePath } from "vite";
import type { Options } from "../options.js";
Expand Down Expand Up @@ -76,39 +77,49 @@ const generateRoute = (

let content = readFileSync(page.entrypoint, "utf-8");

content = content
.replaceAll("getLocalePlaceholder()", `"${locale}"`)
.replaceAll(
"getLocalesPlaceholder()",
`[${locales.map((locale) => `"${locale}"`).join(", ")}]`,
)
.replaceAll("getDefaultLocalePlaceholder()", `"${defaultLocale}"`);

let [, frontmatter, ...body] = content.split("---");
// Handle static imports
frontmatter = frontmatter.replace(
/import\s+([\s\S]*?)\s+from\s+['"](.+?)['"]/g,
(_match, p1: string, p2: string) => {
const updatedPath =
p2.startsWith("./") || p2.startsWith("../")
? updateRelativeImports(p2, page.entrypoint, entrypoint)
: p2;
return `import ${p1} from '${updatedPath}'`;
},
);
// Handle dynamic imports
frontmatter = frontmatter.replace(
/import\s*\(\s*['"](.+?)['"]\s*\)/g,
(_match, p1: string) => {
const updatedPath =
p1.startsWith("./") || p1.startsWith("../")
? updateRelativeImports(p1, page.entrypoint, entrypoint)
: p1;
return `import('${updatedPath}')`;
},
);
if (page.entrypoint.endsWith(".astro")) {
try {
content = content
.replaceAll("getLocalePlaceholder()", `"${locale}"`)
.replaceAll(
"getLocalesPlaceholder()",
`[${locales.map((locale) => `"${locale}"`).join(", ")}]`,
)
.replaceAll("getDefaultLocalePlaceholder()", `"${defaultLocale}"`);

let [, frontmatter, ...body] = content.split("---");
// Handle static imports
frontmatter = frontmatter.replace(
/import\s+([\s\S]*?)\s+from\s+['"](.+?)['"]/g,
(_match, p1: string, p2: string) => {
const updatedPath =
p2.startsWith("./") || p2.startsWith("../")
? updateRelativeImports(p2, page.entrypoint, entrypoint)
: p2;
return `import ${p1} from '${updatedPath}'`;
},
);
// Handle dynamic imports
frontmatter = frontmatter.replace(
/import\s*\(\s*['"](.+?)['"]\s*\)/g,
(_match, p1: string) => {
const updatedPath =
p1.startsWith("./") || p1.startsWith("../")
? updateRelativeImports(p1, page.entrypoint, entrypoint)
: p1;
return `import('${updatedPath}')`;
},
);

content = `---${frontmatter}---${body.join("---")}`;
} catch (err) {
throw new AstroError(
`An error occured while transforming "${page.entrypoint}".`,
"Make sure it has a valid frontmatter, even empty",
);
}
}

content = `---${frontmatter}---${body.join("---")}`;
writeFileSync(entrypoint, content, "utf-8");

return {
Expand Down Expand Up @@ -145,7 +156,7 @@ const generateRoute = (
injectedRoute: {
pattern,
entrypoint,
prerender,
...(prerender ? { prerender } : {}),
},
};
};
Expand Down
6 changes: 6 additions & 0 deletions playground/src/routes/data.json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getLocale } from "i18n:astro";
import type { APIRoute } from "astro";

export const GET: APIRoute = () => {
return Response.json({ locale: getLocale(), foo: "bar" });
};

0 comments on commit 6159cd0

Please sign in to comment.