From 6c3094240cd969a1fcdafd3420850bc36336c808 Mon Sep 17 00:00:00 2001 From: cha0s Date: Mon, 22 Mar 2021 14:20:11 -0500 Subject: [PATCH] feat: HMR! --- packages/core/src/build.js | 79 ++++++++++++++++++++++++++----------- packages/core/src/loader.js | 6 --- packages/core/src/start.js | 1 + packages/core/virtual.js | 6 +++ 4 files changed, 62 insertions(+), 30 deletions(-) delete mode 100644 packages/core/src/loader.js diff --git a/packages/core/src/build.js b/packages/core/src/build.js index 797c9f5..372b3b7 100644 --- a/packages/core/src/build.js +++ b/packages/core/src/build.js @@ -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/, - })); }; diff --git a/packages/core/src/loader.js b/packages/core/src/loader.js deleted file mode 100644 index 1e46147..0000000 --- a/packages/core/src/loader.js +++ /dev/null @@ -1,6 +0,0 @@ -const {getOptions} = require('loader-utils'); - -module.exports = function LatusLoader() { - const {source} = getOptions(this); - return source; -}; diff --git a/packages/core/src/start.js b/packages/core/src/start.js index 15e5f7e..d9eef9a 100644 --- a/packages/core/src/start.js +++ b/packages/core/src/start.js @@ -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')); diff --git a/packages/core/virtual.js b/packages/core/virtual.js index e69de29..1e46147 100644 --- a/packages/core/virtual.js +++ b/packages/core/virtual.js @@ -0,0 +1,6 @@ +const {getOptions} = require('loader-utils'); + +module.exports = function LatusLoader() { + const {source} = getOptions(this); + return source; +};