1
0
mirror of synced 2025-12-22 11:26:57 -05:00

Keep all the React Engine logic in one file

This commit is contained in:
Chiedo
2020-10-21 09:25:50 -04:00
parent fe812048a4
commit a48998c789
2 changed files with 23 additions and 21 deletions

View File

@@ -1,7 +1,30 @@
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/')
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 = {
CodeBlock: require('../../dist/react/CodeBlock'),
CodeEditor: require('../../dist/react/CodeEditor')

View File

@@ -5,30 +5,9 @@ require('./lib/feature-flags')
const express = require('express')
const portUsed = require('port-used')
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 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)
// prevent the app from starting up durings tests