refactor: serving

This commit is contained in:
cha0s 2020-12-09 22:07:23 -06:00
parent a1ba60d9c6
commit 26b45bec44
4 changed files with 45 additions and 32 deletions

View File

@ -4,8 +4,8 @@ const VirtualModulesPlugin = require('webpack-virtual-modules');
export default class WebpackPlugin {
constructor(config) {
this.config = config;
constructor(paths) {
this.paths = paths;
}
apply(compiler) {
@ -13,7 +13,7 @@ export default class WebpackPlugin {
eval('require')('@babel/register')({
presets: ['@babel/preset-react'],
});
const paths = Object.keys(this.config).map((plugin) => {
const paths = this.paths.map((plugin) => {
try {
const local = join(process.cwd(), 'src', plugin);
// eslint-disable-next-line no-eval
@ -25,12 +25,9 @@ export default class WebpackPlugin {
}
});
const modules = paths.map((path) => `require('${path}')`).join(',');
const codedConfig = encodeURIComponent(JSON.stringify(this.config));
const body = [
'/* eslint-disable global-require, no-undef */',
`const config = JSON.parse(decodeURIComponent("${codedConfig}"));`,
'window.$$latus = {',
' config,',
` modules: [${modules}],`,
'};',
].join('\n');

View File

@ -6,6 +6,7 @@
<link rel="icon" href="/favicon.png"/>
<title><%= htmlWebpackPlugin.options.title %></title>
<script src="/latus.js"></script>
<script src="/latus.config.js"></script>
</head>
<body>
<div id="<%= htmlWebpackPlugin.options.appMountId %>">

View File

@ -13,8 +13,7 @@ const {
NODE_ENV,
} = process.env;
const common = async (latus, req) => {
const config = await latus.invokeReduceAsync('@latus/http/plugins', undefined, undefined, req);
const common = async (latus, {latusClientConfig: config}) => {
const instance = neutrino({
root: process.cwd(),
use: [
@ -23,29 +22,9 @@ const common = async (latus, req) => {
neutrino.config
.entry('latus')
.add(`${__dirname}/client/latus`);
const defaults = Object.keys(latus.config)
.map((plugin) => {
try {
const client = `${plugin}/client`;
// eslint-disable-next-line no-eval
eval('require.resolve')(client);
return {
[client]: {},
};
}
catch (error) {
return {};
}
})
.reduce((r, o) => ({...r, ...o}), {});
neutrino.config
.plugin('WebpackPlugin')
.use(WebpackPlugin, [
{
...defaults,
...config,
},
]);
.use(WebpackPlugin, [Object.keys(config)]);
},
(neutrino) => {
latus.invokeFlat('@latus/http/build').forEach((middleware) => neutrino.use(middleware));
@ -144,4 +123,16 @@ const production = (latus) => vary(
},
);
export default ('production' === NODE_ENV ? production : dev);
export const modulesMiddleware = ('production' === NODE_ENV ? production : dev);
export const configMiddleware = (latus) => (req, res, next) => {
const {latusClientConfig: config} = req;
res.setHeader('Content-Type', 'application/javascript; charset=UTF-8');
latus.invoke('@reddichat/core/config.alter', config);
if ('/latus.config.js' !== req.path) {
next();
return;
}
const codedConfig = encodeURIComponent(JSON.stringify(config));
res.send(`window.$$latus.config = JSON.parse(decodeURIComponent("${codedConfig}"));`);
};

View File

@ -6,7 +6,7 @@ import {arrayFlatten} from '@latus/core';
import express from 'express';
import httpProxy from 'http-proxy';
import latusMiddleware from './latus';
import {configMiddleware, modulesMiddleware} from './latus';
const {
HTTP_HOST,
@ -26,7 +26,31 @@ export const createHttpServer = async (latus) => {
const routes = arrayFlatten(latus.invokeFlat('@latus/http/routes'));
routes.forEach(({method, path, handler}) => app[method](path, handler));
// Serve latus.
app.use(latusMiddleware(latus));
app.use(async (req, res, next) => {
const config = await latus.invokeReduceAsync('@latus/http/plugins', undefined, undefined, req);
const defaults = Object.keys(latus.config)
.map((plugin) => {
try {
const client = `${plugin}/client`;
// eslint-disable-next-line no-eval
eval('require.resolve')(client);
return {
[client]: {},
};
}
catch (error) {
return {};
}
})
.reduce((r, o) => ({...r, ...o}), {});
req.latusClientConfig = {
...defaults,
...config,
};
next();
});
app.use(configMiddleware(latus));
app.use(modulesMiddleware(latus));
// eslint-disable-next-line no-eval
if ('production' !== eval('process.env.NODE_ENV')) {
const proxy = httpProxy.createProxyServer({