refactor: closer to server HMR...

This commit is contained in:
cha0s 2021-01-20 14:24:14 -06:00
parent 11977f77b4
commit 0866fa8cbf
4 changed files with 64 additions and 7 deletions

View File

@ -30,7 +30,7 @@ module.exports = {
}),
mocha(),
node({
hot: false,
hot: true,
}),
(neutrino) => {
[
@ -65,7 +65,7 @@ module.exports = {
}]);
neutrino.config
.entry('index')
.prepend('@latus/core/start');
.add('@latus/core/start');
if ('production' !== neutrino.config.get('mode')) {
neutrino.config
.entry('index')
@ -74,6 +74,7 @@ module.exports = {
.plugin('start-server')
.tap((args) => {
const options = args[0];
options.signal = true;
const inspectArg = process.argv.find((arg) => -1 !== arg.indexOf('--inspect'));
if (inspectArg) {
options.nodeArgs.push(inspectArg);

View File

@ -1,3 +1,5 @@
import {LatusPlugin} from './latus-plugin';
export {
unique as arrayUnique,
flatten as arrayFlatten,
@ -9,15 +11,68 @@ export {default as EventEmitter} from './event-emitter';
export {deflate, inflate} from './flate';
export {compose, fastApply} from './function';
export {decorateWithLatus, gatherWithLatus, default as gather} from './gather';
export {LatusPlugin} from './latus-plugin';
export {default as Middleware} from './middleware';
export {default as Latus} from './latus';
export * from './string';
export {LatusPlugin};
export const Class = class {};
export default {
hooks: {
'@latus/core/build': (configs, latus) => {
// eslint-disable-next-line no-undef
configs.app.use.unshift((neutrino) => {
const defaults = Object.keys(latus.originalConfig)
.map((plugin) => {
const config = {};
[plugin, `${plugin}/server`].forEach((path) => {
try {
// eslint-disable-next-line no-eval
eval('require.resolve')(path);
config[path] = {};
}
// eslint-disable-next-line no-empty
catch (error) {}
});
return config;
})
.filter((config) => !!config)
.reduce((r, o) => ({...r, ...o}), {});
const config = Object.fromEntries(
Object.keys(defaults)
.map((path) => [
path,
latus.get(path),
]),
);
neutrino.config
.plugin('latus')
.use(LatusPlugin(
neutrino,
() => config,
(plugin, modulesObject) => {
plugin.writeModule('/%/latus/server/virtual', [
'global.$$latus = {',
` config: ${JSON.stringify(config)},`,
` modules: ${modulesObject},`,
'};',
'if (module.hot) {',
' Object.keys(global.$$latus.modules).forEach((key) => {',
' module.hot.accept(key, () => {',
" console.log('kesy!');",
' });',
' });',
'}',
].join('\n'));
},
));
neutrino.config
.entry('index')
.add('/%/latus/server/virtual');
});
},
'@latus/core/config': () => ({
id: 'latus',
}),

View File

@ -22,7 +22,8 @@ exports.LatusPlugin = (neutrino, getConfig, write) => class LatusPlugin {
});
compiler.hooks.beforeCompile.tapPromise('LatusPlugin', async () => {
const paths = Object.keys(await getConfig())
.map((path) => Latus.runtimePath(path));
.map((path) => Latus.runtimePath(path))
.filter((path) => !!path);
const pathMap = paths.map((path) => `'${path}': require('${path}')`).join(', ');
write(plugin, `{${pathMap}}`, paths);
});

View File

@ -1,14 +1,14 @@
/* eslint-disable no-console */
import Latus from './latus';
process.stdout.write('Latus starting...\n');
console.log('Latus starting...');
(async () => {
const latus = Latus.create();
const latus = new Latus(global.$$latus);
try {
await Promise.all(latus.invokeFlat('@latus/core/starting'));
await Promise.all(latus.invokeFlat('@latus/core/started'));
await latus.invokeSequential('@latus/core/server/up');
// eslint-disable-next-line no-console
console.log('Latus up!');
}
catch (error) {