refactor: JIT config

This commit is contained in:
cha0s 2020-12-10 00:13:04 -06:00
parent 94a8eb94a8
commit cd65fdbe2b
3 changed files with 34 additions and 29 deletions

View File

@ -7,13 +7,16 @@ import webpack, {HotModuleReplacementPlugin} from 'webpack';
import devMiddleware from 'webpack-dev-middleware'; import devMiddleware from 'webpack-dev-middleware';
import hotMiddleware from 'webpack-hot-middleware'; import hotMiddleware from 'webpack-hot-middleware';
import plugins from './plugins';
// TODO watch latus.yml. // TODO watch latus.yml.
const { const {
NODE_ENV, NODE_ENV,
} = process.env; } = process.env;
const common = async (latus, {latusClientConfig: config}) => { const common = async (latus, req) => {
const paths = Object.keys(await plugins(latus, req));
const instance = neutrino({ const instance = neutrino({
root: process.cwd(), root: process.cwd(),
use: [ use: [
@ -24,7 +27,7 @@ const common = async (latus, {latusClientConfig: config}) => {
.add(`${__dirname}/client/latus`); .add(`${__dirname}/client/latus`);
neutrino.config neutrino.config
.plugin('WebpackPlugin') .plugin('WebpackPlugin')
.use(WebpackPlugin, [Object.keys(config)]); .use(WebpackPlugin, [paths]);
}, },
(neutrino) => { (neutrino) => {
latus.invokeFlat('@latus/http/build').forEach((middleware) => neutrino.use(middleware)); latus.invokeFlat('@latus/http/build').forEach((middleware) => neutrino.use(middleware));
@ -125,14 +128,12 @@ const production = (latus) => vary(
export const modulesMiddleware = ('production' === NODE_ENV ? production : dev); export const modulesMiddleware = ('production' === NODE_ENV ? production : dev);
export const configMiddleware = (latus) => (req, res, next) => { export const configMiddleware = (latus) => async (req, res, next) => {
const {latusClientConfig: config} = req;
res.setHeader('Content-Type', 'application/javascript; charset=UTF-8'); res.setHeader('Content-Type', 'application/javascript; charset=UTF-8');
latus.invoke('@reddichat/core/config.alter', config);
if ('/latus.config.js' !== req.path) { if ('/latus.config.js' !== req.path) {
next(); next();
return; return;
} }
const codedConfig = encodeURIComponent(JSON.stringify(config)); const codedConfig = encodeURIComponent(JSON.stringify(await plugins(latus, req)));
res.send(`window.$$latus.config = JSON.parse(decodeURIComponent("${codedConfig}"));`); res.send(`window.$$latus.config = JSON.parse(decodeURIComponent("${codedConfig}"));`);
}; };

View File

@ -0,0 +1,27 @@
export default async (latus, req) => {
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}), {});
return {
...defaults,
...config,
};
};

View File

@ -26,29 +26,6 @@ export const createHttpServer = async (latus) => {
const routes = arrayFlatten(latus.invokeFlat('@latus/http/routes')); const routes = arrayFlatten(latus.invokeFlat('@latus/http/routes'));
routes.forEach(({method, path, handler}) => app[method](path, handler)); routes.forEach(({method, path, handler}) => app[method](path, handler));
// Serve latus. // Serve 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(configMiddleware(latus));
app.use(modulesMiddleware(latus)); app.use(modulesMiddleware(latus));
// eslint-disable-next-line no-eval // eslint-disable-next-line no-eval