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 Middleware from './middleware';
import readConfig from './read-config';
const debug = D('@latus/core/latus');
@ -49,6 +52,17 @@ export default class Latus {
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) {
if (!this.hooks[hook]) {
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 readConfig from './read-config';
process.stdout.write('Latus starting...\n');
// eslint-disable-next-line no-eval
const r = eval('require');
(async () => {
const config = readConfig();
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)),
});
const latus = Latus.create();
await latus.invokeSequential('@latus/core/up');
process.stdout.write('Latus up!\n');
})();

View File

@ -5,27 +5,12 @@ const fs = require('fs');
const web = require('@neutrinojs/web');
const {DefinePlugin, EnvironmentPlugin} = require('webpack');
const {readConfig, Latus} = require('@latus/core');
const {Latus} = require('@latus/core');
const VirtualModulesPlugin = require('webpack-virtual-modules');
const plugins = require('./plugins');
const config = readConfig();
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 latus = Latus.create();
const {
HTTP_DEV_HOST,
@ -71,51 +56,40 @@ const client = {
.public(HTTP_DEV_PUBLIC);
}
},
(neutrino) => {
class LatusPlugin {
constructor(latus) {
this.latus = latus;
}
apply(compiler) {
const plugin = new VirtualModulesPlugin();
plugin.apply(compiler);
// eslint-disable-next-line no-eval
eval('require')('@babel/register')({
presets: ['@babel/preset-react'],
});
compiler.hooks.beforeCompile.tapPromise('LatusPlugin', async () => {
const paths = Object.keys(await plugins(this.latus))
.map((path) => Latus.runtimePath(path));
plugin.writeModule('node_modules/@latus/core/virtual', [
'/* eslint-disable global-require, no-undef */',
'window.$$latus = {',
' config: window.$$latusConfig,',
` modules: [${paths.map((path) => `require('${path}')`).join(',')}],`,
'};',
].join('\n'));
});
}
}
neutrino.config
.plugin('latus')
.use(LatusPlugin, [latus]);
},
],
};
client.use.push((neutrino) => {
class LatusPlugin {
constructor(latus) {
this.latus = latus;
}
apply(compiler) {
const plugin = new VirtualModulesPlugin();
plugin.apply(compiler);
// eslint-disable-next-line no-eval
eval('require')('@babel/register')({
presets: ['@babel/preset-react'],
});
compiler.hooks.beforeCompile.tapPromise('LatusPlugin', async () => {
const paths = Object.keys(await plugins(this.latus))
.map((plugin) => {
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', [
'/* eslint-disable global-require, no-undef */',
'window.$$latus = {',
' config: window.$$latusConfig,',
` modules: [${paths.map((path) => `require('${path}')`).join(',')}],`,
'};',
].join('\n'));
});
}
}
neutrino.config
.plugin('latus')
.use(LatusPlugin, [latus]);
});
latus.invokeFlat('@latus/http/build', client);
client.use.push((neutrino) => {
const {title} = latus.config['@latus/http'];