Skip to content

Commit

Permalink
feat: support arbitrary key for custom check (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework authored Jun 19, 2021
1 parent 1f2837a commit fc14b05
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Unit Test
name: Test

on:
push:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ declare const TestChecker: (
) => (config: CheckerConfig & SharedConfig) => ServeAndBuildChecker

export { TestChecker }
export type { CheckerConfig as VlsConfig }
export type { CheckerConfig }
3 changes: 1 addition & 2 deletions packages/vite-plugin-checker/__tests__/e2e/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ describe('build', () => {

it('run build bin by child_process.spawn', () => {
const plugin = CheckerPlugin({
// TODO: allow custom checker key
vls: TestChecker(),
myChecker: TestChecker(),
})

sandbox.plugin = plugin
Expand Down
4 changes: 4 additions & 0 deletions packages/vite-plugin-checker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
"homepage": "https://github.com/fi3ework/vite-plugin-checker",
"dependencies": {
"@babel/code-frame": "^7.12.13",
"@types/lodash.omit": "^4.5.6",
"@types/lodash.pick": "^4.4.6",
"lodash.omit": "^4.5.0",
"lodash.pick": "^4.4.0",
"log-update": "^4.0.0",
"npm-run-path": "^4.0.1",
"strip-ansi": "^6.0.0",
Expand Down
29 changes: 24 additions & 5 deletions packages/vite-plugin-checker/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import { spawn } from 'child_process'
import omit from 'lodash.omit'
import pick from 'lodash.pick'
import npmRunPath from 'npm-run-path'
import os from 'os'
import invariant from 'tiny-invariant'
import { ConfigEnv, Plugin } from 'vite'

import { OverlayErrorAction, ServeAndBuildChecker, UserPluginConfig } from './types'
import type {
OverlayErrorAction,
BuildInCheckers,
ServeAndBuildChecker,
UserPluginConfig,
SharedConfig,
} from './types'

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

const sharedConfigKeys: (keyof SharedConfig)[] = ['enableBuild', 'overlay']
const buildInCheckerKeys: (keyof BuildInCheckers)[] = ['typescript', 'vueTsc']

function createCheckers(userConfig: UserPluginConfig): ServeAndBuildChecker[] {
const { typescript, vueTsc } = userConfig
const serveAndBuildCheckers: ServeAndBuildChecker[] = []
const { typescript, vueTsc, vls: vlsCurry, ...sharedConfig } = userConfig
const sharedConfig = pick(userConfig, sharedConfigKeys)
const customCheckers = omit(userConfig, [...sharedConfigKeys, ...buildInCheckerKeys])

if (typescript) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
Expand All @@ -26,9 +40,14 @@ function createCheckers(userConfig: UserPluginConfig): ServeAndBuildChecker[] {
serveAndBuildCheckers.push(createServeAndBuild({ vueTsc, ...sharedConfig }))
}

if (vlsCurry) {
serveAndBuildCheckers.push(vlsCurry(sharedConfig))
}
Object.keys(customCheckers).forEach((key) => {
const checkerCurryFn = customCheckers[key]
invariant(
typeof checkerCurryFn === 'function',
`Custom checker key should be a function, but got ${typeof checkerCurryFn}`
)
serveAndBuildCheckers.push(checkerCurryFn(sharedConfig))
})

return serveAndBuildCheckers
}
Expand Down
13 changes: 11 additions & 2 deletions packages/vite-plugin-checker/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,21 @@ export interface SharedConfig {
overlay: boolean
}

export interface PluginConfig extends SharedConfig {
export type CustomChecker = (vlsConfig: any) => ServeAndBuildChecker

export interface CustomCheckers {
// TODO: poor TS index signature type https://stackoverflow.com/questions/49969390/how-do-i-type-an-object-with-known-and-unknown-keys-in-typescript?noredirect=1&lq=1
// should remove `| boolean`
[k: string]: CustomChecker | boolean
}

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

export type PluginConfig = SharedConfig & CustomCheckers & BuildInCheckers

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

Expand Down
32 changes: 32 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fc14b05

Please sign in to comment.