From f5a8f70bfe48087c01e5d37451f0c870df791373 Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 17 Dec 2020 01:35:10 -0600 Subject: [PATCH] refactor: latus --- packages/core/src/latus.js | 31 +++++++++ packages/core/src/start.js | 22 +----- packages/http/src/build/.neutrinorc.js | 94 ++++++++++---------------- 3 files changed, 66 insertions(+), 81 deletions(-) diff --git a/packages/core/src/latus.js b/packages/core/src/latus.js index bdcb364..938f440 100644 --- a/packages/core/src/latus.js +++ b/packages/core/src/latus.js @@ -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)); + } + } diff --git a/packages/core/src/start.js b/packages/core/src/start.js index 10031ef..2b7bf5c 100644 --- a/packages/core/src/start.js +++ b/packages/core/src/start.js @@ -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'); })(); diff --git a/packages/http/src/build/.neutrinorc.js b/packages/http/src/build/.neutrinorc.js index 159bf1f..80d619f 100644 --- a/packages/http/src/build/.neutrinorc.js +++ b/packages/http/src/build/.neutrinorc.js @@ -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'];