-
Notifications
You must be signed in to change notification settings - Fork 2
/
generator.js
203 lines (158 loc) · 5.16 KB
/
generator.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Custom static site generator using Parcel.
//
// This script cannot be run directly by Node.js, but can be built via Parcel
// and then run.
import Parcel from "parcel-bundler";
import path from "path";
import { promises as fs } from "fs";
import rimraf from "rimraf";
import React from "react";
import ReactDOMServer from "react-dom/server";
import { Helmet } from "react-helmet";
import { JSDOM } from "jsdom";
import { StaticRouter as Router } from "react-router-dom";
import App from "./src/components/App";
const CNAME = "v2.rojo.space";
const BASE_URL = process.env.PARCEL_PUBLIC_URL;
function main() {
build({
entry: "src/index.html",
outDir: "dist",
// A list of the initial pages that should be built. The code as-written
// will crawl links to discover any pages linked from these initial
// routes, as well.
initialRoutes: [
"/",
// We can pass an object to override the output path of this route,
// like if our static content host has specific requirements on 404
// pages.
{
route: "/404",
outputPath: "/404.html",
},
],
// Each page will be rendered by this function, defined below.
renderPage,
});
}
// renderPage is expected to render the passed in route and render it to
// a string containing HTML.
//
// We can do that however we want. Here we use React.
//
// `template` is given to us as an HTML string that has been processed
// by Parcel. It'll be the contents of `src/index.html`, but with CSS
// and JS references injected into it.
function renderPage(template, route, addRoute) {
const context = {};
const content = ReactDOMServer.renderToString(
<Router location={ route } context={ context } basename={ BASE_URL }>
<App />
</Router>
);
const helmet = Helmet.renderStatic();
const dom = new JSDOM(template);
const main = dom.window.document.querySelector("#app");
main.innerHTML = content;
const extraHead = helmet.meta.toString() + helmet.title.toString();
const head = dom.window.document.querySelector("head");
head.insertAdjacentHTML("afterbegin", extraHead);
if (context.url != null) {
const redirect = dom.window.document.createElement("meta");
redirect.setAttribute("http-equiv", "refresh");
redirect.setAttribute("content", `0; URL='${ context.url }'`);
head.appendChild(redirect);
console.log("...redirects to", context.url);
}
// To add new pages, we can invoke `addRoute` with a site-relative
// path.
//
// Here, we're just crawling the page for anything that looks like a
// link.
const links = dom.window.document.querySelectorAll("a");
for (const link of links) {
let url = link.href;
// Rough pattern to ignore off-site links
if (/^\w+:\/\//.test(url)) {
continue;
}
if (url.startsWith(BASE_URL)) {
url = url.slice(BASE_URL.length);
}
addRoute(url);
}
return dom.serialize();
}
// Build the given Parcel entrypoint. Individual pages are rendered using the
// `renderPage` callback, which should return HTML strings.
async function build({ entry, outDir, initialRoutes, renderPage }) {
await rimrafPromise(outDir);
await fs.mkdir(outDir, { recursive: true });
const publicUrl = process.env.PARCEL_PUBLIC_URL || "/";
const bundler = new Parcel([entry], {
publicUrl,
watch: false,
minify: true,
autoInstall: false,
});
await bundler.bundle();
// This should be `entry`, but processed by Parcel.
const template = await fs.readFile(path.join(outDir, path.basename(entry)));
const visitedRoutes = new Set(initialRoutes);
const routesToVisit = initialRoutes;
const addRoute = route => {
if (!visitedRoutes.has(route)) {
visitedRoutes.add(route);
routesToVisit.push(route);
}
};
while (true) {
let route = routesToVisit.pop();
if (route == null) {
break;
}
let outputPath;
if (typeof route == "string") {
outputPath = routeToFilePath(route);
} else {
outputPath = route.outputPath;
route = route.route;
}
outputPath = path.join(outDir, outputPath);
console.log(`Generating route ${ route }`);
const rendered = renderPage(template, route, addRoute);
console.log(`Saving to ${ outputPath }`);
await fs.mkdir(path.dirname(outputPath), { recursive: true });
await fs.writeFile(outputPath, rendered);
}
console.log("Writing CNAME file for GitHub Pages");
const cnamePath = path.join(outDir, "CNAME");
await fs.writeFile(cnamePath, CNAME);
console.log("Writing .nojekyll file for GitHub Pages");
const noJekyllPath = path.join(outDir, ".nojekyll");
await fs.writeFile(noJekyllPath, "");
}
// Simple Promise wrapper around rimraf.
function rimrafPromise(dir) {
return new Promise((resolve, reject) => {
rimraf(dir, (err) => {
if (err == null) {
resolve();
} else {
reject(err);
}
});
});
}
// Transforms a site-relative route for a page into a file path.
function routeToFilePath(route) {
let current = "";
for (const piece of route.split("/")) {
if (piece.length === 0) {
continue;
}
current = path.join(current, piece);
}
return path.join(current, "index.html");
}
main();