Skip to content

Commit

Permalink
fix: checker can read shared and own config
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jun 10, 2021
1 parent 1cde236 commit a906e83
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 107 deletions.
2 changes: 1 addition & 1 deletion examples/vue2-vls/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const config = defineConfig({
plugins: [
createVuePlugin({}),
ViteComponents({ transformer: 'vue2' }),
Checker({ tsc: true, vls: VlsChecker({}) }),
Checker({ typescript: true, vls: VlsChecker({}) }),
],
server: {
port: 8080,
Expand Down
2 changes: 1 addition & 1 deletion examples/vue3-vue-tsc/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import Checker from 'vite-plugin-checker'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue(), Checker({ checker: 'vue-tsc' })],
plugins: [vue(), Checker({ vueTsc: true })],
})
35 changes: 17 additions & 18 deletions presets/vls/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {
BuildCheckBin,
ServeCheckerFactory,
SharedConfig,
CreateDiagnostic,
createScript,
lspDiagnosticToViteError,
uriToAbsPath,
ServeAndBuildConfig,
ServeAndBuildChecker,
} from 'vite-plugin-checker'
import { isMainThread, parentPort } from 'worker_threads'

Expand Down Expand Up @@ -50,24 +49,22 @@ export const createDiagnostic: CreateDiagnostic = (userOptions = {}) => {
}
}

const vlsCheckerFactory: ServeCheckerFactory = () => {
return {
createDiagnostic,
}
}

export const buildBin: BuildCheckBin = ['vite-plugin-checker-preset-vls', ['diagnostics']]

