Keep all the React Engine logic in one file
This commit is contained in:
@@ -1,7 +1,30 @@
|
|||||||
const { renderToString } = require('react-dom/server')
|
const { renderToString } = require('react-dom/server')
|
||||||
const { transform } = require('./babel')
|
const { transform } = require('./babel')
|
||||||
const React = require('react')
|
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/')
|
||||||
|
for (const index in tree.children) {
|
||||||
|
const file = tree.children[index]
|
||||||
|
if (file.type === 'file') {
|
||||||
|
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 = {
|
const components = {
|
||||||
CodeBlock: require('../../dist/react/CodeBlock'),
|
CodeBlock: require('../../dist/react/CodeBlock'),
|
||||||
CodeEditor: require('../../dist/react/CodeEditor')
|
CodeEditor: require('../../dist/react/CodeEditor')
|
||||||
|
|||||||
21
server.js
21
server.js
@@ -5,30 +5,9 @@ require('./lib/feature-flags')
|
|||||||
const express = require('express')
|
const express = require('express')
|
||||||
const portUsed = require('port-used')
|
const portUsed = require('port-used')
|
||||||
const warmServer = require('./lib/warm-server')
|
const warmServer = require('./lib/warm-server')
|
||||||
const fs = require('fs')
|
|
||||||
const path = require('path')
|
|
||||||
const dirTree = require('directory-tree')
|
|
||||||
const port = Number(process.env.PORT) || 4000
|
const port = Number(process.env.PORT) || 4000
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
// 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 { transform } = require('./lib/react/babel')
|
|
||||||
|
|
||||||
const tree = dirTree('./react/')
|
|
||||||
for (const index in tree.children) {
|
|
||||||
const file = tree.children[index]
|
|
||||||
if (file.type === 'file') {
|
|
||||||
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
|
|
||||||
|
|
||||||
require('./middleware')(app)
|
require('./middleware')(app)
|
||||||
|
|
||||||
// prevent the app from starting up durings tests
|
// prevent the app from starting up durings tests
|
||||||
|
|||||||
Reference in New Issue
Block a user