-
Notifications
You must be signed in to change notification settings - Fork 76
/
main.ts
147 lines (126 loc) 路 4.45 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import chokidar from 'chokidar'
import stylelint from 'stylelint'
import { translateOptions } from './options.js'
import path from 'path'
import { fileURLToPath } from 'url'
import { parentPort } from 'worker_threads'
import { Checker } from '../../Checker.js'
import { FileDiagnosticManager } from '../../FileDiagnosticManager.js'
import {
composeCheckerSummary,
consoleLog,
diagnosticToRuntimeError,
diagnosticToTerminalLog,
filterLogLevel,
normalizeStylelintDiagnostic,
toViteCustomPayload,
} from '../../logger.js'
import { ACTION_TYPES, DiagnosticLevel } from '../../types.js'
const manager = new FileDiagnosticManager()
let createServeAndBuild
import type { CreateDiagnostic } from '../../types.js'
const __filename = fileURLToPath(import.meta.url)
const createDiagnostic: CreateDiagnostic<'stylelint'> = (pluginConfig) => {
let overlay = true
let terminal = true
return {
config: async ({ enableOverlay, enableTerminal }) => {
overlay = enableOverlay
terminal = enableTerminal
},
async configureServer({ root }) {
if (!pluginConfig.stylelint) return
const translatedOptions = translateOptions(pluginConfig.stylelint.lintCommand)
const logLevel = (() => {
if (typeof pluginConfig.stylelint !== 'object') return undefined
const userLogLevel = pluginConfig.stylelint.dev?.logLevel
if (!userLogLevel) return undefined
const map = {
error: DiagnosticLevel.Error,
warning: DiagnosticLevel.Warning,
} as const
return userLogLevel.map((l) => map[l])
})()
const dispatchDiagnostics = () => {
const diagnostics = filterLogLevel(manager.getDiagnostics(), logLevel)
if (terminal) {
diagnostics.forEach((d) => {
consoleLog(diagnosticToTerminalLog(d, 'Stylelint'))
})
const errorCount = diagnostics.filter((d) => d.level === DiagnosticLevel.Error).length
const warningCount = diagnostics.filter((d) => d.level === DiagnosticLevel.Warning).length
consoleLog(composeCheckerSummary('Stylelint', errorCount, warningCount))
}
if (overlay) {
parentPort?.postMessage({
type: ACTION_TYPES.overlayError,
payload: toViteCustomPayload(
'stylelint',
diagnostics.map((d) => diagnosticToRuntimeError(d))
),
})
}
}
const handleFileChange = async (filePath: string, type: 'change' | 'unlink') => {
const absPath = path.resolve(root, filePath)
if (type === 'unlink') {
manager.updateByFileId(absPath, [])
} else if (type === 'change') {
const { results: diagnosticsOfChangedFile } = await stylelint.lint({ files: filePath })
const newDiagnostics = diagnosticsOfChangedFile
.map((d) => normalizeStylelintDiagnostic(d))
.flat(1)
manager.updateByFileId(absPath, newDiagnostics)
}
dispatchDiagnostics()
}
// initial lint
const { results: diagnostics } = await stylelint.lint({
cwd: root,
...translatedOptions,
...pluginConfig.stylelint.dev?.overrideConfig,
})
manager.initWith(diagnostics.map((p) => normalizeStylelintDiagnostic(p)).flat(1))
dispatchDiagnostics()
// watch lint
const watcher = chokidar.watch([], {
cwd: root,
ignored: (path: string) => path.includes('node_modules'),
})
watcher.add(translatedOptions.files as string)
watcher.on('change', async (filePath) => {
handleFileChange(filePath, 'change')
})
watcher.on('unlink', async (filePath) => {
handleFileChange(filePath, 'unlink')
})
},
}
}
export class StylelintChecker extends Checker<'stylelint'> {
public constructor() {
super({
name: 'stylelint',
absFilePath: __filename,
build: {
buildBin: (pluginConfig) => {
if (pluginConfig.stylelint) {
const { lintCommand } = pluginConfig.stylelint
return ['stylelint', lintCommand.split(' ').slice(1)]
}
return ['stylelint', ['']]
},
},
createDiagnostic,
})
}
public init() {
const _createServeAndBuild = super.initMainThread()
createServeAndBuild = _createServeAndBuild
super.initWorkerThread()
}
}
export { createServeAndBuild }
const stylelintChecker = new StylelintChecker()
stylelintChecker.prepare()
stylelintChecker.init()