Skip to content

Commit

Permalink
feat(console): support esm usage, fix koishijs/koishi#1339
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 26, 2024
1 parent aaf5f24 commit 3819721
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
5 changes: 4 additions & 1 deletion plugins/console/package.json
Expand Up @@ -6,7 +6,10 @@
"types": "lib/index.d.ts",
"exports": {
".": {
"node": "./lib/node/index.js",
"node": {
"require": "./lib/node/index.js",
"import": "./lib/node/index.mjs"
},
"browser": "./lib/browser/index.mjs",
"types": "./lib/index.d.ts"
},
Expand Down
21 changes: 14 additions & 7 deletions plugins/console/src/node/index.ts
Expand Up @@ -6,6 +6,8 @@ import { extname, resolve } from 'path'
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'

declare module 'koishi' {
interface EnvData {
Expand Down Expand Up @@ -54,9 +56,12 @@ class NodeConsole extends Console {
loader.envData.clientCount = this.layer.clients.size
})

// @ts-ignore
const require = createRequire(import.meta.url)
this.root = config.root || (config.devMode
? resolve(require.resolve('@koishijs/client/package.json'), '../app')
: resolve(__dirname, '../../dist'))
// @ts-ignore
: fileURLToPath(new URL('../../dist', import.meta.url)))
}

get config() {
Expand Down Expand Up @@ -84,7 +89,9 @@ class NodeConsole extends Console {
this.serveAssets()

this.ctx.on('server/ready', () => {
const target = this.ctx.server.selfUrl + this.config.uiPath
let { host, port } = this.ctx.server
if (['0.0.0.0', '::'].includes(host)) host = '127.0.0.1'
const target = `http://${host}:${port}${this.config.uiPath}`
if (this.config.open && !this.ctx.get('loader')?.envData.clientCount && !process.env.KOISHI_AGENT) {
open(target)
}
Expand Down Expand Up @@ -194,11 +201,11 @@ class NodeConsole extends Console {

private async createVite() {
const { cacheDir, dev } = this.config
const { createServer } = require('vite') as typeof import('vite')
const { default: mini } = require('unocss/preset-mini') as typeof import('unocss/preset-mini')
const { default: unocss } = require('unocss/vite') as typeof import('unocss/vite')
const { default: vue } = require('@vitejs/plugin-vue') as typeof import('@vitejs/plugin-vue')
const { default: yaml } = require('@maikolib/vite-plugin-yaml') as typeof import('@maikolib/vite-plugin-yaml')
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')

This comment has been minimized.

Copy link
@CyanChanges

CyanChanges Jan 27, 2024

Contributor

好耶,这里改 import 了


this.vite = await createServer({
root: this.root,
Expand Down

0 comments on commit 3819721

Please sign in to comment.