Skip to content

Commit

Permalink
refactor: do not use Vite's CustomPayload type
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jan 20, 2023
1 parent aa853a8 commit 47471b6
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 23 deletions.
4 changes: 2 additions & 2 deletions packages/vite-plugin-checker/src/checkers/eslint/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
diagnosticToTerminalLog,
filterLogLevel,
normalizeEslintDiagnostic,
toViteCustomPayload,
toClientPayload,
} from '../../logger.js'
import { ACTION_TYPES, DiagnosticLevel } from '../../types.js'
import { translateOptions } from './cli.js'
Expand Down Expand Up @@ -74,7 +74,7 @@ const createDiagnostic: CreateDiagnostic<'eslint'> = (pluginConfig) => {
if (overlay) {
parentPort?.postMessage({
type: ACTION_TYPES.overlayError,
payload: toViteCustomPayload(
payload: toClientPayload(
'eslint',
diagnostics.map((d) => diagnosticToRuntimeError(d))
),
Expand Down
4 changes: 2 additions & 2 deletions packages/vite-plugin-checker/src/checkers/stylelint/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
diagnosticToTerminalLog,
filterLogLevel,
normalizeStylelintDiagnostic,
toViteCustomPayload,
toClientPayload,
} from '../../logger.js'
import { ACTION_TYPES, DiagnosticLevel } from '../../types.js'

Expand Down Expand Up @@ -66,7 +66,7 @@ const createDiagnostic: CreateDiagnostic<'stylelint'> = (pluginConfig) => {
if (overlay) {
parentPort?.postMessage({
type: ACTION_TYPES.overlayError,
payload: toViteCustomPayload(
payload: toClientPayload(
'stylelint',
diagnostics.map((d) => diagnosticToRuntimeError(d))
),
Expand Down
4 changes: 2 additions & 2 deletions packages/vite-plugin-checker/src/checkers/typescript/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
diagnosticToTerminalLog,
ensureCall,
normalizeTsDiagnostic,
toViteCustomPayload,
toClientPayload,
wrapCheckerSummary,
} from '../../logger.js'
import { ACTION_TYPES, CreateDiagnostic, DiagnosticToRuntime } from '../../types.js'
Expand Down Expand Up @@ -86,7 +86,7 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {
if (overlay) {
parentPort?.postMessage({
type: ACTION_TYPES.overlayError,
payload: toViteCustomPayload('typescript', currDiagnostics),
payload: toClientPayload('typescript', currDiagnostics),
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/vite-plugin-checker/src/checkers/vls/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
consoleLog,
diagnosticToRuntimeError,
diagnosticToTerminalLog,
toViteCustomPayload,
toClientPayload,
} from '../../logger.js'
import { ACTION_TYPES } from '../../types.js'
import { DiagnosticOptions, diagnostics } from './diagnostics.js'
Expand Down Expand Up @@ -47,7 +47,7 @@ export const createDiagnostic: CreateDiagnostic<'vls'> = (pluginConfig) => {
if (overlay && command === 'serve') {
parentPort?.postMessage({
type: ACTION_TYPES.overlayError,
payload: toViteCustomPayload('vls', diagnosticToRuntimeError(normalized)),
payload: toClientPayload('vls', diagnosticToRuntimeError(normalized)),
})
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vite-plugin-checker/src/checkers/vueTsc/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
diagnosticToTerminalLog,
ensureCall,
normalizeVueTscDiagnostic,
toViteCustomPayload,
toClientPayload,
wrapCheckerSummary,
} from '../../logger.js'
import { ACTION_TYPES, CreateDiagnostic, DiagnosticToRuntime } from '../../types.js'
Expand Down Expand Up @@ -99,7 +99,7 @@ const createDiagnostic: CreateDiagnostic<'vueTsc'> = (pluginConfig) => {
if (overlay) {
parentPort?.postMessage({
type: ACTION_TYPES.overlayError,
payload: toViteCustomPayload('vue-tsc', currDiagnostics),
payload: toClientPayload('vue-tsc', currDiagnostics),
})
}
}
Expand Down
14 changes: 10 additions & 4 deletions packages/vite-plugin-checker/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import { parentPort } from 'worker_threads'
import { codeFrameColumns, SourceLocation } from '@babel/code-frame'

import { WS_CHECKER_ERROR_EVENT } from './client/index.js'
import { ACTION_TYPES, DiagnosticLevel, DiagnosticToRuntime } from './types.js'
import {
ACTION_TYPES,
DiagnosticLevel,
DiagnosticToRuntime,
ClientDiagnosticPayload,
} from './types.js'
import { isMainThread } from './utils.js'

import type { CustomPayload } from 'vite'
const _require = createRequire(import.meta.url)
import type { Range } from 'vscode-languageclient'
import type { ESLint } from 'eslint'
Expand Down Expand Up @@ -142,9 +146,11 @@ export function diagnosticToRuntimeError(
return Array.isArray(diagnostics) ? results : results[0]!
}

export function toViteCustomPayload(id: string, diagnostics: DiagnosticToRuntime[]): CustomPayload {
export function toClientPayload(
id: string,
diagnostics: DiagnosticToRuntime[]
): ClientDiagnosticPayload {
return {
type: 'custom',
event: WS_CHECKER_ERROR_EVENT,
data: {
checkerId: id,
Expand Down
14 changes: 8 additions & 6 deletions packages/vite-plugin-checker/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@ import chalk from 'chalk'
import { spawn } from 'child_process'
import pick from 'lodash.pick'
import npmRunPath from 'npm-run-path'
import type { ConfigEnv, Plugin, ResolvedConfig } from 'vite'

import { Checker } from './Checker.js'
import {
RUNTIME_CLIENT_RUNTIME_PATH,
composePreambleCode,
RUNTIME_CLIENT_ENTRY_PATH,
RUNTIME_CLIENT_RUNTIME_PATH,
runtimeCode,
composePreambleCode,
WS_CHECKER_RECONNECT_EVENT,
} from './client/index.js'
import {
ACTION_TYPES,
BuildCheckBinStr,
BuildInCheckerNames,
ClientDiagnosticPayload,
ClientReconnectPayload,
OverlayErrorAction,
PluginConfig,
ServeAndBuildChecker,
SharedConfig,
UserPluginConfig,
} from './types.js'

import type { ConfigEnv, Plugin, ResolvedConfig } from 'vite'
const sharedConfigKeys: (keyof SharedConfig)[] = ['enableBuild', 'overlay']
const buildInCheckerKeys: BuildInCheckerNames[] = [
'typescript',
Expand Down Expand Up @@ -154,15 +157,15 @@ export function checker(userConfig: UserPluginConfig): Plugin {
})()
},
configureServer(server) {
let latestOverlayErrors: OverlayErrorAction['payload'][] = new Array(checkers.length)
let latestOverlayErrors: ClientReconnectPayload['data'] = new Array(checkers.length)
// for dev mode (2/2)
// Get the server instance and keep reference in a closure
checkers.forEach((checker, index) => {
const { worker, configureServer: workerConfigureServer } = checker.serve
workerConfigureServer({ root: server.config.root })
worker.on('message', (action: OverlayErrorAction) => {
if (action.type === ACTION_TYPES.overlayError) {
latestOverlayErrors[index] = action.payload
latestOverlayErrors[index] = action.payload as ClientDiagnosticPayload
if (action.payload) {
server.ws.send('vite-plugin-checker', action.payload)
}
Expand All @@ -179,7 +182,6 @@ export function checker(userConfig: UserPluginConfig): Plugin {
// will be displayed again after full-reload.
server.ws.on('connection', () => {
server.ws.send('vite-plugin-checker', {
type: 'custom',
event: WS_CHECKER_RECONNECT_EVENT,
data: latestOverlayErrors.filter(Boolean),
})
Expand Down
21 changes: 18 additions & 3 deletions packages/vite-plugin-checker/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ErrorPayload, ConfigEnv, CustomPayload } from 'vite'
import type { ErrorPayload, ConfigEnv } from 'vite'
import type { Worker } from 'worker_threads'
import type { ESLint } from 'eslint'
import type * as Stylelint from 'stylelint'
Expand Down Expand Up @@ -90,6 +90,21 @@ export interface DiagnosticToRuntime extends ErrorPayloadErr {
level?: DiagnosticLevel
}

export interface ClientDiagnosticPayload {
event: 'vite-plugin-checker:error'
data: {
checkerId: string
diagnostics: DiagnosticToRuntime[]
}
}

export interface ClientReconnectPayload {
event: 'vite-plugin-checker:reconnect'
data: ClientDiagnosticPayload[]
}

export type ClientPayload = ClientDiagnosticPayload | ClientReconnectPayload

/** checkers shared configuration */
export interface SharedConfig {
/**
Expand Down Expand Up @@ -180,10 +195,10 @@ interface Action {
export interface OverlayErrorAction extends Action {
type: ACTION_TYPES.overlayError
/**
* send `CustomPayload` to raise error overlay provided by Vite
* send `ClientPayload` to raise error overlay
* send `null` to clear overlay for current checker
*/
payload: CustomPayload | null
payload: ClientPayload
}

interface ConfigActionPayload {
Expand Down
1 change: 1 addition & 0 deletions packages/vite-plugin-checker/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "@tsconfig/node16-strictest-esm/tsconfig.json",
"compilerOptions": {
"isolatedModules": true,
"noUnusedParameters": false,
"exactOptionalPropertyTypes": false
},
Expand Down

0 comments on commit 47471b6

Please sign in to comment.