feat: HMR!

This commit is contained in:
cha0s 2021-03-22 14:20:11 -05:00
parent c09c5152d2
commit 6c3094240c
4 changed files with 62 additions and 30 deletions

View File

@ -1,6 +1,15 @@
import {
basename,
dirname,
extname,
join,
resolve,
} from 'path';
import Latus from './latus'; import Latus from './latus';
const { const {
LATUS_BABEL_CONFIG = require.resolve('@latus/build/build/.babelrc.js'),
LATUS_ESLINT_DEFAULTS = require.resolve('@latus/build/build/.eslint.defaults.js'), LATUS_ESLINT_DEFAULTS = require.resolve('@latus/build/build/.eslint.defaults.js'),
} = process.env; } = process.env;
@ -32,7 +41,11 @@ export default (latus) => (neutrino) => {
SIDE: 'server', SIDE: 'server',
}]); }]);
const node = r('@neutrinojs/node'); const node = r('@neutrinojs/node');
node()(neutrino); node({
babel: {
configFile: LATUS_BABEL_CONFIG,
},
})(neutrino);
const defaults = Object.keys(latus.originalConfig) const defaults = Object.keys(latus.originalConfig)
.map((plugin) => { .map((plugin) => {
const config = {}; const config = {};
@ -55,9 +68,6 @@ export default (latus) => (neutrino) => {
latus.get(path), latus.get(path),
]), ]),
); );
neutrino.config
.entry('index')
.add('@latus/core/virtual');
const babelOptions = neutrino.config.module const babelOptions = neutrino.config.module
.rules.store.get('compile') .rules.store.get('compile')
.uses.store.get('babel') .uses.store.get('babel')
@ -78,16 +88,21 @@ export default (latus) => (neutrino) => {
'if (module.hot) {', 'if (module.hot) {',
]; ];
paths.forEach((key) => { paths.forEach((key) => {
source.push(` module.hot.accept('${key}', () => {`); source.push(` module.hot.accept('${key}', () => {`);
source.push(` console.log('${key}!');`); source.push(` global.latus.invoke('@latus/core/hmr', '${key}');`);
source.push(' });'); source.push(' });');
}); });
source.push('}'); source.push('}');
// Everything's a simulation...
const virtual = r.resolve(`${__dirname}/virtual`);
neutrino.config
.entry('index')
.add(virtual);
neutrino.config.module neutrino.config.module
.rule('@latus/core/virtual') .rule(virtual)
.test(r.resolve('@latus/core/virtual')) .test(virtual)
.use('virtual') .use('virtual')
.loader(`${__dirname}/src/loader`) .loader(virtual)
.options({ .options({
source: source.join('\n'), source: source.join('\n'),
}); });
@ -99,26 +114,46 @@ export default (latus) => (neutrino) => {
neutrino.config neutrino.config
.entry('index') .entry('index')
.add('@latus/core/start'); .add('@latus/core/start');
const nodeExternals = r('webpack-node-externals');
const allowlist = [
/^@latus\/core\/virtual$/,
];
if ('production' !== neutrino.config.get('mode')) { if ('production' !== neutrino.config.get('mode')) {
neutrino.config neutrino.config
.entry('index') .entry('index')
.prepend('dotenv/config'); .prepend('dotenv/config');
allowlist.push(/^webpack/);
paths.forEach((path) => {
const resolved = resolve(r.resolve(path));
// Cheating for now...
const parts = resolved.split('/');
parts.splice(parts.indexOf('packages') + 2, 0, 'src');
let source = parts.join('/');
source = join(dirname(source), basename(source, extname(source)));
allowlist.push(new RegExp(`^${path}$`));
neutrino.config.resolve.alias
.set(`${path}$`, source);
neutrino.config.module
.rule('compile').include.add(dirname(source));
});
}
neutrino.config.externals(nodeExternals({
allowlist,
}));
const entries = neutrino.config.entryPoints.store.get('index').store;
if (entries.has(`${r.resolve('webpack/hot/poll')}?1000`)) {
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);
} }
// 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)) { if (process.argv.find((arg) => '--start-server' === arg)) {
neutrino.config neutrino.config
.plugin('start-server') .plugin('start-server')
.tap((args) => { .tap((args) => {
const options = args[0]; const options = args[0];
// options.signal = true; options.signal = true;
const inspectArg = process.argv.find((arg) => -1 !== arg.indexOf('--inspect')); const inspectArg = process.argv.find((arg) => -1 !== arg.indexOf('--inspect'));
if (inspectArg) { if (inspectArg) {
options.nodeArgs.push(inspectArg); options.nodeArgs.push(inspectArg);
@ -135,8 +170,4 @@ export default (latus) => (neutrino) => {
else { else {
neutrino.config.plugins.delete('start-server'); neutrino.config.plugins.delete('start-server');
} }
const nodeExternals = r('webpack-node-externals');
neutrino.config.externals(nodeExternals({
allowlist: /@latus\/core\/virtual/,
}));
}; };

View File

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

View File

@ -5,6 +5,7 @@ console.log('Latus starting...');
(async () => { (async () => {
const latus = new Latus(global.$$latus); const latus = new Latus(global.$$latus);
global.latus = latus;
try { try {
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'));

View File

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