refactor: JIT config
This commit is contained in:
parent
94a8eb94a8
commit
cd65fdbe2b
|
@ -7,13 +7,16 @@ import webpack, {HotModuleReplacementPlugin} from 'webpack';
|
|||
import devMiddleware from 'webpack-dev-middleware';
|
||||
import hotMiddleware from 'webpack-hot-middleware';
|
||||
|
||||
import plugins from './plugins';
|
||||
|
||||
// TODO watch latus.yml.
|
||||
|
||||
const {
|
||||
NODE_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({
|
||||
root: process.cwd(),
|
||||
use: [
|
||||
|
@ -24,7 +27,7 @@ const common = async (latus, {latusClientConfig: config}) => {
|
|||
.add(`${__dirname}/client/latus`);
|
||||
neutrino.config
|
||||
.plugin('WebpackPlugin')
|
||||
.use(WebpackPlugin, [Object.keys(config)]);
|
||||
.use(WebpackPlugin, [paths]);
|
||||
},
|
||||
(neutrino) => {
|
||||
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 configMiddleware = (latus) => (req, res, next) => {
|
||||
const {latusClientConfig: config} = req;
|
||||
export const configMiddleware = (latus) => async (req, res, next) => {
|
||||
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));
|
||||
const codedConfig = encodeURIComponent(JSON.stringify(await plugins(latus, req)));
|
||||
res.send(`window.$$latus.config = JSON.parse(decodeURIComponent("${codedConfig}"));`);
|
||||
};
|
||||
|
|
27
packages/http/src/plugins.js
Normal file
27
packages/http/src/plugins.js
Normal 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,
|
||||
};
|
||||
};
|
|
@ -26,29 +26,6 @@ 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(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
|
||||
|
|
Loading…
Reference in New Issue
Block a user