From 15f217f54cab637ae1cf646c1c7cef0ffb9529cd Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 17 Jun 2022 20:20:13 +0200 Subject: [PATCH] feat: setup chokidar watcher --- package.json | 1 + playground/index.html | 24 ++++--- playground/src/routes/[...path].vue | 6 ++ playground/src/routes/[name].vue | 5 ++ playground/src/routes/about.vue | 5 ++ .../src/routes/deep/nesting/works/too.vue | 0 playground/src/routes/haha/oeuoe.vue | 0 playground/src/routes/index.vue | 5 ++ playground/src/routes/partial-[name].vue | 5 ++ playground/src/routes/users/[id].vue | 0 playground/src/routes/users/edit.vue | 0 playground/src/routes/users/index.vue | 0 playground/vite.config.ts | 5 +- pnpm-lock.yaml | 2 + src/core/context.ts | 66 +++++++++++++++++++ src/core/utils.ts | 1 + src/index.ts | 48 +++++++++++--- 17 files changed, 150 insertions(+), 23 deletions(-) create mode 100644 playground/src/routes/[...path].vue create mode 100644 playground/src/routes/[name].vue create mode 100644 playground/src/routes/about.vue create mode 100644 playground/src/routes/deep/nesting/works/too.vue create mode 100644 playground/src/routes/haha/oeuoe.vue create mode 100644 playground/src/routes/index.vue create mode 100644 playground/src/routes/partial-[name].vue create mode 100644 playground/src/routes/users/[id].vue create mode 100644 playground/src/routes/users/edit.vue create mode 100644 playground/src/routes/users/index.vue create mode 100644 src/core/context.ts create mode 100644 src/core/utils.ts diff --git a/package.json b/package.json index 3be364ded..b1344867c 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "test": "vitest" }, "dependencies": { + "chokidar": "^3.5.3", "unplugin": "^0.7.0" }, "devDependencies": { diff --git a/playground/index.html b/playground/index.html index 14c818dcd..dd11c5223 100644 --- a/playground/index.html +++ b/playground/index.html @@ -1,13 +1,17 @@ - - - - - - -
- visit /__inspect/ to inspect the intermediate state - - + + + + + +
+ visit /__inspect/ to inspect the intermediate state + + diff --git a/playground/src/routes/[...path].vue b/playground/src/routes/[...path].vue new file mode 100644 index 000000000..aeeaa2a73 --- /dev/null +++ b/playground/src/routes/[...path].vue @@ -0,0 +1,6 @@ + diff --git a/playground/src/routes/[name].vue b/playground/src/routes/[name].vue new file mode 100644 index 000000000..0ef70606a --- /dev/null +++ b/playground/src/routes/[name].vue @@ -0,0 +1,5 @@ + diff --git a/playground/src/routes/about.vue b/playground/src/routes/about.vue new file mode 100644 index 000000000..fc23e04ab --- /dev/null +++ b/playground/src/routes/about.vue @@ -0,0 +1,5 @@ + diff --git a/playground/src/routes/deep/nesting/works/too.vue b/playground/src/routes/deep/nesting/works/too.vue new file mode 100644 index 000000000..e69de29bb diff --git a/playground/src/routes/haha/oeuoe.vue b/playground/src/routes/haha/oeuoe.vue new file mode 100644 index 000000000..e69de29bb diff --git a/playground/src/routes/index.vue b/playground/src/routes/index.vue new file mode 100644 index 000000000..4bc46bec4 --- /dev/null +++ b/playground/src/routes/index.vue @@ -0,0 +1,5 @@ + diff --git a/playground/src/routes/partial-[name].vue b/playground/src/routes/partial-[name].vue new file mode 100644 index 000000000..0cc7a00f0 --- /dev/null +++ b/playground/src/routes/partial-[name].vue @@ -0,0 +1,5 @@ + diff --git a/playground/src/routes/users/[id].vue b/playground/src/routes/users/[id].vue new file mode 100644 index 000000000..e69de29bb diff --git a/playground/src/routes/users/edit.vue b/playground/src/routes/users/edit.vue new file mode 100644 index 000000000..e69de29bb diff --git a/playground/src/routes/users/index.vue b/playground/src/routes/users/index.vue new file mode 100644 index 000000000..e69de29bb diff --git a/playground/vite.config.ts b/playground/vite.config.ts index 6abfbe640..b7b3cd5c9 100644 --- a/playground/vite.config.ts +++ b/playground/vite.config.ts @@ -3,8 +3,5 @@ import Inspect from 'vite-plugin-inspect' import Unplugin from '../src/vite' export default defineConfig({ - plugins: [ - Inspect(), - Unplugin(), - ], + plugins: [Inspect(), Unplugin()], }) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a99fe7162..fc4c14b3b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6,6 +6,7 @@ importers: specifiers: '@types/node': ^17.0.34 bumpp: ^8.1.0 + chokidar: ^3.5.3 esno: ^0.16.3 fast-glob: ^3.2.11 nodemon: ^2.0.16 @@ -19,6 +20,7 @@ importers: vitest: ^0.15.1 webpack: ^5.72.1 dependencies: + chokidar: 3.5.3 unplugin: 0.7.0_hkbj3q5x4yf6mrrlsspksagami devDependencies: '@types/node': 17.0.44 diff --git a/src/core/context.ts b/src/core/context.ts new file mode 100644 index 000000000..f7c39d50f --- /dev/null +++ b/src/core/context.ts @@ -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 = { + 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 = { ...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, + } +} diff --git a/src/core/utils.ts b/src/core/utils.ts new file mode 100644 index 000000000..0a49928eb --- /dev/null +++ b/src/core/utils.ts @@ -0,0 +1 @@ +export type Awaitable = T | PromiseLike diff --git a/src/index.ts b/src/index.ts index d5f02c47f..3fd32ab33 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) => ({ - 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) => { + 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'}` + ) + }, + } +})