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';
const {
LATUS_BABEL_CONFIG = require.resolve('@latus/build/build/.babelrc.js'),
LATUS_ESLINT_DEFAULTS = require.resolve('@latus/build/build/.eslint.defaults.js'),
} = process.env;
@ -32,7 +41,11 @@ export default (latus) => (neutrino) => {
SIDE: 'server',
}]);
const node = r('@neutrinojs/node');
node()(neutrino);
node({
babel: {
configFile: LATUS_BABEL_CONFIG,
},
})(neutrino);
const defaults = Object.keys(latus.originalConfig)
.map((plugin) => {
const config = {};
@ -55,9 +68,6 @@ export default (latus) => (neutrino) => {
latus.get(path),
]),
);
neutrino.config
.entry('index')
.add('@latus/core/virtual');
const babelOptions = neutrino.config.module
.rules.store.get('compile')
.uses.store.get('babel')
@ -78,16 +88,21 @@ export default (latus) => (neutrino) => {
'if (module.hot) {',
];
paths.forEach((key) => {
source.push(` module.hot.accept('${key}', () => {`);
source.push(` console.log('${key}!');`);
source.push(' });');
source.push(` module.hot.accept('${key}', () => {`);
source.push(` global.latus.invoke('@latus/core/hmr', '${key}');`);
source.push(' });');
});
source.push('}');
// Everything's a simulation...
const virtual = r.resolve(`${__dirname}/virtual`);
neutrino.config
.entry('index')
.add(virtual);
neutrino.config.module
.rule('@latus/core/virtual')
.test(r.resolve('@latus/core/virtual'))
.rule(virtual)
.test(virtual)
.use('virtual')
.loader(`${__dirname}/src/loader`)
.loader(virtual)
.options({
source: source.join('\n'),
});
@ -99,26 +114,46 @@ export default (latus) => (neutrino) => {
neutrino.config
.entry('index')
.add('@latus/core/start');
const nodeExternals = r('webpack-node-externals');
const allowlist = [
/^@latus\/core\/virtual$/,
];
if ('production' !== neutrino.config.get('mode')) {
neutrino.config
.entry('index')
.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)) {
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);
@ -135,8 +170,4 @@ 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

@ -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 () => {
const latus = new Latus(global.$$latus);
global.latus = latus;
try {
await Promise.all(latus.invokeFlat('@latus/core/starting'));
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;
};