@hono-filebased-route/vite-plugin 0.1.0 → 0.3.1

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 CHANGED
@@ -1,32 +1,56 @@
1
1
  import type { Plugin } from 'vite'
2
2
  import { createPluginName } from './shared/create'
3
- import { generateRoutesFile } from '@hono-filebased-route/core'
3
+ import { generateRoutesFile, createLogger } from '@hono-filebased-route/core'
4
+ import path from 'path'
5
+ import { writeFile, readFile } from 'fs/promises'
4
6
 
5
7
  interface Options {
6
- routesDir: string
8
+ dir: string
9
+ output: string
10
+ verbose: boolean
7
11
  virtualRoute: boolean
8
- outputFile: string
9
- callback: (router: string) => void
12
+ callback?: (router: string) => void
10
13
  }
11
14
 
12
15
  const useName = createPluginName(true)
16
+ const virtualFileId = 'virtual:generated-routes'
17
+
18
+ const newFileContent = `
19
+ import { Context } from 'hono'
20
+
21
+ export function GET(c: Context) {
22
+ return c.text('')
23
+ }
24
+ `
13
25
 
14
26
  const usePlugin = (options?: Partial<Options>): Plugin => {
15
27
  const {
16
- routesDir = './src/routes',
28
+ dir = './src/routes',
17
29
  virtualRoute = true,
18
- outputFile = './src/generated-routes.ts',
19
- callback
30
+ output = './src/generated-routes.ts',
31
+ verbose = false,
32
+ callback,
20
33
  } = options || {}
21
- const virtualFileId = 'generated-routes'
22
34
  let generated_route: string = ''
35
+ const logger = createLogger(verbose)
23
36
 
24
37
  const generateRoutes = async () => {
25
38
  if (virtualRoute) {
26
- const router = await generateRoutesFile(routesDir, '', false)
39
+ const router = await generateRoutesFile({
40
+ dir,
41
+ output: '',
42
+ write: false,
43
+ verbose,
44
+ typescript: false,
45
+ })
27
46
  generated_route = router
28
47
  } else {
29
- generateRoutesFile(routesDir, outputFile)
48
+ generateRoutesFile({
49
+ dir,
50
+ output,
51
+ write: true,
52
+ verbose,
53
+ })
30
54
  }
31
55
  callback?.(generated_route)
32
56
  }
@@ -39,6 +63,12 @@ const usePlugin = (options?: Partial<Options>): Plugin => {
39
63
  await generateRoutes()
40
64
 
41
65
  server.watcher.on('all', async (event, file) => {
66
+ if (!file.startsWith(path.resolve(dir))) return
67
+ logger.info(`${event}, ${file}`)
68
+ if (event === 'add') {
69
+ const fileContent = (await readFile(file, 'utf-8')).trim()
70
+ if (fileContent === '') return await writeFile(file, newFileContent.trimStart())
71
+ }
42
72
  if (events.includes(event)) {
43
73
  await generateRoutes()
44
74
  server.restart()
@@ -46,20 +76,20 @@ const usePlugin = (options?: Partial<Options>): Plugin => {
46
76
  })
47
77
  },
48
78
  resolveId: virtualRoute
49
- ? (id) => {
50
- if (id === virtualFileId) {
51
- return virtualFileId
79
+ ? id => {
80
+ if (id === virtualFileId) {
81
+ return virtualFileId
82
+ }
52
83
  }
53
- }
54
84
  : undefined,
55
85
  load: virtualRoute
56
- ? (id) => {
57
- if (id === virtualFileId) {
58
- return generated_route
86
+ ? id => {
87
+ if (id === virtualFileId) {
88
+ return generated_route
89
+ }
59
90
  }
60
- }
61
- : undefined
91
+ : undefined,
62
92
  }
63
93
  }
64
94
 
65
- export default usePlugin
95
+ export default usePlugin
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hono-filebased-route/vite-plugin",
3
- "version": "0.1.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "description": "A Vite plugin for file-based routing in Hono applications.",
6
6
  "author": "HM Suiji <hmsuiji@gmail.com>",
@@ -30,11 +30,13 @@
30
30
  "@hono-filebased-route/core": "workspace:*",
31
31
  "chokidar": "^4.0.3",
32
32
  "hono": "^4.9.2",
33
- "path": "^0.12.7"
33
+ "path": "^0.12.7",
34
+ "pino": "^9.9.0"
34
35
  },
35
36
  "devDependencies": {
36
37
  "@types/bun": "latest",
37
38
  "bun-plugin-dts": "^0.3.0",
39
+ "pino-pretty": "^13.1.1",
38
40
  "terser": "^5.43.1",
39
41
  "typescript": "^5.0.0",
40
42
  "vite": "^7.1.3"
@@ -42,6 +44,9 @@
42
44
  "peerDependenciesMeta": {
43
45
  "hono": {
44
46
  "optional": false
47
+ },
48
+ "typescript": {
49
+ "optional": false
45
50
  }
46
51
  },
47
52
  "publishConfig": {
package/shared/create.ts CHANGED
@@ -1,6 +1,4 @@
1
- export const createPluginName = (
2
- reusable: boolean = false
3
- ) => {
1
+ export const createPluginName = (reusable: boolean = false) => {
4
2
  let i = 0
5
3
  return (name: string) => {
6
4
  const base = `vite-plugin-${name}`
@@ -13,6 +11,6 @@ export const createVirtualModuleID = (name: string) => {
13
11
  const resolvedVirtualModuleId = '\0' + virtualModuleId
14
12
  return {
15
13
  virtualModuleId,
16
- resolvedVirtualModuleId
14
+ resolvedVirtualModuleId,
17
15
  }
18
- }
16
+ }