forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.js
57 lines (49 loc) · 1.85 KB
/
engine.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
const { renderToString } = require('react-dom/server')
const { transform } = require('./babel')
const React = require('react')
const fs = require('fs')
const path = require('path')
const dirTree = require('directory-tree')
// Name of directory for saving transformed components that should be gitignored
const dist = 'dist'
// Build React components
// This loops through the react components and transpiles them to /dist
// so they can be used by Node.js when we do server side rendering
const tree = dirTree('./react/')
if (tree) {
for (const index in tree.children) {
const file = tree.children[index]
if (file.type === 'file' && file.extension === '.js') {
if (!fs.existsSync(path.join(dist, 'react'))) {
fs.mkdirSync(path.join(dist, 'react'), { recursive: true })
}
const content = transform(fs.readFileSync(file.path, 'utf8'))
fs.writeFileSync(path.join(dist, file.path), content)
}
}
}
// End Build React Components
// Register components
const components = {
// CodeBlock: require('../../dist/react/CodeBlock'),
// CodeEditor: require('../../dist/react/CodeEditor')
}
const renderReact = async componentStr => {
// Get component name as string so we can use it in the class name
// which will be needed later if we choose to do client side React hydration
const componentName = componentStr.match(/<([a-zA-Z]+)\s/)[1]
// Add the wrapper and class name so we can later use React hydration on the client
// side
const jsx = `<div className="react-component-${componentName}">\n${componentStr}\n</div>`
const component = transform(jsx)
// eslint-disable-next-line
const getComponent = new Function(
'React',
...Object.keys(components),
`${component.replace('React', 'return React')}`
)
return renderToString(getComponent(React, ...Object.values(components)))
}
module.exports = {
renderReact
}