From 10eb05902ca3f44d36ebd7fe7681987ceeaec94e Mon Sep 17 00:00:00 2001 From: Sean Cassiere <33615041+SeanCassiere@users.noreply.github.com> Date: Sat, 22 Jun 2024 04:22:04 +1200 Subject: [PATCH] fix(router-plugin): `experimental.enableCodeSplitting` - allow the use of inlined wrappers for the route options when splitting nodes (#1813) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test(router-plugin): tests for the route-items being wrapped in React.memo * fix(router-plugin): React.memo breaks the codesplitting * test(router-plugin): snapshots for the React.memo stuff * refactor(router-plugin): variable naming is hard 😅 * chore(router-plugin): whoops --- packages/router-plugin/src/compilers.ts | 39 ++++++++++++++++++- ...ructured-react-memo-imported-component.tsx | 9 +++++ ...ed-react-memo-imported-component@split.tsx | 9 +++++ .../snapshots/react-memo-component.tsx | 9 +++++ .../snapshots/react-memo-component@split.tsx | 9 +++++ .../react-memo-imported-component.tsx | 9 +++++ .../react-memo-imported-component@split.tsx | 6 +++ ...ructured-react-memo-imported-component.tsx | 13 +++++++ .../test-files/react-memo-component.tsx | 13 +++++++ .../react-memo-imported-component.tsx | 9 +++++ 10 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 packages/router-plugin/tests/code-splitter/snapshots/destructured-react-memo-imported-component.tsx create mode 100644 packages/router-plugin/tests/code-splitter/snapshots/destructured-react-memo-imported-component@split.tsx create mode 100644 packages/router-plugin/tests/code-splitter/snapshots/react-memo-component.tsx create mode 100644 packages/router-plugin/tests/code-splitter/snapshots/react-memo-component@split.tsx create mode 100644 packages/router-plugin/tests/code-splitter/snapshots/react-memo-imported-component.tsx create mode 100644 packages/router-plugin/tests/code-splitter/snapshots/react-memo-imported-component@split.tsx create mode 100644 packages/router-plugin/tests/code-splitter/test-files/destructured-react-memo-imported-component.tsx create mode 100644 packages/router-plugin/tests/code-splitter/test-files/react-memo-component.tsx create mode 100644 packages/router-plugin/tests/code-splitter/test-files/react-memo-imported-component.tsx diff --git a/packages/router-plugin/src/compilers.ts b/packages/router-plugin/src/compilers.ts index c8fc7aaeba..f7c86bd7ee 100644 --- a/packages/router-plugin/src/compilers.ts +++ b/packages/router-plugin/src/compilers.ts @@ -1,8 +1,9 @@ import * as t from '@babel/types' +import babel from '@babel/core' +import generate from '@babel/generator' import * as template from '@babel/template' import { splitPrefix } from './constants' import { eliminateUnreferencedIdentifiers } from './eliminateUnreferencedIdentifiers' -import type * as babel from '@babel/core' import type { CompileAstFn } from './ast' type SplitModulesById = Record< @@ -431,8 +432,42 @@ export async function splitFile(opts: { ), ]), ) + } else if (t.isCallExpression(splitNode)) { + const outputSplitNodeCode = generate(splitNode).code + const splitNodeAst = babel.parse(outputSplitNodeCode) + + if (!splitNodeAst) { + throw new Error( + `Failed to parse the generated code for "${splitType}" in the node type "${splitNode.type}"`, + ) + } + + const statement = splitNodeAst.program.body[0] + + if (!statement) { + throw new Error( + `Failed to parse the generated code for "${splitType}" in the node type "${splitNode.type}" as no statement was found in the program body`, + ) + } + + if (t.isExpressionStatement(statement)) { + const expression = statement.expression + programPath.pushContainer( + 'body', + t.variableDeclaration('const', [ + t.variableDeclarator( + t.identifier(splitType), + expression, + ), + ]), + ) + } else { + throw new Error( + `Unexpected expression type encounter for "${splitType}" in the node type "${splitNode.type}"`, + ) + } } else { - console.info(splitNode) + console.info('Unexpected splitNode type:', splitNode) throw new Error( `Unexpected splitNode type ☝️: ${splitNode.type}`, ) diff --git a/packages/router-plugin/tests/code-splitter/snapshots/destructured-react-memo-imported-component.tsx b/packages/router-plugin/tests/code-splitter/snapshots/destructured-react-memo-imported-component.tsx new file mode 100644 index 0000000000..446bb12344 --- /dev/null +++ b/packages/router-plugin/tests/code-splitter/snapshots/destructured-react-memo-imported-component.tsx @@ -0,0 +1,9 @@ +const $$splitLoaderImporter = () => import('tsr-split:destructured-react-memo-imported-component.tsx?tsr-split'); +import { lazyFn } from '@tanstack/react-router'; +const $$splitComponentImporter = () => import('tsr-split:destructured-react-memo-imported-component.tsx?tsr-split'); +import { lazyRouteComponent } from '@tanstack/react-router'; +import { createFileRoute } from '@tanstack/react-router'; +export const Route = createFileRoute('/')({ + component: lazyRouteComponent($$splitComponentImporter, 'component'), + loader: lazyFn($$splitLoaderImporter, 'loader') +}); \ No newline at end of file diff --git a/packages/router-plugin/tests/code-splitter/snapshots/destructured-react-memo-imported-component@split.tsx b/packages/router-plugin/tests/code-splitter/snapshots/destructured-react-memo-imported-component@split.tsx new file mode 100644 index 0000000000..93f0e29a19 --- /dev/null +++ b/packages/router-plugin/tests/code-splitter/snapshots/destructured-react-memo-imported-component@split.tsx @@ -0,0 +1,9 @@ +import { memo } from 'react'; +import { importedLoader } from '../shared/imported'; +function Component() { + return