Skip to content

Commit

Permalink
Merge pull request #11961 from remix-run/pedro/vmod-for-with-props-hocs
Browse files Browse the repository at this point in the history
refactor: withprops hocs from virtual module
  • Loading branch information
pcattori authored Sep 6, 2024
2 parents c3d95f0 + 4a06638 commit ebd70a6
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 56 deletions.
13 changes: 13 additions & 0 deletions .changeset/route-component-export-props.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@react-router/dev": minor
"react-router": minor
---

Params, loader data, and action data as props for route component exports

```tsx
export default function Component({ params, loaderData, actionData }) {}

export function HydrateFallback({ params }) {}
export function ErrorBoundary({ params, loaderData, actionData }) {}
```
1 change: 1 addition & 0 deletions packages/react-router-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"arg": "^5.0.1",
"babel-dead-code-elimination": "^1.0.6",
"chalk": "^4.1.2",
"dedent": "^1.5.3",
"es-module-lexer": "^1.3.1",
"exit-hook": "2.2.1",
"fs-extra": "^10.0.0",
Expand Down
5 changes: 3 additions & 2 deletions packages/react-router-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
resolveEntryFiles,
resolvePublicPath,
} from "./config";
import { withProps } from "./with-props";
import * as WithProps from "./with-props";

export async function resolveViteConfig({
configFile,
Expand Down Expand Up @@ -1414,6 +1414,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = (_config) => {
}
},
},
WithProps.plugin,
{
name: "react-router-route-exports",
async transform(code, id, options) {
Expand Down Expand Up @@ -1454,7 +1455,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = (_config) => {

let ast = parse(code, { sourceType: "module" });
removeExports(ast, SERVER_ONLY_ROUTE_EXPORTS);
withProps(ast);
WithProps.transform(ast);
return generate(ast, {
sourceMaps: true,
filename: id,
Expand Down
56 changes: 54 additions & 2 deletions packages/react-router-dev/vite/with-props.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,61 @@
import type { Plugin } from "vite";
import dedent from "dedent";

import type { Babel, NodePath, ParseResult } from "./babel";
import { traverse, t } from "./babel";
import * as VirtualModule from "./vmod";

const vmodId = VirtualModule.id("with-props");

const NAMED_COMPONENT_EXPORTS = ["HydrateFallback", "ErrorBoundary"];

export const withProps = (ast: ParseResult<Babel.File>) => {
export const plugin: Plugin = {
name: "react-router-with-props",
enforce: "pre",
resolveId(id) {
if (id === vmodId) return VirtualModule.resolve(vmodId);
},
async load(id) {
if (id !== VirtualModule.resolve(vmodId)) return;
return dedent`
import { createElement as h } from "react";
import { useActionData, useLoaderData, useParams } from "react-router";
export function withComponentProps(Component) {
return function Wrapped() {
const props = {
params: useParams(),
loaderData: useLoaderData(),
actionData: useActionData(),
};
return h(Component, props);
};
}
export function withHydrateFallbackProps(HydrateFallback) {
return function Wrapped() {
const props = {
params: useParams(),
};
return h(HydrateFallback, props);
};
}
export function withErrorBoundaryProps(ErrorBoundary) {
return function Wrapped() {
const props = {
params: useParams(),
loaderData: useLoaderData(),
actionData: useActionData(),
};
return h(ErrorBoundary, props);
};
}
`;
},
};

export const transform = (ast: ParseResult<Babel.File>) => {
const hocs: Array<[string, Babel.Identifier]> = [];
function getHocUid(path: NodePath, hocName: string) {
const uid = path.scope.generateUidIdentifier(hocName);
Expand Down Expand Up @@ -72,7 +124,7 @@ export const withProps = (ast: ParseResult<Babel.File>) => {
hocs.map(([name, identifier]) =>
t.importSpecifier(identifier, t.identifier(name))
),
t.stringLiteral("react-router")
t.stringLiteral(vmodId)
)
);
}
Expand Down
5 changes: 0 additions & 5 deletions packages/react-router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ export {
useRouteLoaderData,
useRoutes,
} from "./lib/hooks";
export {
withComponentProps,
withHydrateFallbackProps,
withErrorBoundaryProps,
} from "./lib/hocs";

// Expose old RR DOM API
export type {
Expand Down
47 changes: 0 additions & 47 deletions packages/react-router/lib/hocs.tsx

This file was deleted.

12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ebd70a6

Please sign in to comment.