-
Notifications
You must be signed in to change notification settings - Fork 1
/
pre-render.js
54 lines (54 loc) · 1.61 KB
/
pre-render.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
/* eslint-disable functional/no-expression-statement */
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
const write = require('write')
const micro = require('micro')
const handler = require('serve-handler')
const puppeteer = require('puppeteer')
const { listFiles } = require('list-files-in-dir')
const serveConfig = require('./serve.json')
const port = 5555
const format = (h) =>
typeof h === 'string'
? h.replace(/<!--((?!-->)[\w\W])*-->|\s+?class="show"/g, '')
: h
const getHTML = (browser) => async (pathname) => {
console.info('render start', pathname)
const page = await browser.newPage()
await page.goto(`http://localhost:${port}${pathname}`, {
waitUntil: 'domcontentloaded',
})
await page
.waitForSelector('x-app > *', {
timeout: 5000,
})
.catch(console.log)
const html = await page.content()
console.info('render end', pathname)
return html
}
;(async () => {
const serve = await micro((req, res) =>
handler(req, res, serveConfig)
).listen(port)
const pages = await listFiles('content').then((fls) =>
fls
.filter((f) => f.endsWith('.md'))
.map((f) =>
f.replace(`${__dirname}/content`, '').replace(/(\.md|index)/g, '')
)
)
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
})
const ssr = getHTML(browser)
const htmls = await Promise.all(pages.map(ssr))
await browser.close()
await Promise.all(
pages.map((page, i) => write(`dist${page}/index.html`, format(htmls[i])))
)
serve.close()
console.log('completed!!!')
})()