diff --git a/src/VitePlugin.md b/src/VitePlugin.md deleted file mode 100644 index 15a5084..0000000 --- a/src/VitePlugin.md +++ /dev/null @@ -1,327 +0,0 @@ -## ๐Ÿš€ Vite Plugin for SigPro Router - -Automatically generates routes from your file structure with zero configuration. - -### ๐Ÿ“ File Structure - -``` -src/ - pages/ - index.js โ†’ / - about.js โ†’ /about - contact.js โ†’ /contact - user/ - index.js โ†’ /user - [id].js โ†’ /user/:id - [id]/ - posts.js โ†’ /user/:id/posts - [pid].js โ†’ /user/:id/posts/:pid - blog/ - [year]/ - [month]/ - [slug].js โ†’ /blog/:year/:month/:slug - dashboard/ - index.js โ†’ /dashboard - settings.js โ†’ /dashboard/settings -``` - -### ๐Ÿ“ฆ Installation - -```bash -npm install --save-dev @sigpro/vite-plugin -# or -yarn add -D @sigpro/vite-plugin -# or -bun add -D @sigpro/vite-plugin -``` - -### โš™๏ธ Setup in `vite.config.js` - -```javascript -import { defineConfig } from 'vite'; -import sigproRouter from '@sigpro/vite-plugin'; - -export default defineConfig({ - plugins: [sigproRouter()] -}); -``` - -### ๐ŸŽฏ Usage in Your App - -```javascript -// src/app.js -import { $, html } from 'sigpro'; -import { routes } from 'virtual:sigpro-routes'; - -const App = () => html` -
- - -
- ${$.router(routes)} -
-
-`; - -document.body.appendChild(App()); -``` - -### ๐Ÿ“„ Creating Pages - -#### Static Pages - -```javascript -// src/pages/index.js -export default (params) => $.page(() => { - return html` -
-

Home Page

-

Welcome to my app!

-
- `; -}); - -// src/pages/about.js -export default (params) => $.page(() => { - return html` -
-

About Us

-

Learn more about our company.

-
- `; -}); -``` - -#### Dynamic Pages with Parameters - -```javascript -// src/pages/user/[id].js -export default (params) => $.page(({ onUnmount }) => { - // params = { id: '42' } - const userId = params.id; - const userData = $(null); - const loading = $(true); - - // Fetch user data - const fetchUser = async () => { - loading(true); - try { - const res = await fetch(`/api/users/${userId}`); - const data = await res.json(); - userData(data); - } catch (error) { - console.error('Error:', error); - } finally { - loading(false); - } - }; - - fetchUser(); - - return html` -
- ${loading() ? html` -
Loading user ${userId}...
- ` : html` -

${userData().name}

-

Email: ${userData().email}

-

Role: ${userData().role}

- `} -
- `; -}); - -// src/pages/user/[id]/posts/[pid].js -export default (params) => $.page(() => { - // params = { id: '42', pid: '123' } - const { id, pid } = params; - - return html` -
-

Post ${pid}

-

From user ${id}

- โ† Back to posts -
- `; -}); -``` - -#### Nested Dynamic Routes - -```javascript -// src/pages/blog/[year]/[month]/[slug].js -export default (params) => $.page(() => { - // params = { year: '2024', month: '03', slug: 'hello-world' } - const { year, month, slug } = params; - - return html` -
-

${slug.replace(/-/g, ' ')}

- -
-

Blog post content here...

-
- โ† Back to blog -
- `; -}); -``` - -### ๐Ÿ”„ Automatic Cleanup - -```javascript -// src/pages/user/[id].js -export default (params) => $.page(({ onUnmount }) => { - const userId = params.id; - - // โœ… Auto-cleaned by SigPro - $.effect(() => { - console.log('User effect running for', userId); - }); - - // โš ๏ธ Manual cleanup needed - const interval = setInterval(() => { - console.log('Polling user', userId); - }, 5000); - - const handleResize = () => { - console.log('Window resized'); - }; - window.addEventListener('resize', handleResize); - - // โœ… Cleanup with onUnmount - onUnmount(() => { - clearInterval(interval); - window.removeEventListener('resize', handleResize); - console.log('๐Ÿงน Cleaned up user', userId); - }); - - return html`
User ${userId}
`; -}); -``` - -### ๐Ÿงญ Navigation - -```javascript -// Programmatic navigation -import { $ } from 'sigpro'; - -$.router.go('/user/42'); -$.router.go('/user/42/posts/123'); -$.router.go('about'); // Auto-adds leading slash - -// In templates -html` - -`; -``` - -### ๐ŸŽจ Route Parameters in Components - -```javascript -// Access params anywhere -const currentUserId = () => { - const match = window.location.hash.match(/^#\/user\/([^/]+)/); - return match ? match[1] : null; -}; - -// Use in components -$.component('user-header', (props) => { - const userId = currentUserId(); - - return html` -
-

User Dashboard: ${userId}

-
- `; -}, []); -``` - -### โšก Advanced: Custom 404 Page - -```javascript -// src/pages/404.js -export default (params) => $.page(() => { - return html` -
-

404

-

Page not found

- - Go back home - -
- `; -}); - -// The router automatically shows 404 when no route matches -``` - -### ๐Ÿ“Š Route Order (Automatic) - -The plugin automatically orders routes from most specific to least specific: - -```javascript -// Generated order: -[ - { path: '/user/:id/posts/:pid' }, // Most specific first - { path: '/user/:id/posts' }, - { path: '/user/:id' }, - { path: '/user' }, - { path: '/about' }, - { path: '/' }, // Least specific last -] -``` - -### ๐ŸŽฏ API Reference - -#### Virtual Module -```javascript -import { routes } from 'virtual:sigpro-routes'; -``` - -#### Route Object -```javascript -{ - path: string, // '/user/:id' - Auto-generated from file structure - component: Function // Page component that receives params -} -``` - -### ๐Ÿ’ก Pro Tips - -1. **Index files** become the parent route - - `user/index.js` โ†’ `/user` - - `user/[id]/index.js` โ†’ `/user/:id` - -2. **Square brackets** `[param]` become route parameters `:param` - - `[id].js` โ†’ `/:id` - - `[slug].js` โ†’ `/:slug` - -3. **Nested folders** create nested routes - - `user/[id]/posts/[pid].js` โ†’ `/user/:id/posts/:pid` - -4. **Cleanup** is automatic for pages, but manual for intervals/listeners - -### ๐Ÿš€ Benefits - -- โœ… **Zero config** - Just create files in `src/pages/` -- โœ… **Auto-discovery** - No need to manually define routes -- โœ… **File-based routing** - Intuitive and scalable -- โœ… **Dynamic routes** - `[param]` syntax for parameters -- โœ… **Nested routes** - Mirror your folder structure -- โœ… **Automatic ordering** - Most specific routes first -- โœ… **TypeScript ready** - Works with `.tsx` files -- โœ… **Memory leak free** - Built on `$.page` cleanup