@hono-filebased-route/vite-plugin 0.1.0

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/index.ts ADDED
@@ -0,0 +1,65 @@
1
+ import type { Plugin } from 'vite'
2
+ import { createPluginName } from './shared/create'
3
+ import { generateRoutesFile } from '@hono-filebased-route/core'
4
+
5
+ interface Options {
6
+ routesDir: string
7
+ virtualRoute: boolean
8
+ outputFile: string
9
+ callback: (router: string) => void
10
+ }
11
+
12
+ const useName = createPluginName(true)
13
+
14
+ const usePlugin = (options?: Partial<Options>): Plugin => {
15
+ const {
16
+ routesDir = './src/routes',
17
+ virtualRoute = true,
18
+ outputFile = './src/generated-routes.ts',
19
+ callback
20
+ } = options || {}
21
+ const virtualFileId = 'generated-routes'
22
+ let generated_route: string = ''
23
+
24
+ const generateRoutes = async () => {
25
+ if (virtualRoute) {
26
+ const router = await generateRoutesFile(routesDir, '', false)
27
+ generated_route = router
28
+ } else {
29
+ generateRoutesFile(routesDir, outputFile)
30
+ }
31
+ callback?.(generated_route)
32
+ }
33
+
34
+ return {
35
+ name: useName('hono-router'),
36
+ enforce: 'pre',
37
+ async configureServer(server) {
38
+ const events = ['add', 'change', 'unlink']
39
+ await generateRoutes()
40
+
41
+ server.watcher.on('all', async (event, file) => {
42
+ if (events.includes(event)) {
43
+ await generateRoutes()
44
+ server.restart()
45
+ }
46
+ })
47
+ },
48
+ resolveId: virtualRoute
49
+ ? (id) => {
50
+ if (id === virtualFileId) {
51
+ return virtualFileId
52
+ }
53
+ }
54
+ : undefined,
55
+ load: virtualRoute
56
+ ? (id) => {
57
+ if (id === virtualFileId) {
58
+ return generated_route
59
+ }
60
+ }
61
+ : undefined
62
+ }
63
+ }
64
+
65
+ export default usePlugin
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@hono-filebased-route/vite-plugin",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "A Vite plugin for file-based routing in Hono applications.",
6
+ "author": "HM Suiji <hmsuiji@gmail.com>",
7
+ "keywords": [
8
+ "hono",
9
+ "router",
10
+ "file-based",
11
+ "routing",
12
+ "backend",
13
+ "framework",
14
+ "typescript",
15
+ "file-based-route",
16
+ "vite-plugin"
17
+ ],
18
+ "license": "MIT",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/HM-Suiji/hono-filebased-route.git"
22
+ },
23
+ "main": "dist/index.js",
24
+ "types": "dist/index.d.ts",
25
+ "scripts": {
26
+ "build": "bun run build.ts",
27
+ "clean": "rm -rf dist"
28
+ },
29
+ "dependencies": {
30
+ "@hono-filebased-route/core": "workspace:*",
31
+ "chokidar": "^4.0.3",
32
+ "hono": "^4.9.2",
33
+ "path": "^0.12.7"
34
+ },
35
+ "devDependencies": {
36
+ "@types/bun": "latest",
37
+ "bun-plugin-dts": "^0.3.0",
38
+ "terser": "^5.43.1",
39
+ "typescript": "^5.0.0",
40
+ "vite": "^7.1.3"
41
+ },
42
+ "peerDependenciesMeta": {
43
+ "hono": {
44
+ "optional": false
45
+ }
46
+ },
47
+ "publishConfig": {
48
+ "access": "public"
49
+ }
50
+ }
@@ -0,0 +1,18 @@
1
+ export const createPluginName = (
2
+ reusable: boolean = false
3
+ ) => {
4
+ let i = 0
5
+ return (name: string) => {
6
+ const base = `vite-plugin-${name}`
7
+ return reusable ? `${base}:${i++}` : base
8
+ }
9
+ }
10
+
11
+ export const createVirtualModuleID = (name: string) => {
12
+ const virtualModuleId = `virtual:${name}`
13
+ const resolvedVirtualModuleId = '\0' + virtualModuleId
14
+ return {
15
+ virtualModuleId,
16
+ resolvedVirtualModuleId
17
+ }
18
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": ".",
5
+ "outDir": "./dist",
6
+ "composite": true
7
+ },
8
+ "include": [
9
+ "shared/**/*.ts",
10
+ "index.ts"
11
+ ],
12
+ "exclude": [
13
+ "node_modules",
14
+ "dist",
15
+ "**/*.test.ts"
16
+ ],
17
+ "references": [
18
+ {
19
+ "path": "../core"
20
+ }
21
+ ]
22
+ }
@@ -0,0 +1 @@
1
+ {"fileNames":[],"fileInfos":[],"root":[],"options":{"composite":true,"declaration":true,"declarationMap":true,"esModuleInterop":true,"module":99,"outDir":"./dist","rootDir":"./src","skipLibCheck":true,"sourceMap":true,"strict":true,"target":99},"version":"5.8.3"}