@tanstack/router-plugin
Version:
Modern and scalable routing for React applications
39 lines (38 loc) • 1.8 kB
JavaScript
import { getUniqueProgramIdentifier } from "../../utils.js";
import * as t from "@babel/types";
import * as template from "@babel/template";
//#region src/core/code-splitter/plugins/react-stable-hmr-split-route-components.ts
function capitalizeIdentifier(str) {
return str[0].toUpperCase() + str.slice(1);
}
function createHotDataKey(exportName) {
return `tsr-split-component:${exportName}`;
}
var buildStableSplitComponentStatements = template.statements(`
const %%stableComponentIdent%% = import.meta.hot?.data?.[%%hotDataKey%%] ?? %%lazyRouteComponentIdent%%(%%localImporterIdent%%, %%exporterIdent%%)
if (import.meta.hot) {
import.meta.hot.data ??= {}
import.meta.hot.data[%%hotDataKey%%] = %%stableComponentIdent%%
}
`, { syntacticPlaceholders: true });
function createReactStableHmrSplitRouteComponentsPlugin() {
return {
name: "react-stable-hmr-split-route-components",
onSplitRouteProperty(ctx) {
if (ctx.splitNodeMeta.splitStrategy !== "lazyRouteComponent") return;
const stableComponentIdent = getUniqueProgramIdentifier(ctx.programPath, `TSRSplit${capitalizeIdentifier(ctx.splitNodeMeta.exporterIdent)}`);
const hotDataKey = createHotDataKey(ctx.splitNodeMeta.exporterIdent);
ctx.insertionPath.insertBefore(buildStableSplitComponentStatements({
stableComponentIdent,
hotDataKey: t.stringLiteral(hotDataKey),
lazyRouteComponentIdent: t.identifier(ctx.lazyRouteComponentIdent),
localImporterIdent: t.identifier(ctx.splitNodeMeta.localImporterIdent),
exporterIdent: t.stringLiteral(ctx.splitNodeMeta.exporterIdent)
}));
return t.identifier(stableComponentIdent.name);
}
};
}
//#endregion
export { createReactStableHmrSplitRouteComponentsPlugin };
//# sourceMappingURL=react-stable-hmr-split-route-components.js.map