Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

engine.js 1.9 KB

You have to be logged in to leave a comment. Sign In
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
  1. const { renderToString } = require('react-dom/server')
  2. const { transform } = require('./babel')
  3. const React = require('react')
  4. const fs = require('fs')
  5. const path = require('path')
  6. const dirTree = require('directory-tree')
  7. // Name of directory for saving transformed components that should be gitignored
  8. const dist = 'dist'
  9. // Build React components
  10. // This loops through the react components and transpiles them to /dist
  11. // so they can be used by Node.js when we do server side rendering
  12. const tree = dirTree('./react/')
  13. if (tree) {
  14. for (const index in tree.children) {
  15. const file = tree.children[index]
  16. if (file.type === 'file' && file.extension === '.js') {
  17. if (!fs.existsSync(path.join(dist, 'react'))) {
  18. fs.mkdirSync(path.join(dist, 'react'), { recursive: true })
  19. }
  20. const content = transform(fs.readFileSync(file.path, 'utf8'))
  21. fs.writeFileSync(path.join(dist, file.path), content)
  22. }
  23. }
  24. }
  25. // End Build React Components
  26. // Register components
  27. const components = {
  28. // CodeBlock: require('../../dist/react/CodeBlock'),
  29. // CodeEditor: require('../../dist/react/CodeEditor')
  30. }
  31. const renderReact = async componentStr => {
  32. // Get component name as string so we can use it in the class name
  33. // which will be needed later if we choose to do client side React hydration
  34. const componentName = componentStr.match(/<([a-zA-Z]+)\s/)[1]
  35. // Add the wrapper and class name so we can later use React hydration on the client
  36. // side
  37. const jsx = `<div className="react-component-${componentName}">\n${componentStr}\n</div>`
  38. const component = transform(jsx)
  39. // eslint-disable-next-line
  40. const getComponent = new Function(
  41. 'React',
  42. ...Object.keys(components),
  43. `${component.replace('React', 'return React')}`
  44. )
  45. return renderToString(getComponent(React, ...Object.values(components)))
  46. }
  47. module.exports = {
  48. renderReact
  49. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...