Skip to content

Commit

Permalink
wip: ssr module runner too
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Nov 29, 2024
1 parent d1c174c commit 7335a7c
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions viteroll/viteroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,23 @@ export class RolldownEnvironment extends DevEnvironment {
console.timeEnd(`[rolldown:${this.name}:build]`);
}

async buildHmr(file: string) {
logger.info(`hmr '${file}'`, { timestamp: true });
this.changedFiles = [file];
this.changedModules = {};
await this.build();
const newCode = this.changedModules[file];
const stableId = path.relative(this.config.root, file);
const output = `\
self.rolldown_runtime.patch([${JSON.stringify(stableId)}], function(){
rolldown_runtime.define(${JSON.stringify(stableId)},function(require, module, exports){
${newCode}
})
})
`;
return [path.join(this.outDir, "hmr-update.js"), output];
}

async handleUpdate(ctx: HmrContext) {
if (!this.result) {
return;
Expand All @@ -284,27 +301,14 @@ export class RolldownEnvironment extends DevEnvironment {
}
if (this.name === "ssr") {
if (this.outputOptions.format === "app") {
console.time(`[rolldown:${this.name}:hmr]`);
const result = await this.instance.experimental_hmr_rebuild([ctx.file]);
const result = await this.buildHmr(ctx.file);
this.getRunner().evaluate(result[1].toString(), result[0]);
console.timeEnd(`[rolldown:${this.name}:hmr]`);
} else {
await this.build();
}
} else {
logger.info(`hmr '${ctx.file}'`, { timestamp: true });
this.changedFiles = [ctx.file];
this.changedModules = {};
await this.build();
const stableId = path.relative(this.config.root, ctx.file);
const output = `\
self.rolldown_runtime.patch([${JSON.stringify(stableId)}], function(){
rolldown_runtime.define(${JSON.stringify(stableId)},function(require, module, exports){
${this.changedModules[ctx.file]}
})
})
`;
ctx.server.ws.send("rolldown:hmr", [null, output]);
const result = await this.buildHmr(ctx.file);
ctx.server.ws.send("rolldown:hmr", result);
}
}

Expand Down Expand Up @@ -456,7 +460,7 @@ function viterollEntryPlugin(
boundaries.push(moduleId);
invalidModuleIds.push(moduleId);
if (module.parents.filter(Boolean).length === 0) {
window.location.reload();
globalThis.window?.location.reload();
break;
}
for (var i = 0; i < module.parents.length; i++) {`,
Expand Down

0 comments on commit 7335a7c

Please sign in to comment.