Skip to content

Commit 59736e5

Browse files
authored
Merge pull request #11956 from remix-run/pedro/comment-boundaries-for-react-refresh
comment boundaries for react refresh transforms
2 parents 26cc9d9 + 14e3626 commit 59736e5

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

packages/react-router-dev/vite/plugin.ts

+25-7
Original file line numberDiff line numberDiff line change
@@ -1646,14 +1646,32 @@ function addRefreshWrapper(
16461646
]
16471647
: [];
16481648
return (
1649-
REACT_REFRESH_HEADER.replaceAll("__SOURCE__", JSON.stringify(id)) +
1650-
code +
1651-
REACT_REFRESH_FOOTER.replaceAll("__SOURCE__", JSON.stringify(id))
1652-
.replaceAll("__ACCEPT_EXPORTS__", JSON.stringify(acceptExports))
1653-
.replaceAll("__ROUTE_ID__", JSON.stringify(route?.id))
1649+
"\n\n" +
1650+
withCommentBoundaries(
1651+
"REACT REFRESH HEADER",
1652+
REACT_REFRESH_HEADER.replaceAll("__SOURCE__", JSON.stringify(id))
1653+
) +
1654+
"\n\n" +
1655+
withCommentBoundaries("REACT REFRESH BODY", code) +
1656+
"\n\n" +
1657+
withCommentBoundaries(
1658+
"REACT REFRESH FOOTER",
1659+
REACT_REFRESH_FOOTER.replaceAll("__SOURCE__", JSON.stringify(id))
1660+
.replaceAll("__ACCEPT_EXPORTS__", JSON.stringify(acceptExports))
1661+
.replaceAll("__ROUTE_ID__", JSON.stringify(route?.id))
1662+
) +
1663+
"\n"
16541664
);
16551665
}
16561666

1667+
function withCommentBoundaries(label: string, text: string) {
1668+
let begin = `// [BEGIN] ${label} `;
1669+
begin += "-".repeat(80 - begin.length);
1670+
let end = `// [END] ${label} `;
1671+
end += "-".repeat(80 - end.length);
1672+
return `${begin}\n${text}\n${end}`;
1673+
}
1674+
16571675
const REACT_REFRESH_HEADER = `
16581676
import RefreshRuntime from "${hmrRuntimeId}";
16591677
@@ -1674,7 +1692,7 @@ if (import.meta.hot && !inWebWorker) {
16741692
RefreshRuntime.register(type, __SOURCE__ + " " + id)
16751693
};
16761694
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
1677-
}`.replace(/\n+/g, "");
1695+
}`.trim();
16781696

16791697
const REACT_REFRESH_FOOTER = `
16801698
if (import.meta.hot && !inWebWorker) {
@@ -1689,7 +1707,7 @@ if (import.meta.hot && !inWebWorker) {
16891707
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
16901708
});
16911709
});
1692-
}`;
1710+
}`.trim();
16931711

16941712
function getRoute(
16951713
pluginConfig: ResolvedReactRouterConfig,

0 commit comments

Comments
 (0)