diff --git a/packages/core/src/webpack-plugin.js b/packages/core/src/webpack-plugin.js index e4eb940..d22ffe1 100644 --- a/packages/core/src/webpack-plugin.js +++ b/packages/core/src/webpack-plugin.js @@ -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'); diff --git a/packages/http/src/client/index.ejs b/packages/http/src/client/index.ejs index 4ab519a..dd89bbe 100644 --- a/packages/http/src/client/index.ejs +++ b/packages/http/src/client/index.ejs @@ -6,6 +6,7 @@ <%= htmlWebpackPlugin.options.title %> +
diff --git a/packages/http/src/latus.js b/packages/http/src/latus.js index ddbef3b..bfb7983 100644 --- a/packages/http/src/latus.js +++ b/packages/http/src/latus.js @@ -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}"));`); +}; diff --git a/packages/http/src/server.js b/packages/http/src/server.js index e74178a..d91bd9d 100644 --- a/packages/http/src/server.js +++ b/packages/http/src/server.js @@ -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({