-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Crate standalone fs router package (#11)
* refactor: moved fs-router to standalone package * chore: remove useless split source function * feat: add simple test cases * fix: README formatting * fix: remove useless RouteSubNode * feat: update version to v0.4.4
- Loading branch information
1 parent
bc7ffc2
commit e4d4a75
Showing
31 changed files
with
261 additions
and
724 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# tuono-fs-router-vite-plugin | ||
|
||
This is a vite plugin for [tuono](https://github.com/Valerioageno/tuono). | ||
|
||
This package specifically handles the file system based routing. | ||
|
||
Check [tuono](https://github.com/Valerioageno/tuono) for more. | ||
|
||
## Credits | ||
|
||
This plugin is strongly inspired by the [@tanstack/router](https://tanstack.com/router/latest) | ||
route generator plugin. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
"name": "tuono-fs-router-vite-plugin", | ||
"version": "0.4.4", | ||
"description": "Plugin for the tuono's file system router. Tuono is the react/rust fullstack framework", | ||
"scripts": { | ||
"dev": "vite build --watch", | ||
"build": "vite build", | ||
"lint": "eslint --ext .ts,.tsx ./src -c ../../.eslintrc", | ||
"format": "prettier -u --write --ignore-unknown '**/*'", | ||
"format:check": "prettier --check --ignore-unknown '**/*'", | ||
"types": "tsc --noEmit", | ||
"test:watch": "vitest", | ||
"test": "vitest run" | ||
}, | ||
"keywords": [], | ||
"author": "Valerio Ageno", | ||
"license": "MIT", | ||
"type": "module", | ||
"types": "dist/esm/index.d.ts", | ||
"main": "dist/cjs/index.cjs", | ||
"module": "dist/esm/index.js", | ||
"files": [ | ||
"dist", | ||
"src", | ||
"README.md" | ||
], | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./dist/esm/index.d.ts", | ||
"default": "./dist/esm/index.js" | ||
}, | ||
"require": { | ||
"types": "./dist/cjs/index.d.cts", | ||
"default": "./dist/cjs/index.cjs" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"dependencies": { | ||
"@babel/core": "^7.24.4", | ||
"@babel/types": "^7.24.0", | ||
"prettier": "^3.2.4", | ||
"vite": "^5.2.11" | ||
}, | ||
"devDependencies": { | ||
"@tanstack/config": "^0.7.11", | ||
"@types/babel__core": "^7.20.5", | ||
"vitest": "^1.5.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const ROUTES_FOLDER = './src/routes/' | ||
export const ROOT_PATH_ID = '__root' | ||
export const GENERATED_ROUTE_TREE = './.tuono/routeTree.gen.ts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { routeGenerator } from './generator' | ||
|
||
import { normalize } from 'path' | ||
|
||
import type { Plugin } from 'vite' | ||
|
||
const ROUTES_DIRECTORY_PATH = './src/routes' | ||
|
||
let lock = false | ||
|
||
export default function RouterGenerator(): Plugin { | ||
const generate = async (): Promise<void> => { | ||
if (lock) return | ||
lock = true | ||
|
||
try { | ||
await routeGenerator() | ||
} catch (err) { | ||
console.error(err) | ||
} finally { | ||
lock = false | ||
} | ||
} | ||
|
||
const handleFile = async (file: string): Promise<void> => { | ||
const filePath = normalize(file) | ||
|
||
if (filePath.startsWith(ROUTES_DIRECTORY_PATH)) { | ||
await generate() | ||
} | ||
} | ||
|
||
return { | ||
name: 'vite-plugin-tuono-fs-router', | ||
configResolved: async (): Promise<void> => { | ||
await generate() | ||
}, | ||
watchChange: async ( | ||
file: string, | ||
context: { event: string }, | ||
): Promise<void> => { | ||
if (['create', 'update', 'delete'].includes(context.event)) { | ||
await handleFile(file) | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import fs from 'fs/promises' | ||
import { describe, it, expect } from 'vitest' | ||
import { routeGenerator } from '../src/generator' | ||
|
||
function makeFolderDir(folder: string) { | ||
return process.cwd() + `/tests/generator/${folder}` | ||
} | ||
|
||
async function getRouteTreeFileText(folder: string) { | ||
const dir = makeFolderDir(folder) | ||
return await fs.readFile(dir + '/routeTree.gen.ts', 'utf-8') | ||
} | ||
|
||
async function getExpectedRouteTreeFileText(folder: string) { | ||
const dir = makeFolderDir(folder) | ||
const location = dir + '/routeTree.expected.ts' | ||
return await fs.readFile(location, 'utf-8') | ||
} | ||
|
||
describe('generator works', async () => { | ||
const folderNames = await fs.readdir(process.cwd() + '/tests/generator') | ||
|
||
it.each(folderNames.map((folder) => [folder]))( | ||
'should wire-up the routes for a "%s" tree', | ||
async (folderName) => { | ||
const currentFolder = `${process.cwd()}/tests/generator/${folderName}` | ||
|
||
await routeGenerator({ | ||
folderName: `${currentFolder}/routes`, | ||
generatedRouteTree: `${currentFolder}/routeTree.gen.ts`, | ||
}) | ||
|
||
const [expectedRouteTree, generatedRouteTree] = await Promise.all([ | ||
getExpectedRouteTreeFileText(folderName), | ||
getRouteTreeFileText(folderName), | ||
]) | ||
|
||
console.log(generatedRouteTree) | ||
|
||
expect(generatedRouteTree).equal(expectedRouteTree) | ||
}, | ||
) | ||
}) |
40 changes: 40 additions & 0 deletions
40
packages/fs-router-vite-plugin/tests/generator/multi-level/routeTree.expected.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// This file is auto-generated by Tuono | ||
|
||
import { createRoute, dynamic } from 'tuono' | ||
|
||
import RootImport from './routes/__root' | ||
|
||
const AboutImport = dynamic(() => import('./routes/about')) | ||
const IndexImport = dynamic(() => import('./routes/index')) | ||
const PostsMyPostImport = dynamic(() => import('./routes/posts/my-post')) | ||
|
||
const rootRoute = createRoute({ isRoot: true, component: RootImport }) | ||
|
||
const About = createRoute({ component: AboutImport }) | ||
const Index = createRoute({ component: IndexImport }) | ||
const PostsMyPost = createRoute({ component: PostsMyPostImport }) | ||
|
||
// Create/Update Routes | ||
|
||
const AboutRoute = About.update({ | ||
path: '/about', | ||
getParentRoute: () => rootRoute, | ||
}) | ||
|
||
const IndexRoute = Index.update({ | ||
path: '/', | ||
getParentRoute: () => rootRoute, | ||
}) | ||
|
||
const PostsMyPostRoute = PostsMyPost.update({ | ||
path: '/posts/my-post', | ||
getParentRoute: () => rootRoute, | ||
}) | ||
|
||
// Create and export the route tree | ||
|
||
export const routeTree = rootRoute.addChildren([ | ||
IndexRoute, | ||
AboutRoute, | ||
PostsMyPostRoute, | ||
]) |
1 change: 1 addition & 0 deletions
1
packages/fs-router-vite-plugin/tests/generator/multi-level/routes/__root.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/** */ |
1 change: 1 addition & 0 deletions
1
packages/fs-router-vite-plugin/tests/generator/multi-level/routes/about.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/** */ |
1 change: 1 addition & 0 deletions
1
packages/fs-router-vite-plugin/tests/generator/multi-level/routes/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/** */ |
1 change: 1 addition & 0 deletions
1
packages/fs-router-vite-plugin/tests/generator/multi-level/routes/posts/my-post.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/** */ |
29 changes: 29 additions & 0 deletions
29
packages/fs-router-vite-plugin/tests/generator/single-level/routeTree.expected.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// This file is auto-generated by Tuono | ||
|
||
import { createRoute, dynamic } from 'tuono' | ||
|
||
import RootImport from './routes/__root' | ||
|
||
const AboutImport = dynamic(() => import('./routes/about')) | ||
const IndexImport = dynamic(() => import('./routes/index')) | ||
|
||
const rootRoute = createRoute({ isRoot: true, component: RootImport }) | ||
|
||
const About = createRoute({ component: AboutImport }) | ||
const Index = createRoute({ component: IndexImport }) | ||
|
||
// Create/Update Routes | ||
|
||
const AboutRoute = About.update({ | ||
path: '/about', | ||
getParentRoute: () => rootRoute, | ||
}) | ||
|
||
const IndexRoute = Index.update({ | ||
path: '/', | ||
getParentRoute: () => rootRoute, | ||
}) | ||
|
||
// Create and export the route tree | ||
|
||
export const routeTree = rootRoute.addChildren([IndexRoute, AboutRoute]) |
1 change: 1 addition & 0 deletions
1
packages/fs-router-vite-plugin/tests/generator/single-level/routes/__root.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/** */ |
1 change: 1 addition & 0 deletions
1
packages/fs-router-vite-plugin/tests/generator/single-level/routes/about.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/** */ |
1 change: 1 addition & 0 deletions
1
packages/fs-router-vite-plugin/tests/generator/single-level/routes/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/** */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src", "vite.config.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { defineConfig, mergeConfig } from 'vitest/config' | ||
import { tanstackBuildConfig } from '@tanstack/config/build' | ||
|
||
const config = defineConfig({}) | ||
|
||
export default mergeConfig( | ||
config, | ||
tanstackBuildConfig({ | ||
entry: './src/index.ts', | ||
srcDir: './src', | ||
}), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.