From 44a183ca02179b02f9eb4d5fa93d34e34572a09e Mon Sep 17 00:00:00 2001 From: cha0s Date: Mon, 22 Mar 2021 08:05:36 -0500 Subject: [PATCH] refactor: toward server HMR --- packages/build/src/build/.babelrc.js | 25 ++++----- packages/core/.gitignore | 1 + packages/core/package.json | 6 ++- packages/core/src/build.js | 81 +++++++++++++++++----------- packages/core/src/loader.js | 6 +++ packages/core/virtual.js | 0 6 files changed, 76 insertions(+), 43 deletions(-) create mode 100644 packages/core/src/loader.js create mode 100644 packages/core/virtual.js diff --git a/packages/build/src/build/.babelrc.js b/packages/build/src/build/.babelrc.js index e3caf8a..eb854b9 100644 --- a/packages/build/src/build/.babelrc.js +++ b/packages/build/src/build/.babelrc.js @@ -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, }; }; diff --git a/packages/core/.gitignore b/packages/core/.gitignore index cfebc40..8936a27 100644 --- a/packages/core/.gitignore +++ b/packages/core/.gitignore @@ -3,3 +3,4 @@ !/.* !src/**/*.js !/test/**/*.js +!/virtual.js \ No newline at end of file diff --git a/packages/core/package.json b/packages/core/package.json index 4142cc6..de45d68 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -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", diff --git a/packages/core/src/build.js b/packages/core/src/build.js index ff9fa18..797c9f5 100644 --- a/packages/core/src/build.js +++ b/packages/core/src/build.js @@ -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/, + })); }; diff --git a/packages/core/src/loader.js b/packages/core/src/loader.js new file mode 100644 index 0000000..1e46147 --- /dev/null +++ b/packages/core/src/loader.js @@ -0,0 +1,6 @@ +const {getOptions} = require('loader-utils'); + +module.exports = function LatusLoader() { + const {source} = getOptions(this); + return source; +}; diff --git a/packages/core/virtual.js b/packages/core/virtual.js new file mode 100644 index 0000000..e69de29