From a48998c7890b71c8f58eda1fa31b50df348a0042 Mon Sep 17 00:00:00 2001 From: Chiedo Date: Wed, 21 Oct 2020 09:25:50 -0400 Subject: [PATCH] Keep all the React Engine logic in one file --- lib/react/engine.js | 23 +++++++++++++++++++++++ server.js | 21 --------------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/react/engine.js b/lib/react/engine.js index 2e5b169027..cbf56e7179 100644 --- a/lib/react/engine.js +++ b/lib/react/engine.js @@ -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') diff --git a/server.js b/server.js index ccc7eea059..5cf817bdb4 100644 --- a/server.js +++ b/server.js @@ -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