Skip to content

Commit

Permalink
fix(webui): fix import.meta.resolve error, support yarn pnp
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 27, 2024
1 parent d171d81 commit 76f4329
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 27 deletions.
7 changes: 4 additions & 3 deletions plugins/insight/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ export const Config: Schema<Config> = Schema.object({})

export function apply(ctx: Context) {
const entry = ctx.webui.addEntry({
dev: import.meta.resolve('../client/index.ts'),
base: import.meta.url,
dev: '../client/index.ts',
prod: [
import.meta.resolve('../dist/index.js'),
import.meta.resolve('../dist/style.css'),
'../dist/index.js',
'../dist/style.css',
],
}, getGraph)

Expand Down
7 changes: 4 additions & 3 deletions plugins/manager/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ export abstract class Manager extends Service {
start() {
this.ctx.inject(['webui'], (ctx) => {
this.entry = ctx.webui.addEntry({
dev: import.meta.resolve('../client/index.ts'),
base: import.meta.url,
dev: '../client/index.ts',
prod: [
import.meta.resolve('../dist/index.js'),
import.meta.resolve('../dist/style.css'),
'../dist/index.js',
'../dist/style.css',
],
}, () => (this.getPackages(), {
entries: this.getEntries(),
Expand Down
7 changes: 4 additions & 3 deletions plugins/notifier/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ class NotifierService extends Service {
ctx.on('dispose', () => this.entry = undefined)

this.entry = ctx.webui.addEntry({
dev: import.meta.resolve('../client/index.ts'),
base: import.meta.url,
dev: '../client/index.ts',
prod: [
import.meta.resolve('../dist/index.js'),
import.meta.resolve('../dist/style.css'),
'../dist/index.js',
'../dist/style.css',
],
}, () => ({
notifiers: this.store.map(notifier => notifier.toJSON()),
Expand Down
3 changes: 1 addition & 2 deletions plugins/webui/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class BrowserWebUI extends WebUI {
}

resolveEntry(files: Entry.Files) {
if (typeof files === 'string' || Array.isArray(files)) return makeArray(files)
return makeArray(files.prod)
return makeArray(files.prod).map(url => new URL(url, files.base).href)
}
}

Expand Down
27 changes: 14 additions & 13 deletions plugins/webui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FileSystemServeOptions, ViteDevServer } from 'vite'
import { extname, resolve } from 'node:path'
import { createReadStream, existsSync, Stats } from 'node:fs'
import { readFile, stat } from 'node:fs/promises'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { fileURLToPath } from 'node:url'
import { parse } from 'es-module-lexer'
import { Entry, Events, WebUI } from './shared'
import open from 'open'
Expand Down Expand Up @@ -105,19 +105,20 @@ class NodeWebUI extends WebUI<NodeWebUI.Config> {
})
}

private getFiles(files: Entry.Files) {
if (typeof files === 'string' || Array.isArray(files)) return files
if (!this.config.devMode) return files.prod
if (!existsSync(fileURLToPath(files.dev))) return files.prod
return files.dev
private getPaths(files: Entry.Files) {
if (this.config.devMode && files.dev) {
const filename = fileURLToPath(new URL(files.dev, files.base))
if (existsSync(filename)) return [filename]
}
return makeArray(files.prod).map(url => fileURLToPath(new URL(url, files.base)))
}

resolveEntry(files: Entry.Files, key: string) {
return makeArray(this.getFiles(files)).map((url, index) => {
return this.getPaths(files).map((path, index) => {
if (this.config.devMode) {
return `/vite/@fs/${fileURLToPath(url)}`
return `/vite/@fs/${path}`
} else {
return `${this.config.uiPath}/@vendor/${key}/${index}${extname(url)}`
return `${this.config.uiPath}/@vendor/${key}/${index}${extname(path)}`
}
})
}
Expand All @@ -143,11 +144,11 @@ class NodeWebUI extends WebUI<NodeWebUI.Config> {
if (name.startsWith('@vendor/')) {
const [key, value] = name.slice(8).split('/')
if (!this.entries[key]) return ctx.status = 404
const files = makeArray(this.getFiles(this.entries[key].files))
const paths = this.getPaths(this.entries[key].files)
const type = extname(value)
const index = value.slice(0, -type.length)
if (!files[index]) return ctx.status = 404
const filename = fileURLToPath(files[index])
if (!paths[index]) return ctx.status = 404
const filename = paths[index]
ctx.type = type
if (this.config.devMode || ctx.type !== 'application/javascript') {
return sendFile(filename)
Expand Down Expand Up @@ -222,7 +223,7 @@ class NodeWebUI extends WebUI<NodeWebUI.Config> {
name: 'cordis-hmr',
transform: (code, id, options) => {
for (const [key, { files }] of Object.entries(this.entries)) {
const index = makeArray(this.getFiles(files)).indexOf(pathToFileURL(id).href)
const index = this.getPaths(files).indexOf(id)
if (index < 0) continue
code += [
'if (import.meta.hot) {',
Expand Down
5 changes: 2 additions & 3 deletions plugins/webui/src/shared/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { Context } from 'cordis'
import { Client } from './index.ts'

export namespace Entry {
export type Files = string | string[] | EntryOptions

export interface EntryOptions {
export interface Files {
base?: string
dev: string
prod: string | string[]
}
Expand Down

0 comments on commit 76f4329

Please sign in to comment.