feat: script HMR

This commit is contained in:
cha0s 2024-10-21 03:47:55 -05:00
parent a2a33536e7
commit 782fdfc28c
2 changed files with 24 additions and 8 deletions

View File

@ -39,7 +39,13 @@ export default class Script {
if (!fn) {
return undefined;
}
return new this(fn, locals);
const script = new this(fn, locals);
if (import.meta.hot) {
import.meta.hot.accept('./scripts.js', ({default: scripts}) => {
script.fn = scripts[`../..${pathOrFunction}`];
});
}
return script;
}
static register(path, fn) {
@ -101,11 +107,16 @@ export default class Script {
}
const imports = import.meta.glob(
'../../resources/**/*.js',
{eager: true, import: 'default'},
);
for (const path in imports) {
Script.register(path.slice('../..'.length), imports[path]);
function register({default: scripts}) {
for (const path in scripts) {
Script.register(path.slice('../..'.length), scripts[path]);
}
}
register(await import('./scripts.js'));
if (import.meta.hot) {
import.meta.hot.accept('./scripts.js', (M) => {
register(M);
});
}

5
app/util/scripts.js Normal file
View File

@ -0,0 +1,5 @@
export default import.meta.glob(
'../../resources/**/*.js',
{eager: true, import: 'default'},
);