-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
48 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
SITE_URL=https://yourdomain.com/ # The base URL of the site you are building | ||
SITE_LANGUAGE=en # The language you are building your website into. It can be overriden on a per page basis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,35 @@ | ||
import { promises as fs } from "node:fs"; | ||
import { join } from "node:path"; | ||
import { Glob } from "bun"; | ||
import { getEnv } from "./utils"; | ||
|
||
async function renderPages() { | ||
const { siteUrl } = getEnv(); | ||
|
||
const glob = new Glob("**/*.tsx"); | ||
|
||
const builtFilenames = new Array<string>(); | ||
const pages = "src/pages"; | ||
for await (const path of glob.scan(pages)) { | ||
const file = join(pages, path); | ||
|
||
const { default: Page } = await import(file); | ||
const html = Page(); | ||
|
||
const outputFilePath = join("dist", path.replace(".tsx", ".html")); | ||
await fs.mkdir(join(outputFilePath, ".."), { recursive: true }); | ||
await fs.writeFile(outputFilePath, html, "utf8"); | ||
const builtFilename = path.replace(".tsx", ".html"); | ||
|
||
builtFilenames.push(builtFilename); | ||
|
||
await fs.mkdir(join("dist", builtFilename, ".."), { recursive: true }); | ||
await fs.writeFile(join("dist", builtFilename), html, "utf8"); | ||
} | ||
|
||
const sitemap = `<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
${builtFilenames.map((filename) => `<url><loc>${siteUrl}${filename}</loc></url>`).join("\n ")} | ||
</urlset>`; | ||
|
||
await fs.writeFile(join("dist", "sitemap.xml"), sitemap, "utf8"); | ||
} | ||
|
||
renderPages().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export function getEnv() { | ||
const siteUrl = process.env["SITE_URL"]; | ||
|
||
if (!siteUrl) { | ||
throw new Error("SITE_URL environment variable is missing"); | ||
} | ||
if (!siteUrl.endsWith("/")) { | ||
throw new Error("SITE_URL should finish with /"); | ||
} | ||
|
||
const siteLang = process.env["SITE_LANGUAGE"]; | ||
|
||
return { | ||
siteUrl, | ||
siteLang, | ||
}; | ||
} |