Skip to content

Commit

Permalink
fix hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
KermanX committed Jan 26, 2024
1 parent b7d3996 commit 72b3560
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions packages/hmr/src/wrapMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@ import {
} from "./constants";
import { ParseResult } from "./parser";

export function wrapMain({ imports, mainSrc }: ParseResult) {
export function wrapMain({ imports, mainSrc, appInstance }: ParseResult) {
const appId = appInstance ?? appInstDeafultId;
mainSrc.prepend(`${imports}
export const ${mainFuncId} = (${localsObjId}) => `);

mainSrc.append(`
let ${appInstDeafultId}, ${localsObjId};
let ${appId}, ${localsObjId};
export function ${initFuncId}(app, locals) {
${appInstDeafultId} = app;
${localsObjId} = locals;
export function ${initFuncId}(__app_param__, __locals_param__) {
${appId} = __app_param__;
${localsObjId} = __locals_param__;
}
import.meta.hot?.accept(async (newMainModule) => {
newMainModule.${initFuncId}(${appInstDeafultId}, ${localsObjId});
const newMain = newMainModule.${mainFuncId}(${localsObjId});
if(${appInstDeafultId}.state !== "idle") {
await ${appInstDeafultId}.promises.mainExecuted;
import.meta.hot?.accept(async (__new_main_mod__) => {
__new_main_mod__.${initFuncId}(${appId}, ${localsObjId});
const newMain = __new_main_mod__.${mainFuncId}(${localsObjId});
if(${appId}.state !== "idle") {
await ${appId}.promises.mainExecuted;
}
${appInstDeafultId}.main = newMain;
${appInstDeafultId}.update();
${appId}.main = newMain;
${appId}.update();
});
`);
}

0 comments on commit 72b3560

Please sign in to comment.