UNPKG

@tanstack/router-plugin

Version:

Modern and scalable routing for React applications

35 lines (34 loc) 1.67 kB
const require_runtime = require("../../../_virtual/_rolldown/runtime.cjs"); const require_utils = require("../../utils.cjs"); let _babel_types = require("@babel/types"); _babel_types = require_runtime.__toESM(_babel_types); //#region src/core/code-splitter/plugins/react-refresh-route-components.ts var REACT_REFRESH_ROUTE_COMPONENT_IDENTS = new Set([ "component", "pendingComponent", "errorComponent", "notFoundComponent" ]); function createReactRefreshRouteComponentsPlugin() { return { name: "react-refresh-route-components", onUnsplittableRoute(ctx) { if (!ctx.opts.addHmr) return; const hoistedDeclarations = []; ctx.routeOptions.properties.forEach((prop) => { if (!_babel_types.isObjectProperty(prop) || !_babel_types.isIdentifier(prop.key)) return; if (!REACT_REFRESH_ROUTE_COMPONENT_IDENTS.has(prop.key.name)) return; if (!_babel_types.isArrowFunctionExpression(prop.value) && !_babel_types.isFunctionExpression(prop.value)) return; const hoistedIdentifier = require_utils.getUniqueProgramIdentifier(ctx.programPath, `TSR${prop.key.name[0].toUpperCase()}${prop.key.name.slice(1)}`); hoistedDeclarations.push(_babel_types.variableDeclaration("const", [_babel_types.variableDeclarator(hoistedIdentifier, _babel_types.cloneNode(prop.value, true))])); prop.value = _babel_types.cloneNode(hoistedIdentifier); }); if (hoistedDeclarations.length === 0) return; ctx.insertionPath.insertBefore(hoistedDeclarations); return { modified: true }; } }; } //#endregion exports.createReactRefreshRouteComponentsPlugin = createReactRefreshRouteComponentsPlugin; //# sourceMappingURL=react-refresh-route-components.cjs.map