Skip to content

Commit

Permalink
refa: move dev server logic to client
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 27, 2024
1 parent 3819721 commit 3abb736
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import CAC from 'cac'
import { resolve } from 'path'
import { buildExtension } from '.'
import { build } from '.'

const { version } = require('../package.json')

Expand All @@ -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()
Expand Down
59 changes: 54 additions & 5 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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,
Expand Down Expand Up @@ -64,19 +65,67 @@ 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'
const dest = root + '/dist/' + item.fileName
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',
})
await fsp.writeFile(dest, result.code)
}
}
}

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))
}
4 changes: 2 additions & 2 deletions packages/client/src/yakumo.ts
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down Expand Up @@ -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)
}
})
}
8 changes: 7 additions & 1 deletion plugins/console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
54 changes: 5 additions & 49 deletions plugins/console/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 3abb736

Please sign in to comment.