fix: HMR
This commit is contained in:
parent
382643400e
commit
ccf863d032
|
@ -31,6 +31,7 @@ import {D, Flecks} from '@flecks/core';
|
||||||
|
|
||||||
if (module.hot) {
|
if (module.hot) {
|
||||||
module.hot.accept('@flecks/server/runtime', () => {
|
module.hot.accept('@flecks/server/runtime', () => {
|
||||||
|
module.hot.invalidate();
|
||||||
if (cluster.isWorker) {
|
if (cluster.isWorker) {
|
||||||
cluster.worker.disconnect();
|
cluster.worker.disconnect();
|
||||||
const error = new Error('Restart requested!');
|
const error = new Error('Restart requested!');
|
||||||
|
|
|
@ -7,6 +7,23 @@ const {
|
||||||
} = process.env;
|
} = process.env;
|
||||||
|
|
||||||
export const hooks = {
|
export const hooks = {
|
||||||
|
'@flecks/core.hmr': (path, M, flecks) => {
|
||||||
|
if (
|
||||||
|
flecks.fleckImplementation(path, '@flecks/server.up')
|
||||||
|
|| M.hooks?.['@flecks/server.up']) {
|
||||||
|
if (
|
||||||
|
flecks.fleckImplementation(path, '@flecks/server.up')?.toString()
|
||||||
|
!== M.hooks?.['@flecks/server.up']?.toString()
|
||||||
|
) {
|
||||||
|
if (cluster.isWorker) {
|
||||||
|
cluster.worker.disconnect();
|
||||||
|
const error = new Error('@flecks/server.up implementation changed!');
|
||||||
|
error.stack = '';
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
'@flecks/server.up': (flecks) => {
|
'@flecks/server.up': (flecks) => {
|
||||||
if (!FLECKS_SERVER_TEST_SOCKET || 'test' !== NODE_ENV) {
|
if (!FLECKS_SERVER_TEST_SOCKET || 'test' !== NODE_ENV) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -68,9 +68,16 @@ module.exports = async (config, env, argv, flecks) => {
|
||||||
source.push('if (module.hot) {');
|
source.push('if (module.hot) {');
|
||||||
resolvedPaths.forEach((path) => {
|
resolvedPaths.forEach((path) => {
|
||||||
source.push(` module.hot.accept('${path}', async () => {`);
|
source.push(` module.hot.accept('${path}', async () => {`);
|
||||||
source.push(` const updatedFleck = require('${path}');`);
|
source.push(` const M = require('${path}')`);
|
||||||
source.push(` window.flecks.refresh('${path}', updatedFleck);`);
|
source.push(' try {');
|
||||||
source.push(` window.flecks.invoke('@flecks/core.hmr', '${path}', updatedFleck);`);
|
source.push(` global.flecks.invokeSequential('@flecks/core.hmr', '${path}', M);`);
|
||||||
|
source.push(` global.flecks.refresh('${path}', M);`);
|
||||||
|
source.push(' }');
|
||||||
|
source.push(' catch (error) {');
|
||||||
|
// eslint-disable-next-line no-template-curly-in-string
|
||||||
|
source.push(' console.error(`HMR failed for fleck: ${error.message}`);');
|
||||||
|
source.push(' module.hot.invalidate();');
|
||||||
|
source.push(' }');
|
||||||
source.push(' });');
|
source.push(' });');
|
||||||
});
|
});
|
||||||
source.push('}');
|
source.push('}');
|
||||||
|
|
|
@ -1,3 +1,18 @@
|
||||||
|
export const hooks = {
|
||||||
|
'@flecks/core.hmr': (path, M, flecks) => {
|
||||||
|
if (
|
||||||
|
flecks.fleckImplementation(path, '@flecks/web/client.up')
|
||||||
|
|| M.hooks?.['@flecks/web/client.up']) {
|
||||||
|
if (
|
||||||
|
flecks.fleckImplementation(path, '@flecks/web/client.up')?.toString()
|
||||||
|
!== M.hooks?.['@flecks/web/client.up']?.toString()
|
||||||
|
) {
|
||||||
|
throw new Error('@flecks/web/client.up implementation changed!');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
export const mixin = (Flecks) => class FlecksWithWebClient extends Flecks {
|
export const mixin = (Flecks) => class FlecksWithWebClient extends Flecks {
|
||||||
|
|
||||||
constructor(runtime) {
|
constructor(runtime) {
|
||||||
|
|
|
@ -68,3 +68,9 @@ const {version} = require('@flecks/web/package.json');
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
if (module.hot) {
|
||||||
|
module.hot.accept('@flecks/web/runtime', () => {
|
||||||
|
module.hot.invalidate();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user