forked from zendesk/linksf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.js
More file actions
54 lines (49 loc) · 1.57 KB
/
Copy pathrender.js
File metadata and controls
54 lines (49 loc) · 1.57 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
/**
* React Static Boilerplate
* https://github.com/koistya/react-static-boilerplate
* Copyright (c) Konstantin Tarkus (@koistya) | MIT license
*/
import glob from 'glob'
import { join, dirname } from 'path'
import React from 'react'
import ReactDOM from 'react-dom/server'
import Html from '../components/Html'
import task from './lib/task'
import fs from './lib/fs'
const DEBUG = !process.argv.includes('release')
function getPages() {
return new Promise((resolve, reject) => {
glob('**/*.js', { cwd: join(__dirname, '../pages') }, (err, files) => {
if (err) {
reject(err)
} else {
const result = files.map(file => {
let path = '/' + file.substr(0, file.lastIndexOf('.'))
if (path === '/index') {
path = '/'
} else if (path.endsWith('/index')) {
path = path.substr(0, path.lastIndexOf('/index'))
}
return { path, file }
})
resolve(result)
}
})
})
}
async function renderPage(page, component) {
const data = {
body: ReactDOM.renderToString(component),
}
const file = join(__dirname, '../build', page.file.substr(0, page.file.lastIndexOf('.')) + '.html')
const html = '<!doctype html>\n' + ReactDOM.renderToStaticMarkup(<Html debug={DEBUG} {...data} />)
await fs.mkdir(dirname(file))
await fs.writeFile(file, html)
}
export default task(async function render() {
const pages = await getPages()
const { route } = require('../build/app.node')
for (const page of pages) {
await route(page.path, renderPage.bind(undefined, page))
}
})