UNPKG

@tanstack/router-plugin

Version:

Modern and scalable routing for React applications

46 lines (42 loc) 2 kB
import { getUniqueProgramIdentifier } from "../../utils.js"; import * as t from "@babel/types"; import * as template from "@babel/template"; //#region src/core/code-splitter/plugins/react-refresh-ignored-route-exports.ts var buildReactRefreshIgnoredRouteExportsStatement = template.statement(` if (import.meta.hot && typeof window !== 'undefined') { const tsrReactRefresh = window.__TSR_REACT_REFRESH__ ??= (() => { const ignoredExportsById = new Map() const previousGetIgnoredExports = window.__getReactRefreshIgnoredExports window.__getReactRefreshIgnoredExports = (ctx) => { const ignoredExports = previousGetIgnoredExports?.(ctx) ?? [] const moduleIgnored = ignoredExportsById.get(ctx.id) ?? [] return [...ignoredExports, ...moduleIgnored] } return { ignoredExportsById, } })() tsrReactRefresh.ignoredExportsById.set(%%moduleId%%, ['Route']) } `, { syntacticPlaceholders: true }); /** * A trivial component-shaped export that gives `@vitejs/plugin-react` a valid * Fast Refresh boundary. Without at least one non-ignored component export, * the module would be invalidated (full page reload) on every update even * though our custom route HMR handler already manages the update. */ var buildRefreshAnchorStatement = template.statement(`export function %%anchorName%%() { return null }`, { syntacticPlaceholders: true }); function createReactRefreshIgnoredRouteExportsPlugin() { return { name: "react-refresh-ignored-route-exports", onAddHmr(ctx) { const anchorName = getUniqueProgramIdentifier(ctx.programPath, "TSRFastRefreshAnchor"); ctx.programPath.pushContainer("body", buildReactRefreshIgnoredRouteExportsStatement({ moduleId: t.stringLiteral(ctx.opts.id) })); ctx.programPath.pushContainer("body", buildRefreshAnchorStatement({ anchorName })); return { modified: true }; } }; } //#endregion export { createReactRefreshIgnoredRouteExportsPlugin }; //# sourceMappingURL=react-refresh-ignored-route-exports.js.map