forked from petergoes/ssr-web-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
29 lines (23 loc) · 807 Bytes
/
build.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
const path = require('path')
const puppeteer = require('puppeteer')
const fs = require('fs')
const serialize = require('./lib/serialize')
const hydrate = require('./lib/hydrate')
;(async (fileName) => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
page.on('console', event => console.log(event._text))
page.on('load', async (...args) => {
await page.$eval('body', serialize)
const pageContent = await page.content()
fs.writeFile(
path.join(__dirname, fileName.replace('.html', '.ssr.html')),
pageContent.replace('</body>', `${hydrate}</body>`),
{encoding: 'utf8'},
async (err) => {
await browser.close();
}
)
})
await page.goto('file://' + path.join(__dirname, fileName))
})('/public/index.html')