const { mainScript, workerScript } = createScript<VlsConfig>({
const { mainScript, workerScript } = createScript<{ vls: VlsConfig }>({
absFilename: __filename,
buildBin,
checkerFactory: vlsCheckerFactory,
buildBin: ['vite-plugin-checker-preset-vls', ['diagnostics']],
serverChecker: { createDiagnostic },
})!

if (isMainThread) {
const { createServeAndBuild } = mainScript()
module.exports.VlsChecker = createServeAndBuild
module.exports.createServeAndBuild = createServeAndBuild
const createChecker = mainScript()
const configCurryFn = (vlsConfig: VlsConfig) => {
return (sharedConfig: SharedConfig) => {
return createChecker({ vls: vlsConfig, ...sharedConfig })
}
}

module.exports.VlsChecker = configCurryFn
module.exports.createServeAndBuild = configCurryFn
} else {
workerScript()
}
Expand All @@ -77,7 +74,9 @@ type VlsConfig = Partial<{
root: string
}>

declare const VlsChecker: (options?: VlsConfig) => ServeAndBuildConfig
declare const VlsChecker: (
options?: VlsConfig
) => (config: VlsConfig & SharedConfig) => ServeAndBuildChecker

export { VlsChecker }
export type { VlsConfig }
36 changes: 18 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,39 @@ import npmRunPath from 'npm-run-path'
import os from 'os'
import { ConfigEnv, Plugin } from 'vite'

import { ServeAndBuildConfig, OverlayErrorAction, PluginOptions } from './types'
import { ServeAndBuildChecker, OverlayErrorAction, UserPluginConfig } from './types'

export * from './types'
export * from './codeFrame'
export * from './utils'
export * from './worker'

function createServeAndBuild(userOptions: Partial<PluginOptions>): ServeAndBuildConfig[] {
const checkers: ServeAndBuildConfig[] = []
const { typescript, vueTsc, vls } = userOptions
function createCheckers(userConfig: UserPluginConfig): ServeAndBuildChecker[] {
const serveAndBuildCheckers: ServeAndBuildChecker[] = []
const { typescript, vueTsc, vls: vlsCurry, ...sharedConfig } = userConfig

if (typescript) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { createServeAndBuild } = require('./presets/tsc')
checkers.push(createServeAndBuild(userOptions.typescript))
serveAndBuildCheckers.push(createServeAndBuild({ typescript, ...sharedConfig }))
}

if (vueTsc) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { createServeAndBuild } = require('./presets/vue-tsc')
checkers.push(createServeAndBuild(userOptions.vueTsc))
serveAndBuildCheckers.push(createServeAndBuild({ vueTsc, ...sharedConfig }))
}

if (vls) {
checkers.push(vls)
if (vlsCurry) {
serveAndBuildCheckers.push(vlsCurry(sharedConfig))
}

return checkers
return serveAndBuildCheckers
}

export default function Plugin(userOptions?: Partial<PluginOptions>): Plugin {
const serveAndBuilds = createServeAndBuild(userOptions || {})
const enableBuild = userOptions?.enableBuild ?? true
export default function Plugin(userConfig?: UserPluginConfig): Plugin {
const checkers = createCheckers(userConfig || {})
const enableBuild = userConfig?.enableBuild ?? true
let viteMode: ConfigEnv['command'] | undefined

return {
Expand All @@ -46,8 +46,8 @@ export default function Plugin(userOptions?: Partial<PluginOptions>): Plugin {
viteMode = env.command
if (viteMode !== 'serve') return

serveAndBuilds.forEach((serveAndBuild) => {
const workerConfig = serveAndBuild.serve.config
checkers.forEach((checker) => {
const workerConfig = checker.serve.config
workerConfig({
hmr: config.server?.hmr,
env,
Expand All @@ -65,8 +65,8 @@ export default function Plugin(userOptions?: Partial<PluginOptions>): Plugin {
execPath: process.execPath,
})

serveAndBuilds.forEach((serveAndBuild) => {
const buildBin = serveAndBuild.build.buildBin
checkers.forEach((checker) => {
const buildBin = checker.build.buildBin
const proc = spawn(buildBin[0], buildBin[1], {
cwd: process.cwd(),
stdio: 'inherit',
Expand All @@ -85,8 +85,8 @@ export default function Plugin(userOptions?: Partial<PluginOptions>): Plugin {
configureServer(server) {
// for dev mode (2/2)
// Get the server instance and keep reference in a closure
serveAndBuilds.forEach((serveAndBuild) => {
const { worker, configureServer: workerConfigureServer } = serveAndBuild.serve
checkers.forEach((checker) => {
const { worker, configureServer: workerConfigureServer } = checker.serve
workerConfigureServer({ root: server.config.root })
worker.on('message', (action: OverlayErrorAction) => {
server.ws.send(action.payload)
Expand Down
34 changes: 14 additions & 20 deletions src/presets/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,35 @@ import ts from 'typescript'
import { ErrorPayload } from 'vite'
import { isMainThread, parentPort } from 'worker_threads'

import { TscConfig } from '../types'
import { ensureCall, formatHost, tsDiagnosticToViteError } from '../utils'
import { createScript } from '../worker'

import type { CreateDiagnostic, ServeCheckerFactory, BuildCheckBin } from '../types'
import type { CreateDiagnostic, PluginConfig } from '../types'

/**
* Prints a diagnostic every time the watch status changes.
* This is mainly for messages like "Starting compilation" or "Compilation completed".
*/
const createDiagnostic: CreateDiagnostic = (userOptions = {}) => {
const createDiagnostic: CreateDiagnostic<Pick<PluginConfig, 'typescript'>> = (checkerConfig) => {
let overlay = true // Vite defaults to true
let currErr: ErrorPayload['err'] | null = null

return {
config: ({ hmr }) => {
const viteOverlay = !(typeof hmr === 'object' && hmr.overlay === false)

if (userOptions.overlay === false || !viteOverlay) {
if (checkerConfig.overlay === false || !viteOverlay) {
overlay = false
}
},
configureServer({ root }) {
const finalConfig = {
root: userOptions.root ?? root,
tsconfigPath: userOptions.tsconfigPath ?? 'tsconfig.json',
}
const finalConfig =
typeof checkerConfig.typescript === 'boolean'
? { root, tsconfigPath: 'tsconfig.json' }
: {
root: checkerConfig.typescript.root ?? root,
tsconfigPath: checkerConfig.typescript.tsconfigPath ?? 'tsconfig.json',
}

let configFile: string | undefined

Expand Down Expand Up @@ -103,22 +105,14 @@ const createDiagnostic: CreateDiagnostic = (userOptions = {}) => {
}
}

const checkerFactory: ServeCheckerFactory = () => {
return {
createDiagnostic,
}
}

export const buildBin: BuildCheckBin = ['tsc', ['--noEmit']]

const { mainScript, workerScript } = createScript<TscConfig>({
const { mainScript, workerScript } = createScript<Pick<PluginConfig, 'typescript'>>({
absFilename: __filename,
buildBin,
checkerFactory,
buildBin: ['tsc', ['--noEmit']],
serverChecker: { createDiagnostic },
})!

if (isMainThread) {
const { createServeAndBuild } = mainScript()
const createServeAndBuild = mainScript()
module.exports.createServeAndBuild = createServeAndBuild
} else {
workerScript()
Expand Down
21 changes: 6 additions & 15 deletions src/presets/vue-tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isMainThread } from 'worker_threads'

import { createScript } from '../worker'

import type { VueTscConfig, ServeCheckerFactory, CreateDiagnostic, BuildCheckBin } from '../types'
import type { PluginConfig, CreateDiagnostic } from '../types'
import type { UserConfig, ViteDevServer } from 'vite'

// TODO: watch mode is not supported for now
Expand All @@ -14,7 +14,7 @@ import type { UserConfig, ViteDevServer } from 'vite'
*
*/
// @ts-ignore
export const createDiagnostic: CreateDiagnostic = (userOptions = {}) => {
export const createDiagnostic: CreateDiagnostic<Pick<PluginConfig, 'vueTsc'>> = (checkerConfig) => {
return {
config: (config: UserConfig) => {
//
Expand All @@ -25,23 +25,14 @@ export const createDiagnostic: CreateDiagnostic = (userOptions = {}) => {
}
}

export const buildBin: BuildCheckBin = ['vue-tsc', ['--noEmit']]

export const checkerFactory: ServeCheckerFactory = () => {
return {
buildBin: ['vue-tsc', ['--noEmit']],
createDiagnostic: createDiagnostic,
}
}

const { mainScript, workerScript } = createScript<VueTscConfig>({
const { mainScript, workerScript } = createScript<Pick<PluginConfig, 'vueTsc'>>({
absFilename: __filename,
buildBin,
checkerFactory,
buildBin: ['vue-tsc', ['--noEmit']],
serverChecker: { createDiagnostic },
})!

if (isMainThread) {
const { createServeAndBuild } = mainScript()
const createServeAndBuild = mainScript()
module.exports.createServeAndBuild = createServeAndBuild
} else {
workerScript()
Expand Down
34 changes: 14 additions & 20 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ export type TscConfig =
root: string
}>

/** vue-tsc configuration */
/** vue-tsc checker configuration */
export type VueTscConfig =
| boolean
| Partial<{
/** root path of cwd */
root: string
}>

export interface PluginOptions {
typescript: TscConfig
vueTsc: VueTscConfig
vls: ServeAndBuildConfig
/** checkers shared configuration */
export interface SharedConfig {
/**
* Enable checking in build mode
* @defaultValue `true`
Expand All @@ -36,19 +34,17 @@ export interface PluginOptions {
* Same as [Vite config](https://vitejs.dev/config/#root)
*/
overlay: boolean
// /**
// * Root path to find tsconfig file
// * @defaultValue
// * Same as [Vite config](https://vitejs.dev/config/#root)
// */
// root: string
// /**
// * Relative tsconfig path to {@link (PluginOptions:interface).root}
// * @defaultValue `"tsconfig.json"`
// */
// tsconfigPath: string
}

export interface PluginConfig extends SharedConfig {
typescript: TscConfig
vueTsc: VueTscConfig
vls: (vlsConfig: any) => ServeAndBuildChecker
}

/** Userland plugin configuration */
export type UserPluginConfig = Partial<PluginConfig>

/* ----------------------------- worker actions ----------------------------- */

export enum ACTION_TYPES {
Expand Down Expand Up @@ -93,15 +89,13 @@ export interface ConfigureServeChecker {
configureServer: (serverConfig: ConfigureServerAction['payload']) => void
}

export interface ServeAndBuildConfig {
export interface ServeAndBuildChecker {
serve: ConfigureServeChecker
build: { buildBin: BuildCheckBin }
}

// create serve & build checker

export type ServeCheckerFactory = (options?: unknown) => ServeChecker

export interface ServeChecker {
createDiagnostic: CreateDiagnostic
}
Expand All @@ -111,4 +105,4 @@ export interface CheckerDiagnostic {
configureServer: (options: { root: string }) => unknown
}

export type CreateDiagnostic = (config?: Partial<PluginOptions>) => CheckerDiagnostic
export type CreateDiagnostic<T = any> = (config: T & SharedConfig) => CheckerDiagnostic
Loading

0 comments on commit a906e83

Please sign in to comment.