vite-plugin-pages2 0.32.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 hannoeru
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,660 @@
1
+ # vite-plugin-pages2
2
+
3
+ [![npm version](https://badgen.net/npm/v/vite-plugin-pages2)](https://www.npmjs.com/package/vite-plugin-pages2)
4
+ [![monthly downloads](https://badgen.net/npm/dm/vite-plugin-pages2)](https://www.npmjs.com/package/vite-plugin-pages2)
5
+ [![types](https://badgen.net/npm/types/vite-plugin-pages2)](https://github.com/wuc656/vite-plugin-pages/blob/main/src/types.ts)
6
+ [![license](https://badgen.net/npm/license/vite-plugin-pages2)](https://github.com/wuc656/vite-plugin-pages/blob/main/LICENSE)
7
+
8
+ [![Open in Visual Studio Code](https://img.shields.io/static/v1?logo=visualstudiocode&label=&message=Open%20in%20Visual%20Studio%20Code&labelColor=2c2c32&color=007acc&logoColor=007acc)](https://open.vscode.dev/wuc656/vite-plugin-pages2)
9
+
10
+ > File system based routing for Vue 3 / React / Solid applications using
11
+ > [Vite](https://github.com/vitejs/vite)
12
+
13
+ ## Getting Started
14
+
15
+ ### Vue
16
+
17
+ **🚨Important Notes🚨**
18
+
19
+ We recommend that Vue users use [unplugin-vue-router](https://github.com/posva/unplugin-vue-router) instead of this plugin.
20
+
21
+ [unplugin-vue-router](https://github.com/posva/unplugin-vue-router) is a unplugin library created by [@posva](https://github.com/posva), same auther as vue-router. It provide almost same feature as [vite-plugin-pages](https://github.com/hannoeru/vite-plugin-pages) but better intergration with vue-router, include some cool feature like auto generate route types base on your route files to provide autocomplete for vue-router.
22
+
23
+ #### Install:
24
+
25
+ ```bash
26
+ npm install -D vite-plugin-pages2
27
+ npm install vue-router
28
+ ```
29
+
30
+ ### React
31
+
32
+ > since v0.19.0 we only support react-router v6, if you are using react-router v5 use v0.18.2.
33
+
34
+ #### Install:
35
+
36
+ ```bash
37
+ npm install -D vite-plugin-pages2
38
+ npm install react-router react-router-dom
39
+ ```
40
+
41
+ ### Solid
42
+
43
+ #### Install:
44
+
45
+ ```bash
46
+ npm install -D vite-plugin-pages2
47
+ npm install @solidjs/router
48
+ ```
49
+
50
+ ### Vite config
51
+
52
+ Add to your `vite.config.js`:
53
+
54
+ ```js
55
+ import Pages from 'vite-plugin-pages'
56
+
57
+ export default {
58
+ plugins: [
59
+ // ...
60
+ Pages(),
61
+ ],
62
+ }
63
+ ```
64
+
65
+ ## Overview
66
+
67
+ By default a page is a Vue component exported from a `.vue` or `.js` file in the
68
+ `src/pages` directory.
69
+
70
+ You can access the generated routes by importing the `~pages`
71
+ module in your application.
72
+
73
+ ### Vue
74
+
75
+ ```ts
76
+ import { createRouter } from 'vue-router'
77
+ import routes from '~pages'
78
+
79
+ const router = createRouter({
80
+ // ...
81
+ routes,
82
+ })
83
+ ```
84
+
85
+ **Type**
86
+
87
+ ```ts
88
+ // vite-env.d.ts
89
+ /// <reference types="vite-plugin-pages/client" />
90
+ ```
91
+
92
+ ### React
93
+
94
+ **experimental**
95
+
96
+ ```tsx
97
+ import { StrictMode, Suspense } from 'react'
98
+ import { createRoot } from 'react-dom/client'
99
+ import {
100
+ BrowserRouter,
101
+ useRoutes,
102
+ } from 'react-router-dom'
103
+
104
+ import routes from '~react-pages'
105
+
106
+ function App() {
107
+ return (
108
+ <Suspense fallback={<p>Loading...</p>}>
109
+ {useRoutes(routes)}
110
+ </Suspense>
111
+ )
112
+ }
113
+
114
+ const app = createRoot(document.getElementById('root')!)
115
+
116
+ app.render(
117
+ <StrictMode>
118
+ <BrowserRouter>
119
+ <App />
120
+ </BrowserRouter>
121
+ </StrictMode>,
122
+ )
123
+ ```
124
+
125
+ **Type**
126
+
127
+ ```ts
128
+ // vite-env.d.ts
129
+ /// <reference types="vite-plugin-pages/client-react" />
130
+ ```
131
+
132
+ ### Solid
133
+
134
+ #### Passing routes to solid-router
135
+
136
+ This guide is for solid-router v0.10.x and newer. For older versions see the [migration guide](https://github.com/solidjs/solid-router/blob/1c9eb8ed2cb70e4fa5a53d0d2836fc112e8ac4a0/README.md?plain=1#L925).
137
+
138
+ ```tsx
139
+ import { Router } from '@solidjs/router'
140
+ import { render } from 'solid-js/web'
141
+ import routes from '~solid-pages'
142
+
143
+ render(
144
+ () => {
145
+ return (
146
+ <Router
147
+ root={props => (
148
+ <Suspense>
149
+ {props.children}
150
+ </Suspense>
151
+ )}
152
+ >
153
+ {routes}
154
+ </Router>
155
+ )
156
+ },
157
+ document.getElementById('root') as HTMLElement,
158
+ )
159
+ ```
160
+
161
+ #### Configuring vite-plugin with SolidJS
162
+
163
+ Remember to check the `dirs` is set to the correct routes directory in `vite.config.ts`:
164
+
165
+ ```ts
166
+ import { defineConfig } from 'vite'
167
+ import Pages from 'vite-plugin-pages'
168
+ import solidPlugin from 'vite-plugin-solid'
169
+
170
+ export default defineConfig({
171
+ plugins: [
172
+ Pages({
173
+ dirs: ['src/pages'],
174
+ }),
175
+ solidPlugin()
176
+ ],
177
+ })
178
+ ```
179
+
180
+ **Type**
181
+
182
+ ```ts
183
+ // vite-env.d.ts
184
+ /// <reference types="vite-plugin-pages/client-solid" />
185
+ ```
186
+
187
+ ## Configuration
188
+
189
+ To use custom configuration, pass your options to Pages when instantiating the
190
+ plugin:
191
+
192
+ ```js
193
+ // vite.config.js
194
+ import Pages from 'vite-plugin-pages'
195
+
196
+ export default {
197
+ plugins: [
198
+ Pages({
199
+ dirs: 'src/views',
200
+ }),
201
+ ],
202
+ }
203
+ ```
204
+
205
+ ### dirs
206
+
207
+ - **Type:** `string | (string | PageOptions)[]`
208
+ - **Default:** `'src/pages'`
209
+
210
+ Paths to the pages directory. Supports globs.
211
+
212
+ Can be:
213
+
214
+ - single path: routes point to `/`
215
+ - array of paths: all routes in the paths point to `/`
216
+ - array of `PageOptions`, Check below πŸ‘‡
217
+
218
+ ```ts
219
+ interface PageOptions {
220
+ /**
221
+ * Page base directory.
222
+ * @default 'src/pages'
223
+ */
224
+ dir: string
225
+ /**
226
+ * Page base route.
227
+ */
228
+ baseRoute: string
229
+ /**
230
+ * Page file pattern.
231
+ * @example `**\/*.page.vue`
232
+ */
233
+ filePattern?: string
234
+ }
235
+ ```
236
+
237
+ Specifying a glob or an array of `PageOptions` allow you to use multiple
238
+ pages folder, and specify the base route to append to the path and the route
239
+ name.
240
+
241
+ Additionally, you can specify a `filePattern` to filter the files that will be used as pages.
242
+
243
+ #### Example
244
+
245
+ Folder structure
246
+
247
+ ```bash
248
+ src/
249
+ β”œβ”€β”€ features/
250
+ β”‚ └── dashboard/
251
+ β”‚ β”œβ”€β”€ code/
252
+ β”‚ β”œβ”€β”€ components/
253
+ β”‚ └── pages/
254
+ β”œβ”€β”€ admin/
255
+ β”‚ β”œβ”€β”€ code/
256
+ β”‚ β”œβ”€β”€ components/
257
+ β”‚ └── pages/
258
+ └── pages/
259
+ ```
260
+
261
+ Config
262
+
263
+ ```js
264
+ // vite.config.js
265
+ export default {
266
+ plugins: [
267
+ Pages({
268
+ dirs: [
269
+ // basic
270
+ { dir: 'src/pages', baseRoute: '' },
271
+ // features dir for pages
272
+ { dir: 'src/features/**/pages', baseRoute: 'features' },
273
+ // with custom file pattern
274
+ { dir: 'src/admin/pages', baseRoute: 'admin', filePattern: '**/*.page.*' },
275
+ ],
276
+ }),
277
+ ],
278
+ }
279
+ ```
280
+
281
+ ### extensions
282
+
283
+ - **Type:** `string[]`
284
+ - **Default:**
285
+ - Vue: `['vue', 'ts', 'js']`
286
+ - React: `['tsx', 'jsx', 'ts', 'js']`
287
+ - Solid: `['tsx', 'jsx', 'ts', 'js']`
288
+
289
+ An array of valid file extensions for pages. If multiple extensions match for a file, the first one is used.
290
+
291
+ ### exclude
292
+
293
+ - **Type:** `string[]`
294
+ - **Default:** `[]`
295
+
296
+ An array of glob patterns to exclude matches.
297
+
298
+ ```bash
299
+ # folder structure
300
+ src/pages/
301
+ β”œβ”€β”€ users/
302
+ β”‚ β”œβ”€β”€ components
303
+ β”‚ β”‚ └── form.vue
304
+ β”‚ β”œβ”€β”€ [id].vue
305
+ β”‚ └── index.vue
306
+ └── home.vue
307
+ ```
308
+
309
+ ```js
310
+ // vite.config.js
311
+ export default {
312
+ plugins: [
313
+ Pages({
314
+ exclude: ['**/components/*.vue'],
315
+ }),
316
+ ],
317
+ }
318
+ ```
319
+
320
+ ### importMode
321
+
322
+ - **Type:** `'sync' | 'async' | (filepath: string, pluginOptions: ResolvedOptions) => 'sync' | 'async')`
323
+ - **Default:**
324
+ - Top level index file: `'sync'`, others: `async`.
325
+
326
+ Import mode can be set to either `async`, `sync`, or a function which returns
327
+ one of those values.
328
+
329
+ To get more fine-grained control over which routes are loaded sync/async, you
330
+ can use a function to resolve the value based on the route path. For example:
331
+
332
+ ```js
333
+ // vite.config.js
334
+ export default {
335
+ plugins: [
336
+ Pages({
337
+ importMode(filepath, options) {
338
+ // default resolver
339
+ // for (const page of options.dirs) {
340
+ // if (page.baseRoute === '' && filepath.startsWith(`/${page.dir}/index`))
341
+ // return 'sync'
342
+ // }
343
+ // return 'async'
344
+
345
+ // Load about page synchronously, all other pages are async.
346
+ return filepath.includes('about') ? 'sync' : 'async'
347
+ },
348
+ }),
349
+ ],
350
+ }
351
+ ```
352
+
353
+ If you are using `async` mode with `react-router`, you will need to wrap your route components with `Suspense`:
354
+
355
+ ```jsx
356
+ function App() {
357
+ return (
358
+ <Suspense fallback={<p>Loading...</p>}>
359
+ {useRoutes(routes)}
360
+ </Suspense>
361
+ )
362
+ }
363
+ ```
364
+
365
+ ### importPath
366
+
367
+ - **Type:** `'absolute' | 'relative'`
368
+ - **Default:** `'relative'`
369
+
370
+ Import page components from absolute or relative paths. The default behavior is to import from relative paths, but in some special cases, it can be set to `'absolute'` to import from absolute paths.
371
+
372
+ For example, if your page components are located in the `app/pages` directory and you have set `base: /app/` in your `vite.config.js`, you should set `importPath` to `'absolute'` in order to correctly import the page components.
373
+
374
+ ```js
375
+ // vite.config.js
376
+ export default {
377
+ base: '/app/',
378
+ plugins: [
379
+ Pages({
380
+ dirs: 'app/pages',
381
+
382
+ // It should be set to 'absolute' in this case.
383
+ importPath: 'absolute',
384
+ }),
385
+ ],
386
+ }
387
+ ```
388
+
389
+ See [#492](https://github.com/hannoeru/vite-plugin-pages/issues/492) for more details.
390
+
391
+ ### routeBlockLang
392
+
393
+ - **Type:** `string`
394
+ - **Default:** `'json5'`
395
+
396
+ Default SFC route block parser.
397
+
398
+ ### routeStyle
399
+
400
+ - **Type:** `'next' | 'nuxt' | 'remix'`
401
+ - **Default:** `next`
402
+
403
+ Use file system dynamic routing supporting:
404
+
405
+ - [Nextjs Routing](https://nextjs.org/docs/routing/introduction)
406
+ - [Nuxtjs Routing](https://nuxtjs.org/docs/2.x/features/file-system-routing)
407
+ - [Remix Routing](https://remix.run/docs/en/v1/guides/routing)
408
+
409
+ ### routeNameSeparator
410
+
411
+ - **Type:** `string`
412
+ - **Default:** `-`
413
+
414
+ Separator for generated route names.
415
+
416
+ ### resolver
417
+
418
+ - **Type:** `'vue' | 'react' | 'solid' | PageResolver`
419
+ - **Default:** `'auto detect'`
420
+
421
+ Route resolver, support `vue`, `react`, `solid` or custom `PageResolver`.
422
+
423
+ ### moduleId
424
+
425
+ - **Type:** `string`
426
+ - **Default:**
427
+ - Vue: `'~pages'`
428
+ - React: `'~react-pages'`
429
+ - Solid: `'~solid-pages'`
430
+
431
+ Module id for routes import, useful when you what to use multiple pages plugin in one project.
432
+
433
+ ### extendRoute
434
+
435
+ - **Type:**
436
+ `(route: any, parent: any | undefined) => any | void`
437
+
438
+ A function that takes a route and optionally returns a modified route. This is
439
+ useful for augmenting your routes with extra data (e.g. route metadata).
440
+
441
+ ```js
442
+ // vite.config.js
443
+ export default {
444
+ // ...
445
+ plugins: [
446
+ Pages({
447
+ extendRoute(route, parent) {
448
+ if (route.path === '/') {
449
+ // Index is unauthenticated.
450
+ return route
451
+ }
452
+
453
+ // Augment the route with meta that indicates that the route requires authentication.
454
+ return {
455
+ ...route,
456
+ meta: { auth: true },
457
+ }
458
+ },
459
+ }),
460
+ ],
461
+ }
462
+ ```
463
+
464
+ ### onRoutesGenerated
465
+
466
+ - **Type:** `(routes: any[]) => Awaitable<any[] | void>`
467
+
468
+ A function that takes a generated routes and optionally returns a modified
469
+ generated routes.
470
+
471
+ ### onClientGenerated
472
+
473
+ - **Type:** `(clientCode: string) => Awaitable<string | void>`
474
+
475
+ A function that takes a generated client code and optionally returns a modified
476
+ generated client code.
477
+
478
+ ### SFC custom block for Route Data
479
+
480
+ Add route meta to the route by adding a `<route>` block to the SFC. This will be
481
+ directly added to the route after it is generated, and will override it.
482
+
483
+ You can specific a parser to use using `<route lang="yaml">`, or set a default
484
+ parser using `routeBlockLang` option.
485
+
486
+ - **Supported parser:** JSON, JSON5, YAML
487
+ - **Default:** JSON5
488
+
489
+ JSON/JSON5:
490
+
491
+ ```html
492
+ <route>
493
+ {
494
+ name: "name-override",
495
+ meta: {
496
+ requiresAuth: false
497
+ }
498
+ }
499
+ </route>
500
+ ```
501
+
502
+ YAML:
503
+
504
+ ```html
505
+ <route lang="yaml">
506
+ name: name-override
507
+ meta:
508
+ requiresAuth: true
509
+ </route>
510
+ ```
511
+
512
+ #### Syntax Highlighting `<route>`
513
+
514
+ To enable syntax highlighting `<route>` in VS Code using [Vetur's Custom Code Blocks](https://vuejs.github.io/vetur/highlighting.html#custom-block) add the following snippet to your preferences...
515
+
516
+ 1. update setting
517
+
518
+ ```
519
+ "vetur.grammar.customBlocks": {
520
+ "route": "json"
521
+ }
522
+ ```
523
+
524
+ 2. Run the command in vscode
525
+
526
+ `Vetur: Generate grammar from vetur.grammar.customBlocks`
527
+
528
+ 3. Restart VS Code to get syntax highlighting for custom blocks.
529
+
530
+ ### JSX/TSX YAML format comments for Route Data(In Vue)
531
+
532
+ Add route meta to the route by adding a comment block starts with `route` to the JSX or TSX file(In Vue). This will be directly added to the route after it is generated, and will override it.
533
+
534
+ This feature only support JSX/TSX in vue, and will parse only the first block of comments which should also start with `route`.
535
+
536
+ Now only `yaml` parser supported.
537
+
538
+ - **Type:** `'vue'`
539
+ - **Supported parser:** YAML
540
+
541
+ ```jsx
542
+ /*
543
+ route
544
+
545
+ name: name-override
546
+ meta:
547
+ requiresAuth: false
548
+ id: 1234
549
+ string: "1234"
550
+ */
551
+ ```
552
+
553
+ ## File System Routing
554
+
555
+ Inspired by the routing from
556
+ [NuxtJS](https://nuxtjs.org/guides/features/file-system-routing) πŸ’š
557
+
558
+ Pages automatically generates an array of routes for you to plug-in to your
559
+ instance of Vue Router. These routes are determined by the structure of the
560
+ files in your pages directory. Simply create `.vue` files in your pages
561
+ directory and routes will automatically be created for you, no additional
562
+ configuration required!
563
+
564
+ For more advanced use cases, you can tailor Pages to fit the needs of your app
565
+ through [configuration](#configuration).
566
+
567
+ - [Basic Routing](#basic-routing)
568
+ - [Index Routes](#index-routes)
569
+ - [Dynamic Routes](#dynamic-routes)
570
+ - [Nested Routes](#nested-routes)
571
+ - [Catch-all Routes](#catch-all-routes)
572
+
573
+ ### Basic Routing
574
+
575
+ Pages will automatically map files from your pages directory to a route with the
576
+ same name:
577
+
578
+ - `src/pages/users.vue` -> `/users`
579
+ - `src/pages/users/profile.vue` -> `/users/profile`
580
+ - `src/pages/settings.vue` -> `/settings`
581
+
582
+ ### Index Routes
583
+
584
+ Files with the name `index` are treated as the index page of a route:
585
+
586
+ - `src/pages/index.vue` -> `/`
587
+ - `src/pages/users/index.vue` -> `/users`
588
+
589
+ ### Dynamic Routes
590
+
591
+ Dynamic routes are denoted using square brackets. Both directories and pages can
592
+ be dynamic:
593
+
594
+ - `src/pages/users/[id].vue` -> `/users/:id` (`/users/one`)
595
+ - `src/pages/[user]/settings.vue` -> `/:user/settings` (`/one/settings`)
596
+
597
+ Any dynamic parameters will be passed to the page as props. For example, given
598
+ the file `src/pages/users/[id].vue`, the route `/users/abc` will be passed the
599
+ following props:
600
+
601
+ ```json
602
+ { "id": "abc" }
603
+ ```
604
+
605
+ ### Nested Routes
606
+
607
+ We can make use of Vue Routers child routes to create nested layouts. The parent
608
+ component can be defined by giving it the same name as the directory that
609
+ contains your child routes.
610
+
611
+ For example, this directory structure:
612
+
613
+ ```
614
+ src/pages/
615
+ β”œβ”€β”€ users/
616
+ β”‚ β”œβ”€β”€ [id].vue
617
+ β”‚ └── index.vue
618
+ └── users.vue
619
+ ```
620
+
621
+ will result in this routes configuration:
622
+
623
+ ```json5
624
+ [
625
+ {
626
+ "path": "/users",
627
+ "component": "/src/pages/users.vue",
628
+ "children": [
629
+ {
630
+ "path": "",
631
+ "component": "/src/pages/users/index.vue",
632
+ "name": "users"
633
+ },
634
+ {
635
+ "path": ":id",
636
+ "component": "/src/pages/users/[id].vue",
637
+ "name": "users-id"
638
+ }
639
+ ]
640
+ }
641
+ ]
642
+ ```
643
+
644
+ ### Catch-all Routes
645
+
646
+ Catch-all routes are denoted with square brackets containing an ellipsis:
647
+
648
+ - `src/pages/[...all].vue` -> `/*` (`/non-existent-page`)
649
+
650
+ The text after the ellipsis will be used both to name the route, and as the name
651
+ of the prop in which the route parameters are passed.
652
+
653
+ ## Sitemap generation
654
+
655
+ If you need to generate a sitemap from generated routes, you can use [vite-plugin-pages-sitemap](https://github.com/jbaubree/vite-plugin-pages-sitemap).
656
+ This plugin allow you to automatically generate sitemap.xml and robots.xml files with customization.
657
+
658
+ ## License
659
+
660
+ MIT License Β© 2021-PRESENT [hannoeru](https://github.com/hannoeru)
@@ -0,0 +1,13 @@
1
+ declare module '~react-pages' {
2
+ import type { RouteObject } from 'react-router'
3
+
4
+ const routes: RouteObject[]
5
+ export default routes
6
+ }
7
+
8
+ declare module 'virtual:generated-pages-react' {
9
+ import type { RouteObject } from 'react-router'
10
+
11
+ const routes: RouteObject[]
12
+ export default routes
13
+ }
@@ -0,0 +1,6 @@
1
+ declare module '~solid-pages' {
2
+ import type { RouteDefinition } from '@solidjs/router'
3
+
4
+ const routes: RouteDefinition[]
5
+ export default routes
6
+ }
package/client.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ declare module '~pages' {
2
+ import type { RouteRecordRaw } from 'vue-router'
3
+
4
+ const routes: RouteRecordRaw[]
5
+ export default routes
6
+ }
7
+
8
+ declare module 'pages-generated' {
9
+ import type { RouteRecordRaw } from 'vue-router'
10
+
11
+ const routes: RouteRecordRaw[]
12
+ export default routes
13
+ }
14
+
15
+ declare module 'virtual:generated-pages' {
16
+ import type { RouteRecordRaw } from 'vue-router'
17
+
18
+ const routes: RouteRecordRaw[]
19
+ export default routes
20
+ }