diff --git a/packages/vite-plugin-checker/src/@runtime/main.ts b/packages/vite-plugin-checker/src/@runtime/main.ts index b6189ad0..46584a65 100644 --- a/packages/vite-plugin-checker/src/@runtime/main.ts +++ b/packages/vite-plugin-checker/src/@runtime/main.ts @@ -6,7 +6,7 @@ const WS_CHECKER_RECONNECT_EVENT = 'vite-plugin-checker:reconnect' export function inject() { const socketProtocol = null || (location.protocol === 'https:' ? 'wss' : 'ws') - const socketHost = `${null || location.hostname}:${'3000'}` + const socketHost = `${location.hostname}:${location.port}` const socket = new WebSocket(`${socketProtocol}://${socketHost}`, 'vite-hmr') socket.addEventListener('message', async ({ data: dataStr }) => { diff --git a/packages/vite-plugin-checker/src/DiagnosticCache.ts b/packages/vite-plugin-checker/src/DiagnosticCache.ts deleted file mode 100644 index cd641cf6..00000000 --- a/packages/vite-plugin-checker/src/DiagnosticCache.ts +++ /dev/null @@ -1,38 +0,0 @@ -import type { NormalizedDiagnostic } from './logger' - -class FileDiagnosticCache { - public diagnostics: NormalizedDiagnostic[] = [] - - public initWith(diagnostics: NormalizedDiagnostic[]) { - diagnostics.forEach((d) => { - this.diagnostics.push(d) - }) - } - - public getDiagnostics(fileName?: string) { - if (fileName) { - return this.diagnostics.filter((f) => f.id === fileName) - } - - return this.diagnostics - } - - public get lastDiagnostic() { - return this.diagnostics[this.diagnostics.length - 1] - } - - public setFile(fileName: string, next: NormalizedDiagnostic[] | null) { - for (let i = 0; i < this.diagnostics.length; i++) { - if (this.diagnostics[i].id === fileName) { - this.diagnostics.splice(i, 1) - i-- - } - } - - if (next?.length) { - this.diagnostics.push(...next) - } - } -} - -export { FileDiagnosticCache } diff --git a/packages/vite-plugin-checker/src/FileDiagnosticManager.ts b/packages/vite-plugin-checker/src/FileDiagnosticManager.ts index 9b3c33e8..f7003bf0 100644 --- a/packages/vite-plugin-checker/src/FileDiagnosticManager.ts +++ b/packages/vite-plugin-checker/src/FileDiagnosticManager.ts @@ -2,11 +2,21 @@ import type { NormalizedDiagnostic } from './logger' class FileDiagnosticManager { public diagnostics: NormalizedDiagnostic[] = [] + private initialized = false + /** + * Only used when initializing the manager + */ public initWith(diagnostics: NormalizedDiagnostic[]) { + if (this.initialized) { + throw new Error('FileDiagnosticManager is already initialized') + } + diagnostics.forEach((d) => { this.diagnostics.push(d) }) + + this.initialized = true } public getDiagnostics(fileName?: string) { @@ -17,13 +27,9 @@ class FileDiagnosticManager { return this.diagnostics } - // public get lastDiagnostic() { - // return this.diagnostics[this.diagnostics.length - 1] - // } - - public setFile(fileName: string, next: NormalizedDiagnostic[] | null) { + public updateByFileId(fileId: string, next: NormalizedDiagnostic[] | null) { for (let i = 0; i < this.diagnostics.length; i++) { - if (this.diagnostics[i].id === fileName) { + if (this.diagnostics[i].id === fileId) { this.diagnostics.splice(i, 1) i-- } @@ -33,19 +39,6 @@ class FileDiagnosticManager { this.diagnostics.push(...next) } } - - // public updateFile(next: NormalizedDiagnostic[] | null) { - // for (let i = 0; i < this.diagnostics.length; i++) { - // if (this.diagnostics[i].loc?.start.line === fileName) { - // this.diagnostics.splice(i, 1) - // i-- - // } - // } - - // if (next?.length) { - // this.diagnostics.push(...next) - // } - // } } export { FileDiagnosticManager } diff --git a/packages/vite-plugin-checker/src/checkers/eslint/main.ts b/packages/vite-plugin-checker/src/checkers/eslint/main.ts index aa75fd91..0577bec0 100644 --- a/packages/vite-plugin-checker/src/checkers/eslint/main.ts +++ b/packages/vite-plugin-checker/src/checkers/eslint/main.ts @@ -11,7 +11,6 @@ import { consoleLog, diagnosticToTerminalLog, diagnosticToViteError, - NormalizedDiagnostic, toViteCustomPayload, normalizeEslintDiagnostic, } from '../../logger' @@ -35,40 +34,18 @@ const createDiagnostic: CreateDiagnostic<'eslint'> = (pluginConfig) => { const options = optionator.parse(pluginConfig.eslint.lintCommand) const translatedOptions = translateOptions(options) as ESLint.Options - // const extensions = config.extensions ?? ['.js'] - // const overrideConfigFile = pluginConfig.eslint.configFile - // ? { overrideConfigFile: pluginConfig.eslint.configFile } - // : {} const eslint = new ESLint({ cwd: root, ...translatedOptions, ...pluginConfig.eslint.devOptions, - // extensions, - // ...overrideConfigFile, }) - // invariant(pluginConfig.eslint, 'config.eslint should not be `false`') - // invariant( - // pluginConfig.eslint.files, - // `eslint.files is required, but got ${pluginConfig.eslint.files}` - // ) - - // const paths = - // typeof pluginConfig.eslint.files === 'string' - // ? [pluginConfig.eslint.files] - // : pluginConfig.eslint.files - // const paths = config. - - // let diagnosticsCache: NormalizedDiagnostic[] = [] - const dispatchDiagnostics = () => { const diagnostics = manager.getDiagnostics() diagnostics.forEach((d) => { consoleLog(diagnosticToTerminalLog(d, 'ESLint')) }) - // const lastErr = diagnosticsCache[0] - if (overlay) { parentPort?.postMessage({ type: ACTION_TYPES.overlayError, @@ -76,32 +53,21 @@ const createDiagnostic: CreateDiagnostic<'eslint'> = (pluginConfig) => { 'eslint', diagnostics.map((d) => diagnosticToViteError(d)) ), - - // payload: lastErr - // ? { - // type: 'error', - // err: diagnosticToViteError(lastErr), - // } - // : null, }) } } const handleFileChange = async (filePath: string, type: 'change' | 'unlink') => { - // if (!extensions.includes(path.extname(filePath))) return const absPath = path.resolve(root, filePath) if (type === 'unlink') { - manager.setFile(absPath, []) - // diagnosticsCache = diagnosticsCache.filter((d) => d.id !== absPath) + manager.updateByFileId(absPath, []) } else if (type === 'change') { const diagnosticsOfChangedFile = await eslint.lintFiles(filePath) const newDiagnostics = diagnosticsOfChangedFile .map((d) => normalizeEslintDiagnostic(d)) .flat(1) - // const absPath = diagnosticsOfChangedFile[0].filePath - // diagnosticsCache = diagnosticsCache.filter((d) => d.id !== absPath).concat(newDiagnostics) - manager.setFile(absPath, newDiagnostics) + manager.updateByFileId(absPath, newDiagnostics) } dispatchDiagnostics() diff --git a/packages/vite-plugin-checker/src/checkers/vls/diagnostics.ts b/packages/vite-plugin-checker/src/checkers/vls/diagnostics.ts index 34355dad..7795210a 100644 --- a/packages/vite-plugin-checker/src/checkers/vls/diagnostics.ts +++ b/packages/vite-plugin-checker/src/checkers/vls/diagnostics.ts @@ -37,7 +37,7 @@ import { DeepPartial } from '../../types' import { getInitParams, VlsOptions } from './initParams' import type { ErrorPayload } from 'vite' -import { FileDiagnosticCache } from '../../DiagnosticCache' +import { FileDiagnosticManager } from '../../FileDiagnosticManager' enum DOC_VERSION { init = -1, } @@ -46,7 +46,7 @@ export type LogLevel = typeof logLevels[number] export const logLevels = ['ERROR', 'WARN', 'INFO', 'HINT'] as const let disposeSuppressConsole: ReturnType -const diagnosticCache = new FileDiagnosticCache() +const fileDiagnosticManager = new FileDiagnosticManager() export const logLevel2Severity = { ERROR: DiagnosticSeverity.Error, @@ -59,7 +59,7 @@ export interface DiagnosticOptions { watch: boolean verbose: boolean config: DeepPartial | null - errorCallback?: (diagnostic: PublishDiagnosticsParams, viteError: ErrorPayload['err']) => void + errorCallback?: (diagnostic: PublishDiagnosticsParams, viteError: ErrorPayload['err'][]) => void } export async function diagnostics( @@ -165,14 +165,14 @@ export async function prepareClientConnection( const absFilePath = URI.parse(publishDiagnostics.uri).fsPath publishDiagnostics.diagnostics = filterDiagnostics(publishDiagnostics.diagnostics, severity) const nextDiagnosticInFile = await normalizePublishDiagnosticParams(publishDiagnostics) - diagnosticCache.setFile(absFilePath, nextDiagnosticInFile) + fileDiagnosticManager.updateByFileId(absFilePath, nextDiagnosticInFile) - const res = diagnosticCache.getDiagnostics() + const res = fileDiagnosticManager.getDiagnostics() vlsConsoleLog(os.EOL) vlsConsoleLog(res.map((d) => diagnosticToTerminalLog(d, 'VLS')).join(os.EOL)) - if (diagnosticCache.lastDiagnostic) { - const normalized = diagnosticToViteError(diagnosticCache.lastDiagnostic) + if (res) { + const normalized = diagnosticToViteError(res) options.errorCallback?.(publishDiagnostics, normalized) } } diff --git a/packages/vite-plugin-checker/src/checkers/vls/main.ts b/packages/vite-plugin-checker/src/checkers/vls/main.ts index 68b9dcf1..01b4faa6 100644 --- a/packages/vite-plugin-checker/src/checkers/vls/main.ts +++ b/packages/vite-plugin-checker/src/checkers/vls/main.ts @@ -19,13 +19,7 @@ export const createDiagnostic: CreateDiagnostic<'vls'> = (pluginConfig) => { if (!overlay) return parentPort?.postMessage({ type: ACTION_TYPES.overlayError, - payload: toViteCustomPayload('vls', overlayErr ? [overlayErr] : []), - // payload: overlayErr - // ? { - // type: 'error', - // err: overlayErr, - // } - // : null, + payload: toViteCustomPayload('vls', overlayErr ? overlayErr : []), }) }