From 3abb736c95280831bf50556e25a6430c311b9695 Mon Sep 17 00:00:00 2001 From: Shigma Date: Sat, 27 Jan 2024 15:04:06 +0800 Subject: [PATCH] refa: move dev server logic to client --- package.json | 2 +- packages/client/src/bin.ts | 4 +-- packages/client/src/index.ts | 59 ++++++++++++++++++++++++++++--- packages/client/src/yakumo.ts | 4 +-- plugins/console/package.json | 8 ++++- plugins/console/src/node/index.ts | 54 +++------------------------- 6 files changed, 71 insertions(+), 60 deletions(-) diff --git a/package.json b/package.json index a43e22a6..86f219d6 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "license": "MIT", "scripts": { "client": "yakumo client", - "build": "tsc -b packages/client && yakumo tsx build", + "build": "tsc -b packages/client && yakumo build", "bump": "yakumo version", "dep": "yakumo upgrade", "pub": "yakumo publish", diff --git a/packages/client/src/bin.ts b/packages/client/src/bin.ts index 0a758036..41099928 100644 --- a/packages/client/src/bin.ts +++ b/packages/client/src/bin.ts @@ -1,6 +1,6 @@ import CAC from 'cac' import { resolve } from 'path' -import { buildExtension } from '.' +import { build } from '.' const { version } = require('../package.json') @@ -9,7 +9,7 @@ const cli = CAC('koishi-console').help().version(version) cli.command('build [root]') .action((root) => { root = resolve(process.cwd(), root || '.') - buildExtension(root) + build(root) }) cli.parse() diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts index c6fccdd5..fd5fb2fd 100644 --- a/packages/client/src/index.ts +++ b/packages/client/src/index.ts @@ -1,12 +1,13 @@ -import { build, InlineConfig, mergeConfig, transformWithEsbuild, UserConfig } from 'vite' +import * as vite from 'vite' import { RollupOutput } from 'rollup' import { existsSync, promises as fsp } from 'fs' import unocss from 'unocss/vite' import mini from 'unocss/preset-mini' import vue from '@vitejs/plugin-vue' import yaml from '@maikolib/vite-plugin-yaml' +import { resolve } from 'path' -export async function buildExtension(root: string, config: UserConfig = {}) { +export async function build(root: string, config: vite.UserConfig = {}) { if (!existsSync(root + '/client')) return const outDir = root + '/dist' @@ -15,7 +16,7 @@ export async function buildExtension(root: string, config: UserConfig = {}) { } await fsp.mkdir(root + '/dist', { recursive: true }) - const results = await build(mergeConfig({ + const results = await vite.build(vite.mergeConfig({ root, build: { write: false, @@ -64,7 +65,7 @@ export async function buildExtension(root: string, config: UserConfig = {}) { define: { 'process.env.NODE_ENV': '"production"', }, - } as InlineConfig, config)) as RollupOutput[] + } as vite.InlineConfig, config)) as RollupOutput[] for (const item of results[0].output) { if (item.fileName === 'index.mjs') item.fileName = 'index.js' @@ -72,7 +73,7 @@ export async function buildExtension(root: string, config: UserConfig = {}) { if (item.type === 'asset') { await fsp.writeFile(dest, item.source) } else { - const result = await transformWithEsbuild(item.code, dest, { + const result = await vite.transformWithEsbuild(item.code, dest, { minifyWhitespace: true, charset: 'utf8', }) @@ -80,3 +81,51 @@ export async function buildExtension(root: string, config: UserConfig = {}) { } } } + +export function createServer(config?: vite.InlineConfig) { + const root = resolve(__dirname, '../app') + return vite.createServer(vite.mergeConfig({ + root, + base: '/vite/', + server: { + middlewareMode: true, + }, + plugins: [ + vue(), + yaml(), + unocss({ + presets: [ + mini({ + preflight: false, + }), + ], + }), + ], + resolve: { + dedupe: ['vue', 'vue-demi', 'vue-router', 'element-plus', '@vueuse/core', '@popperjs/core', 'marked', 'xss'], + alias: { + // for backward compatibility + '../client.js': '@koishijs/client', + '../vue.js': 'vue', + '../vue-router.js': 'vue-router', + '../vueuse.js': '@vueuse/core', + }, + }, + optimizeDeps: { + include: [ + 'vue', + 'vue-router', + 'element-plus', + '@vueuse/core', + '@popperjs/core', + 'marked', + 'xss', + ], + }, + build: { + rollupOptions: { + input: root + '/index.html', + }, + }, + }, config)) +} diff --git a/packages/client/src/yakumo.ts b/packages/client/src/yakumo.ts index 04c19fd3..50b8a5a7 100644 --- a/packages/client/src/yakumo.ts +++ b/packages/client/src/yakumo.ts @@ -1,7 +1,7 @@ import { createRequire } from 'module' import { UserConfig } from 'vite' import { Context } from 'yakumo' -import { buildExtension } from '.' +import { build } from '.' import ns from 'ns-require' declare module 'yakumo' { @@ -35,7 +35,7 @@ export function apply(ctx: Context) { } else if (!deps['@koishijs/client']) { continue } - await buildExtension(ctx.yakumo.cwd + path, config) + await build(ctx.yakumo.cwd + path, config) } }) } diff --git a/plugins/console/package.json b/plugins/console/package.json index e84307d3..543f1efd 100644 --- a/plugins/console/package.json +++ b/plugins/console/package.json @@ -55,7 +55,13 @@ } }, "peerDependencies": { - "koishi": "^4.16.6" + "@koishijs/client": "^5.26.5", + "koishi": "^4.16.7" + }, + "peerDependenciesMeta": { + "@koishijs/client": { + "optional": true + } }, "devDependencies": { "@koishijs/client": "^5.26.0", diff --git a/plugins/console/src/node/index.ts b/plugins/console/src/node/index.ts index 80d6edc8..63cb5330 100644 --- a/plugins/console/src/node/index.ts +++ b/plugins/console/src/node/index.ts @@ -7,7 +7,7 @@ import { createReadStream, existsSync, promises as fsp, Stats } from 'fs' import {} from '@koishijs/plugin-server-proxy' import open from 'open' import { createRequire } from 'module' -import { fileURLToPath } from 'url' +import { fileURLToPath, pathToFileURL } from 'url' declare module 'koishi' { interface EnvData { @@ -57,11 +57,11 @@ class NodeConsole extends Console { }) // @ts-ignore - const require = createRequire(import.meta.url) + const base = import.meta.url || pathToFileURL(__filename).href + const require = createRequire(base) this.root = config.root || (config.devMode ? resolve(require.resolve('@koishijs/client/package.json'), '../app') - // @ts-ignore - : fileURLToPath(new URL('../../dist', import.meta.url))) + : fileURLToPath(new URL('../../dist', base))) } get config() { @@ -201,57 +201,13 @@ class NodeConsole extends Console { private async createVite() { const { cacheDir, dev } = this.config - const { createServer } = await import('vite') - const { default: mini } = await import('unocss/preset-mini') - const { default: unocss } = await import('unocss/vite') - const { default: vue } = await import('@vitejs/plugin-vue') - const { default: yaml } = await import('@maikolib/vite-plugin-yaml') + const { createServer } = await import('@koishijs/client/lib/index.js') this.vite = await createServer({ - root: this.root, - base: '/vite/', cacheDir: resolve(this.ctx.baseDir, cacheDir), server: { - middlewareMode: true, fs: dev.fs, }, - plugins: [ - vue(), - yaml(), - unocss({ - presets: [ - mini({ - preflight: false, - }), - ], - }), - ], - resolve: { - dedupe: ['vue', 'vue-demi', 'vue-router', 'element-plus', '@vueuse/core', '@popperjs/core', 'marked', 'xss'], - alias: { - // for backward compatibility - '../client.js': '@koishijs/client', - '../vue.js': 'vue', - '../vue-router.js': 'vue-router', - '../vueuse.js': '@vueuse/core', - }, - }, - optimizeDeps: { - include: [ - 'vue', - 'vue-router', - 'element-plus', - '@vueuse/core', - '@popperjs/core', - 'marked', - 'xss', - ], - }, - build: { - rollupOptions: { - input: this.root + '/index.html', - }, - }, }) this.ctx.server.all('/vite(/.+)*', (ctx) => new Promise((resolve) => {