-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathssr.js
68 lines (61 loc) · 2.37 KB
/
ssr.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
const fs = require('fs').promises;
const chromium = require('chrome-aws-lambda');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const config = require('./webpack/webpack')({production: true});
const {getDirectories} = require('./webpack/webpack.utility');
const {paths} = require('./webpack/webpack.constants');
const PORT = 9000;
const PATHS = [
'/motivation',
...getDirectories(paths.src + '/content/posts').map(d => '/blog/post/' + d),
...getDirectories(paths.src + '/content/examples').map(d => '/examples/' + d),
...getDirectories(paths.src + '/content/docs/introduction').map(d => '/docs/introduction/' + d),
...getDirectories(paths.src + '/content/docs/components').map(d => '/docs/components/' + d),
...getDirectories(paths.src + '/content/docs/hooks').map(d => '/docs/hooks/' + d),
...getDirectories(paths.src + '/content/docs/tools').map(d => '/docs/tools/' + d),
];
const runServer = () => new Promise((resolve, reject) => {
const compiler = webpack(config);
const server = new WebpackDevServer(compiler, config.devServer);
compiler.hooks.done.tap('IDoNotUnderstandWhatThisStringIsForButItCannotBeEmpty', () => {
console.log('Done compiling');
resolve(server);
});
server.listen(PORT, '0.0.0.0', err => {
if (err) {
reject(err);
}
});
});
const runBrowser = async () => {
let browser;
try {
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: true,
ignoreHTTPSErrors: true,
});
} catch(e) {
console.error(e);
process.exit(1);
}
return browser;
};
const ssr = async () => {
const server = await runServer();
const browser = await runBrowser();
const page = await browser.newPage();
for (const path of PATHS) {
console.log(`Rendering http://localhost:${PORT}${path} into build${path}/index.html`);
await page.goto(`http://localhost:${PORT}${path}`, {waitUntil: 'networkidle0'});
const html = await page.content()
await fs.mkdir(`build${path}`, {recursive: true});
await fs.writeFile(`build${path}/index.html`, html);
}
await browser.close();
await server.close();
};
ssr();