Skip to content

Commit

Permalink
feat: setup chokidar watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jun 17, 2022
1 parent 2295ff5 commit 15f217f
Show file tree
Hide file tree
Showing 17 changed files with 150 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"test": "vitest"
},
"dependencies": {
"chokidar": "^3.5.3",
"unplugin": "^0.7.0"
},
"devDependencies": {
Expand Down
24 changes: 14 additions & 10 deletions playground/index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="app"></div>
<a href="/__inspect/module?id=./main.ts&index=2" target="_blank" style="text-decoration: none; margin-top:10px; display: block;">visit /__inspect/ to inspect the intermediate state</a>
<script type="module" src="./main.ts"></script>
</body>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<div id="app"></div>
<a
href="/__inspect/module?id=./main.ts&index=2"
target="_blank"
style="text-decoration: none; margin-top: 10px; display: block"
>visit /__inspect/ to inspect the intermediate state</a
>
<script type="module" src="./main.ts"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions playground/src/routes/[...path].vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<template>
<main>
<h1>Not Found</h1>
<p>{{ $route.params.path }} does not exist.</p>
</main>
</template>
5 changes: 5 additions & 0 deletions playground/src/routes/[name].vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<main>
<h1>Param: {{ $route.params.name }}</h1>
</main>
</template>
5 changes: 5 additions & 0 deletions playground/src/routes/about.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<main>
<h1>About</h1>
</main>
</template>
Empty file.
Empty file.
5 changes: 5 additions & 0 deletions playground/src/routes/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<main>
<h1>Home</h1>
</main>
</template>
5 changes: 5 additions & 0 deletions playground/src/routes/partial-[name].vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<main>
<h1>Partial Param: {{ $route.params.name }}</h1>
</main>
</template>
Empty file.
Empty file.
Empty file.
5 changes: 1 addition & 4 deletions playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@ import Inspect from 'vite-plugin-inspect'
import Unplugin from '../src/vite'

export default defineConfig({
plugins: [
Inspect(),
Unplugin(),
],
plugins: [Inspect(), Unplugin()],
})
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions src/core/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { FSWatcher, watch as chokidar } from 'chokidar'
import { resolve } from 'path'

export interface RoutesContextOptions {
extensions?: string[]
routesFolder?: string
importMode?:
| 'sync'
| 'async'
| ((path: string, resolvedOptions: any) => 'sync' | 'async')

ignore?: string[]

routeStyle?: 'nuxt' | 'remix'

routesModuleId?: string

root?: string
}

const DEFAULT_OPTIONS: Required<RoutesContextOptions> = {
extensions: ['.vue', '.js', '.jsx', '.ts', '.tsx'],
ignore: [],
routesFolder: 'src/routes',
importMode: 'async',
routeStyle: 'nuxt',
routesModuleId: '@routes',
root: process.cwd(),
}

export function createRoutesContext(opt?: RoutesContextOptions) {
const options: Required<RoutesContextOptions> = { ...DEFAULT_OPTIONS, ...opt }
let serverWatcher: FSWatcher | null = null

function setupWatcher(watcher?: FSWatcher) {
// already setup
if (serverWatcher) return

// create the watcher if necessary
// console.log({ root: options.root, routesFolder: options.routesFolder })
if (!watcher) {
watcher = chokidar(options.routesFolder, {
// TODO: allow user options
cwd: options.root,
})
}

serverWatcher = watcher

watcher
.on('change', (path) => {
// TODO: parse defineRoute macro?
console.log('change', path)
})
.on('add', (path) => {
console.log('add', path)
})
.on('unlink', (path) => {
console.log('remove', path)
})
}

return {
setupWatcher,
}
}
1 change: 1 addition & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Awaitable<T> = T | PromiseLike<T>
48 changes: 39 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
import { resolve } from 'path'
import { createUnplugin } from 'unplugin'
import { createRoutesContext } from './core/context'
import type { Options } from './types'
import { watch } from 'chokidar'

export default createUnplugin<Options>((options) => ({
name: 'unplugin-vue-router',
transformInclude(id) {
return id.endsWith('main.ts')
},
transform(code) {
return code.replace('__UNPLUGIN__', `Hello Unplugin! ${options}`)
},
}))
const root = process.cwd()
export default createUnplugin<Options>((options) => {
const ctx = createRoutesContext(options)

return {
name: 'unplugin-vue-router',
enforce: 'pre',

// should we transform the file id?
transformInclude(id) {
// console.log(id)
return id.endsWith('main.ts')
},

buildStart() {
ctx.setupWatcher()
},

watchChange(id, change) {
console.log('we watch', { id, change })
process.exit(0)
},

// load(id) {
// console.log('load', { id })
// return null
// },

transform(code) {
return code.replace(
'__UNPLUGIN__',
`Hello Unplugin! ${options || 'no options'}`
)
},
}
})

0 comments on commit 15f217f

Please sign in to comment.