Skip to content

Commit

Permalink
feat(viteroll): support ssr patch module
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Nov 14, 2024
1 parent e574d32 commit 5131158
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions viteroll/examples/ssr/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default defineConfig({
plugins: [
viteroll({
reactRefresh: true,
// ssrPatchModule: true,
}),
{
name: "ssr-middleware",
Expand Down
29 changes: 21 additions & 8 deletions viteroll/viteroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const require = createRequire(import.meta.url);

interface ViterollOptions {
reactRefresh?: boolean;
ssrPatchModule?: boolean;
}

const logger = createLogger("info", {
Expand Down Expand Up @@ -151,7 +152,9 @@ window.__rolldown_hot = hot;
export class RolldownEnvironment extends DevEnvironment {
instance!: rolldown.RolldownBuild;
result!: rolldown.RolldownOutput;
outDir!: string;
outDir: string;
inputOptions!: rolldown.InputOptions;
outputOptions!: rolldown.OutputOptions;
buildTimestamp = Date.now();

static createFactory(
Expand Down Expand Up @@ -206,7 +209,7 @@ export class RolldownEnvironment extends DevEnvironment {
}

console.time(`[rolldown:${this.name}:build]`);
const inputOptions: rolldown.InputOptions = {
this.inputOptions = {
// TODO: no dev ssr for now
dev: this.name === "client",
// NOTE:
Expand Down Expand Up @@ -238,12 +241,16 @@ export class RolldownEnvironment extends DevEnvironment {
...(plugins as any),
],
};
this.instance = await rolldown.rolldown(inputOptions);
this.instance = await rolldown.rolldown(this.inputOptions);

// `generate` should work but we use `write` so it's easier to see output and debug
const outputOptions: rolldown.OutputOptions = {
this.outputOptions = {
dir: this.outDir,
format: this.name === "client" ? "app" : "esm",
format:
this.name === "client" ||
(this.name === "ssr" && this.viterollOptions.ssrPatchModule)
? "app"
: "esm",
// TODO: hmr_rebuild returns source map file when `sourcemap: true`
sourcemap: "inline",
// TODO: https://github.com/rolldown/rolldown/issues/2041
Expand All @@ -253,7 +260,7 @@ export class RolldownEnvironment extends DevEnvironment {
? `import __nodeModule from "node:module"; const require = __nodeModule.createRequire(import.meta.url);`
: undefined,
};
this.result = await this.instance.write(outputOptions);
this.result = await this.instance.write(this.outputOptions);

this.buildTimestamp = Date.now();
console.timeEnd(`[rolldown:${this.name}:build]`);
Expand All @@ -268,18 +275,24 @@ export class RolldownEnvironment extends DevEnvironment {
return;
}
if (this.name === "ssr") {
await this.build();
if (this.outputOptions.format === "app") {
// TODO
} else {
await this.build();
}
} else {
logger.info(`hmr '${ctx.file}'`, { timestamp: true });
console.time(`[rolldown:${this.name}:hmr]`);
const result = await this.instance.experimental_hmr_rebuild([ctx.file]);
console.timeEnd(`[rolldown:${this.name}:hmr]`);
ctx.server.ws.send("rolldown:hmr", result);
}
return true;
}

async import(input: string): Promise<unknown> {
if (this.outputOptions.format === "app") {
// TODO: eval or vm
}
const output = this.result.output.find((o) => o.name === input);
assert(output, `invalid import input '${input}'`);
const filepath = path.join(this.outDir, output.fileName);
Expand Down

0 comments on commit 5131158

Please sign in to comment.