Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rsc): Make Router-related imports safer for RSC #11644

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/internal/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path'
import chalk from 'chalk'

import { getPaths, getRouteHookForPage } from '@redwoodjs/project-config'
import { getRouteRegexAndParams } from '@redwoodjs/router'
import { getRouteRegexAndParams } from '@redwoodjs/router/dist/util'
import { getProject } from '@redwoodjs/structure/dist/index.js'
import type { RWRoute } from '@redwoodjs/structure/dist/model/RWRoute'

Expand Down
1 change: 0 additions & 1 deletion packages/router/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable no-var */
/* eslint-disable no-var */
/// <reference types="react/experimental" />

declare global {
Expand Down
4 changes: 4 additions & 0 deletions packages/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
"import": "./dist/util.js",
"require": "./dist/cjs/util.js"
},
"./react-util": {
"import": "./dist/react-util.js",
"require": "./dist/cjs/react-util.js"
},
"./rscCss": {
"require": "./dist/cjs/rsc/rscCss.js",
"import": "./dist/rsc/rscCss.js"
Expand Down
14 changes: 14 additions & 0 deletions packages/router/src/react-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { ReactNode } from 'react'
import { Children, isValidElement } from 'react'

export function flattenAll(children: ReactNode): ReactNode[] {
const childrenArray = Children.toArray(children)

return childrenArray.flatMap((child) => {
if (isValidElement(child) && child.props.children) {
return [child, ...flattenAll(child.props.children)]
}

return [child]
})
}
14 changes: 0 additions & 14 deletions packages/router/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
// These are utils that can be shared between both server- and client components

import type { ReactNode } from 'react'
import { Children, isValidElement } from 'react'

export function flattenAll(children: ReactNode): ReactNode[] {
const childrenArray = Children.toArray(children)

return childrenArray.flatMap((child) => {
if (isValidElement(child) && child.props.children) {
return [child, ...flattenAll(child.props.children)]
}

return [child]
})
}
/**
* Get param name, type, and match for a route.
*
Expand Down
3 changes: 2 additions & 1 deletion packages/storybook/src/mocks/MockRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import type React from 'react'

import { flattenAll } from '@redwoodjs/router/dist/react-util'
import { isValidRoute } from '@redwoodjs/router/dist/route-validators'
import type { RouterProps } from '@redwoodjs/router/dist/router'
import { flattenAll, replaceParams } from '@redwoodjs/router/dist/util'
import { replaceParams } from '@redwoodjs/router/dist/util'

export * from '@redwoodjs/router/dist/index'

Expand Down
6 changes: 2 additions & 4 deletions packages/testing/src/web/MockRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import type React from 'react'

import { flattenAll } from '@redwoodjs/router/dist/react-util'
// Bypass the `main` field in `package.json` because we alias `@redwoodjs/router`
// for jest and Storybook. Not doing so would cause an infinite loop.
// See: ./packages/testing/config/jest/web/jest-preset.js
// @ts-ignore
import { isValidRoute } from '@redwoodjs/router/dist/route-validators'
import type { RouterProps } from '@redwoodjs/router/dist/router'
import { flattenAll, replaceParams } from '@redwoodjs/router/dist/util'
// @ts-ignore
import { replaceParams } from '@redwoodjs/router/dist/util'
export * from '@redwoodjs/router/dist/index'

export const routes: { [routeName: string]: () => string } = {}
Expand Down
Loading