refactor: LatusPlugin

This commit is contained in:
cha0s 2021-01-20 13:15:07 -06:00
parent 671e23b989
commit 183c5ab9a6
6 changed files with 53 additions and 43 deletions

View File

@ -33,7 +33,8 @@
"lodash.flatten": "^4.4.0", "lodash.flatten": "^4.4.0",
"lodash.get": "^4.4.2", "lodash.get": "^4.4.2",
"lodash.set": "^4.3.2", "lodash.set": "^4.3.2",
"mkdirp": "^1.0.4" "mkdirp": "^1.0.4",
"webpack-virtual-modules": "^0.4.1"
}, },
"devDependencies": { "devDependencies": {
"@neutrinojs/airbnb-base": "^9.4.0", "@neutrinojs/airbnb-base": "^9.4.0",

View File

@ -9,6 +9,7 @@ export {default as EventEmitter} from './event-emitter';
export {deflate, inflate} from './flate'; export {deflate, inflate} from './flate';
export {compose, fastApply} from './function'; export {compose, fastApply} from './function';
export {decorateWithLatus, gatherWithLatus, default as gather} from './gather'; export {decorateWithLatus, gatherWithLatus, default as gather} from './gather';
export {LatusPlugin} from './latus-plugin';
export {default as Middleware} from './middleware'; export {default as Middleware} from './middleware';
export {default as Latus} from './latus'; export {default as Latus} from './latus';
export * from './string'; export * from './string';

View File

@ -0,0 +1,31 @@
const VirtualModulesPlugin = require('webpack-virtual-modules');
const {default: Latus} = require('./latus');
exports.LatusPlugin = (neutrino, getConfig, write) => class LatusPlugin {
// eslint-disable-next-line class-methods-use-this
apply(compiler) {
const plugin = new VirtualModulesPlugin();
plugin.apply(compiler);
let babelOptions;
neutrino.config.module
.rule('compile')
.use('babel')
.tap((options) => {
babelOptions = options;
});
// eslint-disable-next-line no-eval
eval('require')('@babel/register')({
plugins: babelOptions.plugins,
presets: babelOptions.presets,
});
compiler.hooks.beforeCompile.tapPromise('LatusPlugin', async () => {
const paths = Object.keys(await getConfig())
.map((path) => Latus.runtimePath(path));
const pathMap = paths.map((path) => `'${path}': require('${path}')`).join(', ');
write(plugin, `{${pathMap}}`, paths);
});
}
};

View File

@ -6070,6 +6070,11 @@ webpack-sources@^1.1.0, webpack-sources@^1.4.0, webpack-sources@^1.4.1:
source-list-map "^2.0.0" source-list-map "^2.0.0"
source-map "~0.6.1" source-map "~0.6.1"
webpack-virtual-modules@^0.4.1:
version "0.4.1"
resolved "http://npm.cha0sdev/webpack-virtual-modules/-/webpack-virtual-modules-0.4.1.tgz#cae5a7085d34331d077225f77037bea233dbfdad"
integrity sha512-BH/RKOHk223WdBDLFqghztx3DF5AqR3CKg3ue1KN9S1SAaXP68Kj/4rF0lsdysxXaanzx7aWl1u0+lnfj7+OtQ==
webpack@^4: webpack@^4:
version "4.46.0" version "4.46.0"
resolved "http://npm.cha0sdev/webpack/-/webpack-4.46.0.tgz#bf9b4404ea20a073605e0a011d188d77cb6ad542" resolved "http://npm.cha0sdev/webpack/-/webpack-4.46.0.tgz#bf9b4404ea20a073605e0a011d188d77cb6ad542"

View File

@ -5,7 +5,7 @@ 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 {Latus} = require('@latus/core'); const {Latus, LatusPlugin} = require('@latus/core');
const VirtualModulesPlugin = require('webpack-virtual-modules'); const VirtualModulesPlugin = require('webpack-virtual-modules');
const clientPlugins = require('./client-plugins'); const clientPlugins = require('./client-plugins');
@ -56,36 +56,16 @@ const client = {
}]); }]);
}, },
(neutrino) => { (neutrino) => {
class LatusPlugin { neutrino.config
.plugin('latus')
constructor(latus) { .use(LatusPlugin(
this.latus = latus; neutrino,
} () => clientPlugins(latus),
(plugin, modulesObject, paths) => {
apply(compiler) { plugin.writeModule('node_modules/@latus/core/client/virtual', [
const plugin = new VirtualModulesPlugin(); 'export default {',
plugin.apply(compiler);
let babelOptions;
neutrino.config.module
.rule('compile')
.use('babel')
.tap(options => {
babelOptions = options;
});
const {plugins, presets, ...rest} = babelOptions;
// eslint-disable-next-line no-eval
eval('require')('@babel/register')({
plugins,
presets,
});
compiler.hooks.beforeCompile.tapPromise('LatusPlugin', async () => {
const paths = Object.keys(await clientPlugins(this.latus))
.map((path) => Latus.runtimePath(path));
const pathMap = paths.map((path) => `'${path}': require('${path}')`).join(', ');
plugin.writeModule('node_modules/@latus/core/virtual', [
'window.$$latus = {',
' config: window.$$latusConfig,', ' config: window.$$latusConfig,',
` modules: {${pathMap}},`, ` modules: ${modulesObject},`,
'};', '};',
].join('\n')); ].join('\n'));
const testPaths = paths const testPaths = paths
@ -94,13 +74,8 @@ const client = {
plugin.writeModule(`node_modules/@latus/core/tests`, testPaths.map( plugin.writeModule(`node_modules/@latus/core/tests`, testPaths.map(
([original, path]) => `describe('${original}', () => require('${path}'));` ([original, path]) => `describe('${original}', () => require('${path}'));`
).join('')); ).join(''));
}); },
} ));
}
neutrino.config
.plugin('latus')
.use(LatusPlugin, [latus]);
neutrino.config.resolve.modules.merge([ neutrino.config.resolve.modules.merge([
join(process.cwd(), 'node_modules'), join(process.cwd(), 'node_modules'),
'node_modules', 'node_modules',

View File

@ -1,12 +1,9 @@
import {Latus} from '@latus/core'; import {Latus} from '@latus/core';
// eslint-disable-next-line import/no-unresolved // eslint-disable-next-line import/no-unresolved
import '@latus/core/virtual'; import config from '@latus/core/client/virtual';
(async () => { (async () => {
if (!window.$$latus) { const latus = new Latus(config);
return;
}
const latus = new Latus(window.$$latus);
await Promise.all(latus.invokeFlat('@latus/core/starting')); await Promise.all(latus.invokeFlat('@latus/core/starting'));
await Promise.all(latus.invokeFlat('@latus/core/started')); await Promise.all(latus.invokeFlat('@latus/core/started'));
await latus.invokeSequential('@latus/http/client/up'); await latus.invokeSequential('@latus/http/client/up');