refactor: path only script + weak ref hmr
This commit is contained in:
parent
bd5c069090
commit
c499afb246
|
@ -22,27 +22,19 @@ export default class Script {
|
||||||
return this.fn(this.locals).next().value;
|
return this.fn(this.locals).next().value;
|
||||||
}
|
}
|
||||||
|
|
||||||
static load(pathOrFunction, locals) {
|
static load(path, locals) {
|
||||||
let fn;
|
const fn = this.registered[path];
|
||||||
if (this.registered[pathOrFunction]) {
|
|
||||||
fn = this.registered[pathOrFunction];
|
|
||||||
}
|
|
||||||
else if (pathOrFunction) {
|
|
||||||
try {
|
|
||||||
fn = eval(pathOrFunction);
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
console.error("Couldn't eval script", pathOrFunction);
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!fn) {
|
if (!fn) {
|
||||||
return undefined;
|
throw new Error(`no such script: ${path}`);
|
||||||
}
|
}
|
||||||
const script = new this(fn, locals);
|
const script = new this(fn, locals);
|
||||||
if (import.meta.hot) {
|
if (import.meta.hot) {
|
||||||
|
const hotRef = new WeakRef(script);
|
||||||
import.meta.hot.accept('./scripts.js', ({default: scripts}) => {
|
import.meta.hot.accept('./scripts.js', ({default: scripts}) => {
|
||||||
script.fn = scripts[`../..${pathOrFunction}`];
|
const ref = hotRef.deref();
|
||||||
|
if (ref) {
|
||||||
|
ref.fn = scripts[`../..${path}`];
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return script;
|
return script;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user