Skip to content

Commit

Permalink
feat: support name conventions of next remix releases
Browse files Browse the repository at this point in the history
  • Loading branch information
rush committed Jul 11, 2024
1 parent 82f9d08 commit 8163e80
Show file tree
Hide file tree
Showing 49 changed files with 83 additions and 67 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

>For more information, please refer to the React Router [documentation](https://reactrouter.com/en/main). Note that it follows the Remix file convention.
>**Important**
although react-router use names `loader`, `action`, `Component` these parts should exported from file as `clientLoader`, `clientAction`, `default`, see example below. use these name for future react-router releases.

## Install

```bash
Expand Down Expand Up @@ -117,12 +120,14 @@ export const caseSensitive = false

export const id = 'main-page'

export async function loader() {}
// every `loader` should exported by name `clientLoader` from v2
export async function clientLoader() {}

export async function action() {}
// every `action` should exported by name `clientAction` from v2
export async function clientAction() {}

// every component should exported without default with name `Component`
export function Component() {
// every component should exported as `default` no matter what is the name from v2
export default function Component() {
return <h1>Hello Remix Router!</h1>
}

Expand Down
4 changes: 3 additions & 1 deletion playground/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import React from 'react'
import { Outlet } from 'react-router-dom'

export function Component() {

Check failure on line 6 in playground/app/root.tsx

View workflow job for this annotation

GitHub Actions / lint

More than 1 blank line not allowed

export default function Component() {
return (
<>
<h1>test for first page</h1>
Expand Down
2 changes: 1 addition & 1 deletion playground/app/routes/($lang).dashboard.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>dashboard</h1>
}
2 changes: 1 addition & 1 deletion playground/app/routes/_auth.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react'
import { Outlet } from 'react-router-dom'

export function Component() {
export default function Component() {
return (
<>
<h1>hidden auth layout</h1>
Expand Down
4 changes: 2 additions & 2 deletions playground/app/routes/_auth.login.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react'
import { useLoaderData } from 'react-router-dom'

export async function loader() {
export async function clientLoader() {
// sleep 2 seconds then return data
await new Promise(resolve => setTimeout(resolve, 2000))
return ['Note 1', 'Note 2', 'Note 3']
}

export function Component() {
export default function Component() {
const notes = useLoaderData() as string[]

return (
Expand Down
4 changes: 2 additions & 2 deletions playground/app/routes/_index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { useState } from 'react'
import { useLoaderData } from 'react-router-dom'

export async function loader() {
export async function clientLoader() {
return 'Vite + React'
}

export function Component() {
export default function Component() {
const [count, setCount] = useState(0)
const data = useLoaderData() as string

Expand Down
2 changes: 1 addition & 1 deletion playground/app/routes/auth.$.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>Splat Route</h1>
}
2 changes: 1 addition & 1 deletion playground/app/routes/auth.$test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>Dynamic Route</h1>
}
2 changes: 1 addition & 1 deletion playground/app/routes/auth.deploy.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { Outlet } from 'react-router-dom'

export function Component() {
export default function Component() {
return (
<>
<h1>deploy</h1>
Expand Down
2 changes: 1 addition & 1 deletion playground/app/routes/auth.deploy.test.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>deploy test</h1>
}
2 changes: 1 addition & 1 deletion playground/app/routes/auth.forgot-pass.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>forgot password</h1>
}
2 changes: 1 addition & 1 deletion playground/app/routes/auth.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react'
import { Outlet } from 'react-router-dom'

export function Component() {
export default function Component() {
return (
<>
<h1>auth layout</h1>
Expand Down
2 changes: 1 addition & 1 deletion playground/app/routes/auth.new.without.parent.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>without parent</h1>
}
2 changes: 1 addition & 1 deletion playground/app/routes/auth.recover.test.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>recover/test</h1>
}
2 changes: 1 addition & 1 deletion playground/app/routes/auth.recover/route.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { Outlet } from 'react-router-dom'

export function Component() {
export default function Component() {
return (
<>
<h1>recover</h1>
Expand Down
2 changes: 1 addition & 1 deletion playground/app/routes/concerts.$.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>Splat</h1>
}
6 changes: 3 additions & 3 deletions playground/app/routes/concerts.$city.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react'

export async function loader() {
export async function clientLoader() {
return 'loader'
}

export async function action() {
export async function clientAction() {
return 'action'
}

export function Component() {
export default function Component() {
return <h1>Dynamic</h1>
}
2 changes: 1 addition & 1 deletion playground/app/routes/concerts._index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>concerts index</h1>
}
2 changes: 1 addition & 1 deletion playground/app/routes/concerts.trending.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>concerts trending</h1>
}
2 changes: 1 addition & 1 deletion playground/app/routes/concerts.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { Outlet } from 'react-router-dom'

export function Component() {
export default function Component() {
return (
<>
<h1>concerts layout</h1>
Expand Down
2 changes: 1 addition & 1 deletion playground/app/routes/concerts_.mine.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>mine is out of concerts layout</h1>
}
2 changes: 1 addition & 1 deletion playground/app/routes/concerts_.your.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>your is out of concerts layout</h1>
}
4 changes: 2 additions & 2 deletions playground/app/routes/notes.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react'
import { useLoaderData } from 'react-router-dom'

export async function loader() {
export async function clientLoader() {
// sleep 2 seconds then return data
await new Promise(resolve => setTimeout(resolve, 2000))
return ['Note 1', 'Note 2', 'Note 3']
}

export function Component() {
export default function Component() {
const notes = useLoaderData() as string[]

return (
Expand Down
17 changes: 13 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,21 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = options =>

const routesObject = JSON.stringify(routesMap)
.replace(/"ImportStart/g, '() => import(')
.replace(/ImportEnd"/g, ')')
.replace(/"spread":"SpreadStart/g, '...')
.replace(/SpreadEnd"/g, '')
.replace(/ImportEnd"/g, ').then(moduleFactory)')
.replace(/"spread":"SpreadStart/g, '...moduleFactory(')
.replace(/SpreadEnd"/g, ')')
// console.log(JSON.stringify(routesObject, null, 2))

const routesCode = `${imports}\nexport const routes = ${routesObject}`
const routesCode = `
${imports}
function moduleFactory(module) {
const { default: Component, clientLoader: loader, actionLoader: action, loader: _loader, action: _action, Component: _Component, ...rest } = module;
return { Component, loader, action, ...rest };
}
export const routes = ${routesObject}
`
return routesCode
}
},
Expand Down
2 changes: 1 addition & 1 deletion test/sub-routes/($lang).dashboard.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>dashboard</h1>
}
2 changes: 1 addition & 1 deletion test/sub-routes/_auth.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react'
import { Outlet } from 'react-router-dom'

export function Component() {
export default function Component() {
return (
<>
<h1>hidden auth layout</h1>
Expand Down
4 changes: 2 additions & 2 deletions test/sub-routes/_auth.login.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react'
import { useLoaderData } from 'react-router-dom'

export async function loader() {
export async function clientLoader() {
// sleep 2 seconds then return data
await new Promise(resolve => setTimeout(resolve, 2000))
return ['Note 1', 'Note 2', 'Note 3']
}

export function Component() {
export default function Component() {
const notes = useLoaderData() as string[]

return (
Expand Down
4 changes: 2 additions & 2 deletions test/sub-routes/_index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { useState } from 'react'
import { useLoaderData } from 'react-router-dom'

export async function loader() {
export async function clientLoader() {
return 'Vite + React'
}

export function Component() {
export default function Component() {
const [count, setCount] = useState(0)
const data = useLoaderData() as string

Expand Down
2 changes: 1 addition & 1 deletion test/sub-routes/auth.recover/route.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { Outlet } from 'react-router-dom'

export function Component() {
export default function Component() {
return (
<>
<h1>recover</h1>
Expand Down
2 changes: 1 addition & 1 deletion test/toplevel-routes/($lang).dashboard.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>dashboard</h1>
}
2 changes: 1 addition & 1 deletion test/toplevel-routes/_auth.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react'
import { Outlet } from 'react-router-dom'

export function Component() {
export default function Component() {
return (
<>
<h1>hidden auth layout</h1>
Expand Down
4 changes: 2 additions & 2 deletions test/toplevel-routes/_auth.login.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react'
import { useLoaderData } from 'react-router-dom'

export async function loader() {
export async function clientLoader() {
// sleep 2 seconds then return data
await new Promise(resolve => setTimeout(resolve, 2000))
return ['Note 1', 'Note 2', 'Note 3']
}

export function Component() {
export default function Component() {
const notes = useLoaderData() as string[]

return (
Expand Down
4 changes: 2 additions & 2 deletions test/toplevel-routes/_index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { useState } from 'react'
import { useLoaderData } from 'react-router-dom'

export async function loader() {
export async function clientLoader() {
return 'Vite + React'
}

export function Component() {
export default function Component() {
const [count, setCount] = useState(0)
const data = useLoaderData() as string

Expand Down
2 changes: 1 addition & 1 deletion test/toplevel-routes/auth.$.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>Splat Route</h1>
}
2 changes: 1 addition & 1 deletion test/toplevel-routes/auth.$test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>Dynamic Route</h1>
}
2 changes: 1 addition & 1 deletion test/toplevel-routes/auth.deploy.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { Outlet } from 'react-router-dom'

export function Component() {
export default function Component() {
return (
<>
<h1>deploy</h1>
Expand Down
2 changes: 1 addition & 1 deletion test/toplevel-routes/auth.deploy.test.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>deploy test</h1>
}
2 changes: 1 addition & 1 deletion test/toplevel-routes/auth.forgot-pass.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

export function Component() {
export default function Component() {
return <h1>forgot password</h1>
}
Loading

0 comments on commit 8163e80

Please sign in to comment.