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

feat(plugins/fs-routes): allow to customize fresh app tree and exts #2777

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JOTSR
Copy link

@JOTSR JOTSR commented Nov 26, 2024

Currently fresh enforce usage of routes and islands as respectively "routes" and "islands" directory.
Also fresh only support js(x) and ts(x) files extensions to registers routes.

I currently facing a problem using fresh to build project to render mdx files with a custom semantic.
Despite options to overload "island" and "route" loading there is no options to define other roots.
This PR allow to customize the default behavior of routes and islands registrations.

For example:

await fsRoutes(app, {
    dir: './',
    loadIsland: (path) => import(`./islands/${path}`),
    loadRoute: async (path) => {
        const { default: Page, frontmatter } = path.endsWith('.mdx')
            ? await compileMdx(`./pages/${path}`)
            : await import(`./pages/${path}`)
    
        // Define Page component
        // Pass PageProps and allow custom props
	const Component =
            (pageProps: PageProps) => (props: Record<string, unknown>) =>
            Page({ ...pageProps, ...props })

        // Define App Wrapper
        const Wrapper = (props: PageProps) =>
            Template({ ...props, Component: Component(props), frontmatter })

        // Load wrapped route
        return { default: Wrapper }
    },
    routesDir: 'pages',
    fileExts: ['mdx'],
})

This allow the following directory structure:

Mode  Size Name
d----    -   .
d----    -  ├──  components
-a---  146  │   ├──  Authors.tsx
-----  232  │   ├──  Button.tsx
-----  492  │   ├──  Katex.tsx
-----  182  │   ├──  PapexBadge.tsx
-a---  653  │   └──  Raw.tsx
-a---  153  ├──  config.ts
-a--- 1.2k  ├──  deno.json
d----    -  ├──  islands
-----  361  │   ├──  Counter.tsx
-a---  769  │   └──  Plotly.tsx
----- 1.9k  ├──  main.ts
d----    -  ├──  pages
----- 1.1k  │   └──  main.mdx
-a---  381  └──  README.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant