feat: latus HMR!
This commit is contained in:
parent
a448ac22af
commit
bb7d78fdcf
|
@ -275,6 +275,45 @@ export default class Latus {
|
|||
});
|
||||
}
|
||||
|
||||
refresh(plugin, M) {
|
||||
const keys = Object.keys(this.hooks);
|
||||
for (let j = 0; j < keys.length; j++) {
|
||||
const key = keys[j];
|
||||
if (this.hooks[key]) {
|
||||
const index = this.hooks[key].findIndex(({plugin: hookPlugin}) => hookPlugin === plugin);
|
||||
if (-1 !== index) {
|
||||
this.hooks[key].splice(index, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (M.default) {
|
||||
const {default: {hooks}} = M;
|
||||
if (hooks) {
|
||||
const keys = Object.keys(hooks);
|
||||
debug("hooks for '%s': %O", plugin, keys);
|
||||
for (let j = 0; j < keys.length; j++) {
|
||||
const key = keys[j];
|
||||
if (!this.hooks[key]) {
|
||||
this.hooks[key] = [];
|
||||
}
|
||||
this.hooks[key].push({
|
||||
plugin,
|
||||
fn: hooks[key],
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
debug(`'${plugin}' has no default export`);
|
||||
}
|
||||
const defaultConfig = this.invoke('@latus/core/config');
|
||||
this.config[plugin] = {
|
||||
...defaultConfig[plugin],
|
||||
...this.config[plugin],
|
||||
};
|
||||
this.invokeFlat('@latus/core/config/alter', this.config);
|
||||
}
|
||||
|
||||
static runtimePath(path) {
|
||||
try {
|
||||
R.resolve(path);
|
||||
|
|
|
@ -41,6 +41,7 @@ module.exports = async (latus) => {
|
|||
const contexts = new Set();
|
||||
paths.forEach((path) => {
|
||||
source.push(` module.hot.accept('${path}', () => {`);
|
||||
source.push(` window.latus.refresh('${path}', require('${path}'));`);
|
||||
source.push(` window.latus.invoke('@latus/core/hmr', '${path}');`);
|
||||
source.push(' });');
|
||||
const context = join(dirname(R.resolve(path)), 'assets');
|
||||
|
|
|
@ -1,6 +1,21 @@
|
|||
import {useContext} from 'react';
|
||||
import {useContext, useEffect, useState} from 'react';
|
||||
|
||||
// eslint-disable-next-line import/no-extraneous-dependencies
|
||||
import {LatusContext} from '@latus/react/client';
|
||||
|
||||
export default () => useContext(LatusContext);
|
||||
export default () => {
|
||||
const [, setId] = useState(0);
|
||||
const latus = useContext(LatusContext);
|
||||
useEffect(() => {
|
||||
if (!latus.hooks['@latus/core/hmr']) {
|
||||
latus.hooks['@latus/core/hmr'] = [];
|
||||
}
|
||||
latus.hooks['@latus/core/hmr'].push({
|
||||
plugin: '@latus/react/hmr',
|
||||
fn: () => {
|
||||
setId(Math.random());
|
||||
},
|
||||
});
|
||||
}, [latus]);
|
||||
return latus;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user