From 28de8c192ae6f88da8f7dfa5c42acae3a2e202d1 Mon Sep 17 00:00:00 2001 From: reco_luan Date: Mon, 20 May 2024 04:08:28 +0800 Subject: [PATCH] WIP: optimize local kit debugging --- .../@revili/shared/src/client/composables/index.ts | 1 - .../@revili/shared/src/client/composables/instance.ts | 9 --------- .../@revili/shared/src/node/composables/socket.ts | 4 +++- packages/core/src/node/cli.ts | 11 +++++++++++ packages/core/src/node/command/createStartCommand.ts | 4 ++-- packages/core/src/node/viteServer/index.ts | 4 ++-- .../core/src/node/viteServer/resolveViteConfig.ts | 6 +++--- packages/core/src/types/node.d.ts | 7 ------- 8 files changed, 21 insertions(+), 25 deletions(-) delete mode 100644 packages/@revili/shared/src/client/composables/instance.ts delete mode 100644 packages/core/src/types/node.d.ts diff --git a/packages/@revili/shared/src/client/composables/index.ts b/packages/@revili/shared/src/client/composables/index.ts index 7ea5566..cd789ac 100644 --- a/packages/@revili/shared/src/client/composables/index.ts +++ b/packages/@revili/shared/src/client/composables/index.ts @@ -1,2 +1 @@ -export * from './instance.js' export * from './socket.js' diff --git a/packages/@revili/shared/src/client/composables/instance.ts b/packages/@revili/shared/src/client/composables/instance.ts deleted file mode 100644 index 19e3788..0000000 --- a/packages/@revili/shared/src/client/composables/instance.ts +++ /dev/null @@ -1,9 +0,0 @@ -import {getCurrentInstance} from 'vue' - -export function useInstance() { - const instance = getCurrentInstance() - if (!instance) { - throw new Error('useInstance must use in setup!') - } - return instance -} diff --git a/packages/@revili/shared/src/node/composables/socket.ts b/packages/@revili/shared/src/node/composables/socket.ts index 6994282..e8aaa8e 100644 --- a/packages/@revili/shared/src/node/composables/socket.ts +++ b/packages/@revili/shared/src/node/composables/socket.ts @@ -1,4 +1,6 @@ -export function useServerSocket(server: any) { +import type { ViteDevServer, WebSocketServer } from 'vite' + +export function useServerSocket(server: ViteDevServer): WebSocketServer | null { let socket = null; if (server?.ws) { diff --git a/packages/core/src/node/cli.ts b/packages/core/src/node/cli.ts index e1978a3..b05425d 100644 --- a/packages/core/src/node/cli.ts +++ b/packages/core/src/node/cli.ts @@ -12,6 +12,17 @@ import { createInitKitCommand } from './command/index.js' +/** + * todo : + * 1. 通过 process.argv 判断用户是否增加了参数 --dir + * 2. 如果带了参数,说明是本地套件,直接通过 dir 值来获取套件实例 + * 3. 套件实例传递给 createKitCommands、createStartCommand + * 4. 增加一个 getCurrentKit 方法,判断获取本地 kit 或 cache kit + */ + +const args = process.argv.slice(2); +console.log(args); + // import {getMergedConfig} from './config/getMergedConfig.js' ;(async () => { diff --git a/packages/core/src/node/command/createStartCommand.ts b/packages/core/src/node/command/createStartCommand.ts index 4093457..079114b 100644 --- a/packages/core/src/node/command/createStartCommand.ts +++ b/packages/core/src/node/command/createStartCommand.ts @@ -6,7 +6,7 @@ export function createStartCommand(program: CAC) { .command('start', 'Starting the service') .option('--dev', 'Open develop mode') .option('--dir ', 'Specify the entry file path for kit') - .action(async ({ dev: devMode = false, dir: customKitDir }) => { - await createViteServer({ devMode, customKitDir }) + .action(async ({ dir }) => { + await createViteServer(dir) }) } diff --git a/packages/core/src/node/viteServer/index.ts b/packages/core/src/node/viteServer/index.ts index 21e079a..66e7936 100644 --- a/packages/core/src/node/viteServer/index.ts +++ b/packages/core/src/node/viteServer/index.ts @@ -1,8 +1,8 @@ import {createServer} from 'vite' import { resolveViteConfig } from './resolveViteConfig.js' -export async function createViteServer({ devMode, customKitDir }: { devMode: boolean, customKitDir: string }) { - const viteConfig = await resolveViteConfig({ devMode, customKitDir }) +export async function createViteServer(customKitDir: string) { + const viteConfig = await resolveViteConfig(customKitDir) const server = await createServer(viteConfig) await server.listen() diff --git a/packages/core/src/node/viteServer/resolveViteConfig.ts b/packages/core/src/node/viteServer/resolveViteConfig.ts index 856e225..1a77247 100644 --- a/packages/core/src/node/viteServer/resolveViteConfig.ts +++ b/packages/core/src/node/viteServer/resolveViteConfig.ts @@ -21,17 +21,17 @@ import {virtualModulePlugin} from './plugins/vitePluginVirtualModule.js' import type { Kit } from '@revili/shared/node' -export async function resolveViteConfig({ devMode, customKitDir }: { devMode: boolean, customKitDir: string }): Promise { +export async function resolveViteConfig(customKitDir: string): Promise { const { activeKit: activeKitName } = await getReviliCache(); // const NODE_MODULES_PATH_OF_KIT = path.join(CACHE_FOLDER_PATH, `./node_modules`); const ACTIVE_KIT_DIR = path.join(CACHE_FOLDER_PATH, `./node_modules/${activeKitName}`); const CLIENT_DIR = customKitDir ? path.join(CWD, `${customKitDir}/client`) - : path.join(devMode ? CWD : ACTIVE_KIT_DIR, `./dist/client`); + : path.join(ACTIVE_KIT_DIR, `./dist/client`); const ACTIVE_KIT_ENTRY = customKitDir ? path.join(CWD, `${customKitDir}/node/index.js`) - : path.join(devMode ? CWD : ACTIVE_KIT_DIR, `./dist/node/index.js`); + : path.join(ACTIVE_KIT_DIR, `./dist/node/index.js`); const activeKit = (await import(pathToFileURL(ACTIVE_KIT_ENTRY) as unknown as string)).default as Kit; diff --git a/packages/core/src/types/node.d.ts b/packages/core/src/types/node.d.ts deleted file mode 100644 index 3108477..0000000 --- a/packages/core/src/types/node.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -interface ImportMeta { - hot: { - on: (event: string, handler: (data: any) => void) => void; - glob: (value: string) => any; - globEager: (value: string) => any; - }; -}