-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathgen-multiple-htmls.ts
More file actions
72 lines (60 loc) · 2.34 KB
/
gen-multiple-htmls.ts
File metadata and controls
72 lines (60 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// for dev, run:
// npm run clean && npm run bundle && npm run generate:htmls
// npm run bundle:preview
import { join } from "path";
import { readFileSync, writeFileSync, mkdirSync } from "fs";
import { allPages } from "./pages";
import { Environment, environments } from "@dzcode.io/utils/dist/config/environment";
import { fsConfig } from "@dzcode.io/utils/dist/config";
let stage = process.env.STAGE as Environment;
if (!environments.includes(stage)) {
console.log(`⚠️ No STAGE provided, falling back to "development"`);
stage = "development";
}
const config = fsConfig(stage);
const distFolder = "./bundle";
const indexHtmlPath = join(distFolder, "index.html");
const indexHtml = readFileSync(indexHtmlPath).toString();
const SKIP_ROOT_HTML = process.env.SKIP_ROOT_HTML === "true";
console.log(`generating ${allPages.length} html files ...`);
allPages.forEach((pageInfo) => {
let pathName = pageInfo.uri.replace(/\/$/, "");
pathName = `${pathName || "index"}.html`;
if (SKIP_ROOT_HTML && pathName === "index.html") {
console.log(`skipping root html`);
return;
}
const outputHtmlPath = join(distFolder, pathName);
const outputHtmlParentDir = join(outputHtmlPath, "..");
let newHtml = indexHtml;
newHtml = newHtml.replace(
/{{googleAnalytics}}/g,
stage === "development"
? ""
: `
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YPJCPRQSNE"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-YPJCPRQSNE');
</script>`
.replace(/\n/g, "")
.replace(/\s{2,}/g, " "),
);
newHtml = newHtml.replace(/{{themeColor}}/g, "#43a047");
newHtml = newHtml.replace(/{{lang}}/g, pageInfo.lang);
newHtml = newHtml.replace(/{{keywords}}/g, pageInfo.keywords);
newHtml = newHtml.replace(/{{title}}/g, pageInfo.title);
newHtml = newHtml.replace(/{{description}}/g, pageInfo.description);
newHtml = newHtml.replace(
/{{canonical}}/g,
pageInfo.canonicalUrl || `${config.web.url}${pageInfo.uri}`,
);
newHtml = newHtml.replace(/{{ogImage}}/g, pageInfo.ogImage);
mkdirSync(outputHtmlParentDir, { recursive: true });
writeFileSync(outputHtmlPath, newHtml);
console.log(outputHtmlPath, "✅");
});
console.log("done");