Skip to content

Commit

Permalink
fix: vite 6 breaking change for ssr module hmr, add migration plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianCataldo committed Dec 22, 2024
1 parent eb128d7 commit b0026e4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/engine/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { GracileConfig } from './user-config.js';
import { buildRoutes } from './vite/build-routes.js';
import { htmlRoutesLoader } from './vite/html-routes.js';
import { virtualRoutes, virtualRoutesClient } from './vite/virtual-routes.js';
import { hmrSsrReload } from './vite/hmr.js';

let isClientBuilt = false;

Expand Down Expand Up @@ -103,6 +104,8 @@ export const gracile = (config?: GracileConfig): any[] => {
// },
// },

hmrSsrReload(),

{
name: 'vite-plugin-gracile-serve-middleware',

Expand Down
40 changes: 40 additions & 0 deletions packages/engine/src/vite/hmr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { Plugin, EnvironmentModuleNode } from 'vite';

// NOTE: From https://vite.dev/guide/migration#advanced (Vite 5>6 migration).
export function hmrSsrReload(): Plugin {
return {
name: 'vite-plugin-gracile-hmr-ssr-reload',
enforce: 'post',

hotUpdate: {
order: 'post',

handler({ modules, server, timestamp }) {
if (this.environment.name !== 'ssr') return;

let hasSsrOnlyModules = false;

const invalidatedModules = new Set<EnvironmentModuleNode>();
for (const module_ of modules) {
if (module_.id == null) continue;
const clientModule =
server.environments.client.moduleGraph.getModuleById(module_.id);
if (clientModule != null) continue;

this.environment.moduleGraph.invalidateModule(
module_,
invalidatedModules,
timestamp,
true,
);
hasSsrOnlyModules = true;
}

if (hasSsrOnlyModules) {
server.ws.send({ type: 'full-reload' });
return [];
}
},
},
};
}
4 changes: 2 additions & 2 deletions packages/engine/src/vite/virtual-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export { routes, routeImports, routeAssets };

return null;
},
} satisfies Plugin,
},
];
}

Expand Down Expand Up @@ -154,6 +154,6 @@ export { routeImports };

return null;
},
} satisfies Plugin,
},
];
}

0 comments on commit b0026e4

Please sign in to comment.