refactor: LatusPlugin
This commit is contained in:
parent
671e23b989
commit
183c5ab9a6
|
@ -33,7 +33,8 @@
|
|||
"lodash.flatten": "^4.4.0",
|
||||
"lodash.get": "^4.4.2",
|
||||
"lodash.set": "^4.3.2",
|
||||
"mkdirp": "^1.0.4"
|
||||
"mkdirp": "^1.0.4",
|
||||
"webpack-virtual-modules": "^0.4.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@neutrinojs/airbnb-base": "^9.4.0",
|
||||
|
|
|
@ -9,6 +9,7 @@ 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';
|
||||
|
|
31
packages/core/src/latus-plugin.js
Normal file
31
packages/core/src/latus-plugin.js
Normal 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);
|
||||
});
|
||||
}
|
||||
|
||||
};
|
|
@ -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-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:
|
||||
version "4.46.0"
|
||||
resolved "http://npm.cha0sdev/webpack/-/webpack-4.46.0.tgz#bf9b4404ea20a073605e0a011d188d77cb6ad542"
|
||||
|
|
|
@ -5,7 +5,7 @@ const fs = require('fs');
|
|||
const web = require('@neutrinojs/web');
|
||||
const {DefinePlugin, EnvironmentPlugin} = require('webpack');
|
||||
|
||||
const {Latus} = require('@latus/core');
|
||||
const {Latus, LatusPlugin} = require('@latus/core');
|
||||
const VirtualModulesPlugin = require('webpack-virtual-modules');
|
||||
|
||||
const clientPlugins = require('./client-plugins');
|
||||
|
@ -56,36 +56,16 @@ const client = {
|
|||
}]);
|
||||
},
|
||||
(neutrino) => {
|
||||
class LatusPlugin {
|
||||
|
||||
constructor(latus) {
|
||||
this.latus = latus;
|
||||
}
|
||||
|
||||
apply(compiler) {
|
||||
const plugin = new VirtualModulesPlugin();
|
||||
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 = {',
|
||||
neutrino.config
|
||||
.plugin('latus')
|
||||
.use(LatusPlugin(
|
||||
neutrino,
|
||||
() => clientPlugins(latus),
|
||||
(plugin, modulesObject, paths) => {
|
||||
plugin.writeModule('node_modules/@latus/core/client/virtual', [
|
||||
'export default {',
|
||||
' config: window.$$latusConfig,',
|
||||
` modules: {${pathMap}},`,
|
||||
` modules: ${modulesObject},`,
|
||||
'};',
|
||||
].join('\n'));
|
||||
const testPaths = paths
|
||||
|
@ -94,13 +74,8 @@ const client = {
|
|||
plugin.writeModule(`node_modules/@latus/core/tests`, testPaths.map(
|
||||
([original, path]) => `describe('${original}', () => require('${path}'));`
|
||||
).join(''));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
neutrino.config
|
||||
.plugin('latus')
|
||||
.use(LatusPlugin, [latus]);
|
||||
},
|
||||
));
|
||||
neutrino.config.resolve.modules.merge([
|
||||
join(process.cwd(), 'node_modules'),
|
||||
'node_modules',
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
import {Latus} from '@latus/core';
|
||||
// eslint-disable-next-line import/no-unresolved
|
||||
import '@latus/core/virtual';
|
||||
import config from '@latus/core/client/virtual';
|
||||
|
||||
(async () => {
|
||||
if (!window.$$latus) {
|
||||
return;
|
||||
}
|
||||
const latus = new Latus(window.$$latus);
|
||||
const latus = new Latus(config);
|
||||
await Promise.all(latus.invokeFlat('@latus/core/starting'));
|
||||
await Promise.all(latus.invokeFlat('@latus/core/started'));
|
||||
await latus.invokeSequential('@latus/http/client/up');
|
||||
|
|
Loading…
Reference in New Issue
Block a user