Skip to content
Merged
Changes from all 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
32 changes: 25 additions & 7 deletions packages/react-router-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1646,14 +1646,32 @@ function addRefreshWrapper(
]
: [];
return (
REACT_REFRESH_HEADER.replaceAll("__SOURCE__", JSON.stringify(id)) +
code +
REACT_REFRESH_FOOTER.replaceAll("__SOURCE__", JSON.stringify(id))
.replaceAll("__ACCEPT_EXPORTS__", JSON.stringify(acceptExports))
.replaceAll("__ROUTE_ID__", JSON.stringify(route?.id))
"\n\n" +
withCommentBoundaries(
"REACT REFRESH HEADER",
REACT_REFRESH_HEADER.replaceAll("__SOURCE__", JSON.stringify(id))
) +
"\n\n" +
withCommentBoundaries("REACT REFRESH BODY", code) +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this one be left off? When reading the compiled source code, typically I'm looking for this bit — it's only the other blocks I'm trying to ignore, which is where the comments are nice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly either way. I thought it was nice to give it a name so there's a ubiquitous language for it when debugging or asking users about it, but if you feel its mostly noise I'm ok with removing it.

"\n\n" +
withCommentBoundaries(
"REACT REFRESH FOOTER",
REACT_REFRESH_FOOTER.replaceAll("__SOURCE__", JSON.stringify(id))
.replaceAll("__ACCEPT_EXPORTS__", JSON.stringify(acceptExports))
.replaceAll("__ROUTE_ID__", JSON.stringify(route?.id))
) +
"\n"
);
}

function withCommentBoundaries(label: string, text: string) {
let begin = `// [BEGIN] ${label} `;
begin += "-".repeat(80 - begin.length);
let end = `// [END] ${label} `;
end += "-".repeat(80 - end.length);
return `${begin}\n${text}\n${end}`;
}

const REACT_REFRESH_HEADER = `
import RefreshRuntime from "${hmrRuntimeId}";

Expand All @@ -1674,7 +1692,7 @@ if (import.meta.hot && !inWebWorker) {
RefreshRuntime.register(type, __SOURCE__ + " " + id)
};
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
}`.replace(/\n+/g, "");
}`.trim();

const REACT_REFRESH_FOOTER = `
if (import.meta.hot && !inWebWorker) {
Expand All @@ -1689,7 +1707,7 @@ if (import.meta.hot && !inWebWorker) {
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
});
});
}`;
}`.trim();

function getRoute(
pluginConfig: ResolvedReactRouterConfig,
Expand Down