refactor: latus

This commit is contained in:
cha0s 2020-12-17 01:35:10 -06:00
parent b0978ddbc3
commit f5a8f70bfe
3 changed files with 66 additions and 81 deletions

View File

@ -1,6 +1,9 @@
import {join} from 'path';
import D from 'debug'; import D from 'debug';
import Middleware from './middleware'; import Middleware from './middleware';
import readConfig from './read-config';
const debug = D('@latus/core/latus'); const debug = D('@latus/core/latus');
@ -49,6 +52,17 @@ export default class Latus {
debug('latus config: %O', this.config); debug('latus config: %O', this.config);
} }
static create(config = readConfig()) {
// eslint-disable-next-line no-eval
const R = eval('require');
const paths = Latus.runtimePaths(config);
return new Latus({
config,
modules: paths.map((path) => R(path)),
});
}
invoke(hook, ...args) { invoke(hook, ...args) {
if (!this.hooks[hook]) { if (!this.hooks[hook]) {
return []; return [];
@ -137,4 +151,21 @@ export default class Latus {
} }
} }
static runtimePath(path) {
// eslint-disable-next-line no-eval
const R = eval('require');
try {
const local = join(process.cwd(), 'src', path);
R.resolve(local);
return local;
}
catch (error) {
return path;
}
}
static runtimePaths(config) {
return Object.keys(config).map((path) => this.runtimePath(path));
}
} }

View File

@ -1,29 +1,9 @@
import {join} from 'path';
import Latus from './latus'; import Latus from './latus';
import readConfig from './read-config';
process.stdout.write('Latus starting...\n'); process.stdout.write('Latus starting...\n');
// eslint-disable-next-line no-eval
const r = eval('require');
(async () => { (async () => {
const config = readConfig(); const latus = Latus.create();
const paths = Object.keys(config).map((plugin) => {
try {
const local = join(process.cwd(), 'src', plugin);
r.resolve(local);
return local;
}
catch (error) {
return plugin;
}
});
const latus = new Latus({
config,
modules: paths.map((path) => r(path)),
});
await latus.invokeSequential('@latus/core/up'); await latus.invokeSequential('@latus/core/up');
process.stdout.write('Latus up!\n'); process.stdout.write('Latus up!\n');
})(); })();

View File

@ -5,27 +5,12 @@ const fs = require('fs');
const web = require('@neutrinojs/web'); const web = require('@neutrinojs/web');
const {DefinePlugin, EnvironmentPlugin} = require('webpack'); const {DefinePlugin, EnvironmentPlugin} = require('webpack');
const {readConfig, Latus} = require('@latus/core'); const {Latus} = require('@latus/core');
const VirtualModulesPlugin = require('webpack-virtual-modules'); const VirtualModulesPlugin = require('webpack-virtual-modules');
const plugins = require('./plugins'); const plugins = require('./plugins');
const config = readConfig(); const latus = Latus.create();
const paths = Object.entries(config).map(([plugin]) => {
try {
const local = join(process.cwd(), 'src', plugin);
require.resolve(local);
return local;
}
catch (error) {
return plugin;
}
});
const latus = new Latus({
config,
// eslint-disable-next-line global-require, import/no-dynamic-require
modules: paths.map((path) => require(path)),
});
const { const {
HTTP_DEV_HOST, HTTP_DEV_HOST,
@ -71,10 +56,7 @@ const client = {
.public(HTTP_DEV_PUBLIC); .public(HTTP_DEV_PUBLIC);
} }
}, },
], (neutrino) => {
};
client.use.push((neutrino) => {
class LatusPlugin { class LatusPlugin {
constructor(latus) { constructor(latus) {
@ -90,17 +72,7 @@ client.use.push((neutrino) => {
}); });
compiler.hooks.beforeCompile.tapPromise('LatusPlugin', async () => { compiler.hooks.beforeCompile.tapPromise('LatusPlugin', async () => {
const paths = Object.keys(await plugins(this.latus)) const paths = Object.keys(await plugins(this.latus))
.map((plugin) => { .map((path) => Latus.runtimePath(path));
try {
const local = join(process.cwd(), 'src', plugin);
// eslint-disable-next-line no-eval
eval('require.resolve')(local);
return local;
}
catch (error) {
return plugin;
}
});
plugin.writeModule('node_modules/@latus/core/virtual', [ plugin.writeModule('node_modules/@latus/core/virtual', [
'/* eslint-disable global-require, no-undef */', '/* eslint-disable global-require, no-undef */',
'window.$$latus = {', 'window.$$latus = {',
@ -115,7 +87,9 @@ client.use.push((neutrino) => {
neutrino.config neutrino.config
.plugin('latus') .plugin('latus')
.use(LatusPlugin, [latus]); .use(LatusPlugin, [latus]);
}); },
],
};
latus.invokeFlat('@latus/http/build', client); latus.invokeFlat('@latus/http/build', client);
client.use.push((neutrino) => { client.use.push((neutrino) => {
const {title} = latus.config['@latus/http']; const {title} = latus.config['@latus/http'];