UNPKG

@tanstack/router-plugin

Version:

Modern and scalable routing for React applications

1 lines 3.28 kB
{"version":3,"file":"react-stable-hmr-split-route-components.cjs","names":[],"sources":["../../../../../src/core/code-splitter/plugins/react-stable-hmr-split-route-components.ts"],"sourcesContent":["import * as template from '@babel/template'\nimport * as t from '@babel/types'\nimport { getUniqueProgramIdentifier } from '../../utils'\nimport type { HmrStyle } from '../../config'\nimport type { ReferenceRouteCompilerPlugin } from '../plugins'\n\nfunction capitalizeIdentifier(str: string) {\n return str[0]!.toUpperCase() + str.slice(1)\n}\n\nfunction createHotDataKey(exportName: string) {\n return `tsr-split-component:${exportName}`\n}\n\nconst buildStableSplitComponentStatements = template.statements(\n `\n const %%stableComponentIdent%% = (() => {\n const hot = %%hotExpression%%\n const hotData = hot ? (hot.data ??= {}) : undefined\n return hotData?.[%%hotDataKey%%] ?? %%lazyRouteComponentIdent%%(%%localImporterIdent%%, %%exporterIdent%%)\n })()\n if (%%hotExpression%%) {\n ((%%hotExpression%%).data ??= {})[%%hotDataKey%%] = %%stableComponentIdent%%\n }\n `,\n {\n syntacticPlaceholders: true,\n },\n)\n\nfunction hotExpressionAstFor(hmrStyle: HmrStyle): t.Expression {\n return template.expression.ast(\n hmrStyle === 'webpack' ? 'import.meta.webpackHot' : 'import.meta.hot',\n )\n}\n\nexport function createReactStableHmrSplitRouteComponentsPlugin(opts: {\n hmrStyle: HmrStyle\n}): ReferenceRouteCompilerPlugin {\n return {\n name: 'react-stable-hmr-split-route-components',\n onSplitRouteProperty(ctx) {\n if (ctx.splitNodeMeta.splitStrategy !== 'lazyRouteComponent') {\n return\n }\n\n const stableComponentIdent = getUniqueProgramIdentifier(\n ctx.programPath,\n `TSRSplit${capitalizeIdentifier(ctx.splitNodeMeta.exporterIdent)}`,\n )\n\n const hotDataKey = createHotDataKey(ctx.splitNodeMeta.exporterIdent)\n\n ctx.insertionPath.insertBefore(\n buildStableSplitComponentStatements({\n stableComponentIdent,\n hotDataKey: t.stringLiteral(hotDataKey),\n hotExpression: hotExpressionAstFor(opts.hmrStyle),\n lazyRouteComponentIdent: t.identifier(ctx.lazyRouteComponentIdent),\n localImporterIdent: t.identifier(\n ctx.splitNodeMeta.localImporterIdent,\n ),\n exporterIdent: t.stringLiteral(ctx.splitNodeMeta.exporterIdent),\n }),\n )\n\n return t.identifier(stableComponentIdent.name)\n },\n }\n}\n"],"mappings":";;;;;;;AAMA,SAAS,qBAAqB,KAAa;CACzC,OAAO,IAAI,GAAI,YAAY,IAAI,IAAI,MAAM,CAAC;AAC5C;AAEA,SAAS,iBAAiB,YAAoB;CAC5C,OAAO,uBAAuB;AAChC;AAEA,IAAM,sCAAsC,gBAAS,WACnD;;;;;;;;;KAUA,EACE,uBAAuB,KACzB,CACF;AAEA,SAAS,oBAAoB,UAAkC;CAC7D,OAAO,gBAAS,WAAW,IACzB,aAAa,YAAY,2BAA2B,iBACtD;AACF;AAEA,SAAgB,+CAA+C,MAE9B;CAC/B,OAAO;EACL,MAAM;EACN,qBAAqB,KAAK;GACxB,IAAI,IAAI,cAAc,kBAAkB,sBACtC;GAGF,MAAM,uBAAuB,cAAA,2BAC3B,IAAI,aACJ,WAAW,qBAAqB,IAAI,cAAc,aAAa,GACjE;GAEA,MAAM,aAAa,iBAAiB,IAAI,cAAc,aAAa;GAEnE,IAAI,cAAc,aAChB,oCAAoC;IAClC;IACA,YAAY,aAAE,cAAc,UAAU;IACtC,eAAe,oBAAoB,KAAK,QAAQ;IAChD,yBAAyB,aAAE,WAAW,IAAI,uBAAuB;IACjE,oBAAoB,aAAE,WACpB,IAAI,cAAc,kBACpB;IACA,eAAe,aAAE,cAAc,IAAI,cAAc,aAAa;GAChE,CAAC,CACH;GAEA,OAAO,aAAE,WAAW,qBAAqB,IAAI;EAC/C;CACF;AACF"}