refactor: web config
This commit is contained in:
parent
0c8ff56850
commit
a2e775b64f
|
@ -2,4 +2,3 @@ export {dump as dumpYml, load as loadYml} from 'js-yaml';
|
|||
export {default as webpack} from 'webpack';
|
||||
|
||||
export * from '../build/webpack';
|
||||
|
||||
|
|
|
@ -7,3 +7,9 @@ export {
|
|||
ByType,
|
||||
Flecks,
|
||||
} from '../build/flecks';
|
||||
|
||||
export const hooks = {
|
||||
'@flecks/web.config': async (req, flecks) => ({
|
||||
id: flecks.get('@flecks/core.id'),
|
||||
}),
|
||||
};
|
||||
|
|
|
@ -1,11 +1,3 @@
|
|||
export {glob} from 'glob';
|
||||
|
||||
export {JsonStream, transform} from '../../build/stream';
|
||||
|
||||
export const hooks = {
|
||||
'@flecks/web.config': async (req, flecks) => ({
|
||||
'@flecks/core': {
|
||||
id: flecks.get('@flecks/core.id'),
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
|
|
@ -10,7 +10,7 @@ export const hooks = {
|
|||
const slices = await flecks.invokeMergeUnique('@flecks/redux.slices');
|
||||
const reducer = createReducer(flecks, slices);
|
||||
// Hydrate from server.
|
||||
const {preloadedState} = flecks.get('@flecks/redux/client');
|
||||
const {preloadedState} = flecks.get('@flecks/redux');
|
||||
const store = await configureStore(flecks, reducer, {preloadedState});
|
||||
flecks.redux = store;
|
||||
return [Provider, {store}];
|
||||
|
|
|
@ -7,4 +7,7 @@ export * from './actions';
|
|||
|
||||
export const hooks = {
|
||||
'@flecks/socket.packets': Flecks.provide(require.context('./packets', false, /\.js$/)),
|
||||
'@flecks/web.config': async (req) => ({
|
||||
preloadedState: req.redux.getState(),
|
||||
}),
|
||||
};
|
||||
|
|
|
@ -30,9 +30,4 @@ export const hooks = {
|
|||
req.redux = await configureStore(flecks, reducer, {preloadedState});
|
||||
next();
|
||||
},
|
||||
'@flecks/web.config': async (req) => ({
|
||||
'@flecks/redux/client': {
|
||||
preloadedState: req.redux.getState(),
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
|
|
@ -11,21 +11,17 @@ export const hooks = {
|
|||
'@flecks/core.starting': (flecks) => {
|
||||
flecks.socket.Packets = flecks.gather('@flecks/socket.packets', {check: badPacketsCheck});
|
||||
},
|
||||
'@flecks/web.config': async (
|
||||
req,
|
||||
{config: {'@flecks/socket': {'packets.decorate': decorators = ['...']}}},
|
||||
) => ({
|
||||
'@flecks/socket': {
|
||||
'packets.decorate': decorators.filter(
|
||||
(decorator) => 'server' !== decorator.split('/').pop(),
|
||||
),
|
||||
},
|
||||
}),
|
||||
'@flecks/socket.packets': (flecks) => ({
|
||||
Bundle: Bundle(flecks),
|
||||
Redirect,
|
||||
Refresh,
|
||||
}),
|
||||
'@flecks/web.config': async (req, flecks) => ({
|
||||
'packets.decorate': (
|
||||
flecks.get('@flecks/socket.packets.decorate', ['...'])
|
||||
.filter((decorator) => 'server' !== decorator.split('/').pop())
|
||||
),
|
||||
}),
|
||||
};
|
||||
|
||||
export const mixin = (Flecks) => class FlecksWithSocket extends Flecks {
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
},
|
||||
"files": [
|
||||
"entry.js",
|
||||
"index.js",
|
||||
"runtime.js",
|
||||
"server.js",
|
||||
"server/build/entry.js",
|
||||
|
|
6
packages/web/src/index.js
Normal file
6
packages/web/src/index.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
export const hooks = {
|
||||
'@flecks/web.config': async (req, flecks) => {
|
||||
const {appMountId, title} = flecks.get('@flecks/web');
|
||||
return {appMountId, title};
|
||||
},
|
||||
};
|
|
@ -4,30 +4,11 @@ const {
|
|||
NODE_ENV,
|
||||
} = process.env;
|
||||
|
||||
export const configSource = async (flecks, req) => {
|
||||
req.onlyAllow = (object, keys) => (
|
||||
Object.fromEntries(
|
||||
Object.entries(object)
|
||||
.map(([key, value]) => [key, keys.includes(key) ? value : undefined]),
|
||||
)
|
||||
);
|
||||
const httpConfig = await flecks.invokeMergeAsync('@flecks/web.config', req);
|
||||
const config = Object.fromEntries(
|
||||
Object.entries(flecks.web.config)
|
||||
.filter(([path]) => !path.endsWith('/server'))
|
||||
.map(([path, config]) => [path, {...config, ...httpConfig[path]}]),
|
||||
);
|
||||
// Fold in any bespoke configuration.
|
||||
Object.keys(httpConfig)
|
||||
.forEach((key) => {
|
||||
if (!(key in config)) {
|
||||
config[key] = httpConfig[key];
|
||||
}
|
||||
});
|
||||
return `window[Symbol.for('@flecks/web.config')] = JSON.parse(decodeURIComponent("${
|
||||
encodeURIComponent(JSON.stringify(config))
|
||||
}"));`;
|
||||
};
|
||||
export const configSource = async (flecks, req) => (
|
||||
`window[Symbol.for('@flecks/web.config')] = JSON.parse(decodeURIComponent("${
|
||||
encodeURIComponent(JSON.stringify(await flecks.invokeAsync('@flecks/web.config', req)))
|
||||
}"));`
|
||||
);
|
||||
|
||||
class InlineConfig extends Transform {
|
||||
|
||||
|
|
|
@ -8,9 +8,6 @@ const debug = D('@flecks/web/server');
|
|||
export {configSource};
|
||||
|
||||
export const hooks = {
|
||||
'@flecks/web.config': async ({onlyAllow}, flecks) => ({
|
||||
'@flecks/web': onlyAllow(flecks.get('@flecks/web'), ['appMountId', 'title']),
|
||||
}),
|
||||
'@flecks/web.routes': (flecks) => [
|
||||
{
|
||||
method: 'get',
|
||||
|
|
Loading…
Reference in New Issue
Block a user