fix(router-plugin): keep TypeScript namespaces intact when splitting routes #8818
Openjxom(GitHub profile, opens in a new tab)wants to merge 1 commit into
main fromrefs/pull/8818/headopened - packages/router-utils/src/compiler-helpers.ts line 640@@ -592,3 +635,6 @@592635 map.set(declaration.id.name, declaration)593636 } else if (t.isClassDeclaration(declaration) && declaration.id) {594637 map.set(declaration.id.name, declaration)638+ } else if (639+ isRuntimeNamespace(declaration) &&640+ !map.has(declaration.id.name)packages/router-plugin/tests/code-splitter-scope.test.ts line 346@@ -337,3 +337,10 @@337337 expect(sharedBindings).toEqual(['a', 'b', 'c', 'r'])338338 })339339 })340+341+describe('TypeScript namespaces', () => {342+ // `namespace styles { export const title = ... }` compiles to a value. Its343+ // `export` is a member of the namespace, not a module export.344+ it('keeps the members of a namespace the split component reads', async () => {345+ const { modules } = compileRouteModules(346+ pageRoute(`function Page() {packages/router-plugin/src/core/code-splitter/compilers.ts line 1195@@ -1173,3 +1190,6 @@11731190 // becomes11741191 // import { x } from '${opts.id}'117511921193+ // An export inside a namespace is a member of the namespace,1194+ // not a module export.1195+ if (!path.parentPath.isProgram()) return
- packages/router-plugin/src/core/code-splitter/compilers.ts lines 1193 to 1196@@ -1173,3 +1190,7 @@11731190 // becomes11741191 // import { x } from '${opts.id}'117511921193+ // An export inside a namespace is a member of the namespace,1194+ // not a module export.1195+ if (!path.parentPath.isProgram()) return1196+packages/router-utils/src/compiler-helpers.ts lines 265 to 269@@ -245,3 +259,11 @@245259 ) {246260 if (!current) return247261262+ // A type annotation, such as `Page.Props` in a return type, reads no value263+ // at runtime. Type arguments are still read, since a plain JavaScript264+ // `a < b > (c)` parses as a call with a type argument.265+ if (266+ t.isTSTypeAnnotation(current) ||267+ t.isTSTypeParameterDeclaration(current) ||268+ t.isTSDeclareFunction(current) ||269+ (t.isTSModuleDeclaration(current) && current.declare)packages/router-utils/src/compiler-helpers.ts lines 638 to 643@@ -592,3 +635,9 @@592635 map.set(declaration.id.name, declaration)593636 } else if (t.isClassDeclaration(declaration) && declaration.id) {594637 map.set(declaration.id.name, declaration)638+ } else if (639+ isRuntimeNamespace(declaration) &&640+ !map.has(declaration.id.name)641+ ) {642+ // A merged namespace maps to its first declaration.643+ map.set(declaration.id.name, declaration)
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: f8f8a129e841d49dd2f1ccceca794fca0558a6c6
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 10 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR