refactor: toward server HMR

This commit is contained in:
cha0s 2021-03-22 08:05:36 -05:00
parent eee0eff666
commit 44a183ca02
6 changed files with 76 additions and 43 deletions

View File

@ -3,18 +3,19 @@ const {
} = process.env;
module.exports = (api) => {
api.cache(false);
return {
plugins: [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-private-methods',
'@babel/plugin-syntax-jsx',
[
'babel-plugin-webpack-alias',
{
config: LATUS_WEBPACK_CONFIG,
},
]
api.cache(true);
const plugins = [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-private-methods',
'@babel/plugin-syntax-jsx',
[
'babel-plugin-webpack-alias',
{
config: LATUS_WEBPACK_CONFIG,
},
],
];
return {
plugins,
};
};

View File

@ -3,3 +3,4 @@
!/.*
!src/**/*.js
!/test/**/*.js
!/virtual.js

View File

@ -16,12 +16,15 @@
"client.js.map",
"index.js",
"index.js.map",
"loader.js",
"loader.js.map",
"server.js",
"server.js.map",
"start.js",
"start.js.map",
"test.js",
"test.js.map"
"test.js.map",
"virtual.js"
],
"dependencies": {
"@neutrinojs/copy": "^9.4.0",
@ -29,6 +32,7 @@
"autoprefixer": "^9.8.6",
"debug": "4.3.1",
"js-yaml": "3.14.0",
"loader-utils": "^2.0.0",
"lodash.flatten": "^4.4.0",
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2",

View File

@ -1,4 +1,4 @@
import {LatusPlugin} from './latus-plugin';
import Latus from './latus';
const {
LATUS_ESLINT_DEFAULTS = require.resolve('@latus/build/build/.eslint.defaults.js'),
@ -31,6 +31,8 @@ export default (latus) => (neutrino) => {
.use(EnvironmentPlugin, [{
SIDE: 'server',
}]);
const node = r('@neutrinojs/node');
node()(neutrino);
const defaults = Object.keys(latus.originalConfig)
.map((plugin) => {
const config = {};
@ -53,38 +55,44 @@ export default (latus) => (neutrino) => {
latus.get(path),
]),
);
neutrino.config
.plugin('latus')
.use(LatusPlugin(
neutrino,
() => config,
(plugin, modulesObject) => {
plugin.writeModule('/%/latus/server/virtual', [
'global.$$latus = {',
` config: ${JSON.stringify(config)},`,
` modules: ${modulesObject},`,
'};',
'if (module.hot) {',
' Object.keys(global.$$latus.modules).forEach((key) => {',
' module.hot.accept(key, () => {',
" console.log('kesy!');",
' });',
' });',
'}',
].join('\n'));
},
));
neutrino.config
.entry('index')
.add('/%/latus/server/virtual');
const nodeExternals = r('webpack-node-externals');
neutrino.config.externals(nodeExternals());
.add('@latus/core/virtual');
const babelOptions = neutrino.config.module
.rules.store.get('compile')
.uses.store.get('babel')
.store.get('options');
r('@babel/register')({
plugins: babelOptions.plugins,
presets: babelOptions.presets,
});
const paths = Object.keys(config)
.map((path) => Latus.runtimePath(path))
.filter((path) => !!path);
const pathMap = paths.map((path) => `'${path}': require('${path}')`).join(', ');
const source = [
'global.$$latus = {',
` config: ${JSON.stringify(config)},`,
` modules: {${pathMap}},`,
'};',
'if (module.hot) {',
];
paths.forEach((key) => {
source.push(` module.hot.accept('${key}', () => {`);
source.push(` console.log('${key}!');`);
source.push(' });');
});
source.push('}');
neutrino.config.module
.rule('@latus/core/virtual')
.test(r.resolve('@latus/core/virtual'))
.use('virtual')
.loader(`${__dirname}/src/loader`)
.options({
source: source.join('\n'),
});
const mocha = r('@neutrinojs/mocha');
mocha()(neutrino);
const node = r('@neutrinojs/node');
node({
hot: false,
})(neutrino);
if (process.env.LATUS_LINTING) {
return;
}
@ -96,12 +104,21 @@ export default (latus) => (neutrino) => {
.entry('index')
.prepend('dotenv/config');
}
// Why no worky?
// if (process.argv.find((arg) => '--hot' === arg)) {
// const entries = neutrino.config.entryPoints.store.get('index').store;
// entries.delete(`${r.resolve('webpack/hot/poll')}?1000`);
// const entriesArray = Array.from(entries);
// entriesArray.unshift('webpack/hot/signal');
// // eslint-disable-next-line no-param-reassign
// neutrino.config.entryPoints.store.get('index').store = new Set(entriesArray);
// }
if (process.argv.find((arg) => '--start-server' === arg)) {
neutrino.config
.plugin('start-server')
.tap((args) => {
const options = args[0];
options.signal = true;
// options.signal = true;
const inspectArg = process.argv.find((arg) => -1 !== arg.indexOf('--inspect'));
if (inspectArg) {
options.nodeArgs.push(inspectArg);
@ -118,4 +135,8 @@ export default (latus) => (neutrino) => {
else {
neutrino.config.plugins.delete('start-server');
}
const nodeExternals = r('webpack-node-externals');
neutrino.config.externals(nodeExternals({
allowlist: /@latus\/core\/virtual/,
}));
};

View File

@ -0,0 +1,6 @@
const {getOptions} = require('loader-utils');
module.exports = function LatusLoader() {
const {source} = getOptions(this);
return source;
};

0
packages/core/virtual.js Normal file
View File