Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support Vite 6 HMR #10255

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@
- sambostock
- sandstone991
- sandulat
- sapphi-red
- sarahse
- sathvik-k
- sbernheim4
Expand Down
49 changes: 49 additions & 0 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1815,9 +1815,58 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
return modules;
},
},
{
name: "remix-server-change-trigger-client-hmr",
// hotUpdate only exists in Vite 6+, remove any after upgrading Vite to v6
hotUpdate(this: any, { server, modules }: any) {
if (this.environment.name !== "ssr" && modules.length <= 0) return;

let clientModules = uniqueNodes(
modules.flatMap((mod: ViteEnvironmentModuleNode) =>
getParentClientNodes(server.environments.client.moduleGraph, mod)
)
);

for (let clientModule of clientModules) {
server.environments.client.reloadModule(clientModule);
}
},
},
];
};

// replace with Vite.EnvironmentModuleGraph after upgrading Vite to v6
type ViteEnvironmentModuleGraph = Vite.ModuleGraph
// replace with Vite.EnvironmentModuleNode after upgrading Vite to v6
type ViteEnvironmentModuleNode = Vite.ModuleNode

function getParentClientNodes(
clientModuleGraph: ViteEnvironmentModuleGraph,
module: ViteEnvironmentModuleNode
): ViteEnvironmentModuleNode[] {
if (!module.id) return [];

let clientModule = clientModuleGraph.getModuleById(module.id);
if (clientModule) return [clientModule];

return [...module.importers].flatMap((importer) =>
getParentClientNodes(clientModuleGraph, importer)
);
}

function uniqueNodes(
nodes: ViteEnvironmentModuleNode[]
): ViteEnvironmentModuleNode[] {
let nodeUrls = new Set<string>();
let uniqued: ViteEnvironmentModuleNode[] = [];
for (let node of nodes) {
if (nodeUrls.has(node.url)) continue;
nodeUrls.add(node.url);
uniqued.push(node);
}
return uniqued;
}

function isInRemixMonorepo() {
let devPath = path.dirname(require.resolve("@remix-run/dev/package.json"));
let devParentDir = path.basename(path.resolve(devPath, ".."));
Expand Down