Skip to content

Commit

Permalink
feat: support VLS in dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jun 1, 2021
1 parent d6e602d commit 3e151cf
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 25 deletions.
2 changes: 1 addition & 1 deletion examples/vue2-ts/src/components/HelloWorld.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="hello">
<h1>{{ msg1 }}</h1>
<h1>{{ msg12 }}</h1>
<p>
For a guide and recipes on how to configure / customize this project,<br />
check out the
Expand Down
27 changes: 15 additions & 12 deletions pnpm-lock.yaml

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

7 changes: 4 additions & 3 deletions presets/vls/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"bugs": "https://github.com/fi3ework/vite-plugin-ts-checker/issues",
"homepage": "https://github.com/fi3ework/vite-plugin-ts-checker",
"dependencies": {
"chokidar": "^3.5.1",
"commander": "^7.2.0",
"vls": "^0.7.2"
},
Expand All @@ -38,15 +39,15 @@
"vite-plugin-ts-checker": "^0.1.3"
},
"devDependencies": {
"typescript": "^4.2.2",
"vite-plugin-ts-checker": "^0.1.3",
"vite": "^2.1.3",
"@babel/code-frame": "^7.12.13",
"@types/babel__code-frame": "^7.0.2",
"@types/glob": "^7.1.3",
"chalk": "^4.1.1",
"glob": "^7.1.7",
"rollup": "^2.47.0",
"typescript": "^4.2.2",
"vite": "^2.1.3",
"vite-plugin-ts-checker": "^0.1.3",
"vscode-languageclient": "^7.0.0",
"vscode-languageserver": "^7.0.0",
"vscode-languageserver-protocol": "^3.16.0",
Expand Down
4 changes: 4 additions & 0 deletions presets/vls/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Command, Option } from 'commander'

import { diagnostics, LogLevel, logLevels } from './commands/diagnostics'

function getVersion(): string {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { version }: { version: string } = require('../package.json')
return `v${version}`
}
Expand All @@ -28,9 +30,11 @@ function validateLogLevel(logLevelInput: unknown): logLevelInput is LogLevel {
)
.action(async (workspace, options) => {
const logLevelOption: unknown = options.logLevel

if (!validateLogLevel(logLevelOption)) {
throw new Error(`Invalid log level: ${logLevelOption}`)
}

await diagnostics(workspace, logLevelOption)
})

Expand Down
41 changes: 36 additions & 5 deletions presets/vls/src/commands/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import chalk from 'chalk'
import chokidar from 'chokidar'
import fs from 'fs'
import glob from 'glob'
import path from 'path'
Expand All @@ -11,6 +12,7 @@ import {
Diagnostic,
DiagnosticSeverity,
DidOpenTextDocumentNotification,
DidChangeTextDocumentNotification,
InitializeParams,
InitializeRequest,
InitializeResult,
Expand All @@ -34,7 +36,7 @@ const logLevel2Severity = {
HINT: DiagnosticSeverity.Hint,
}

export async function diagnostics(workspace: string | null, logLevel: LogLevel) {
export async function diagnostics(workspace: string | null, logLevel: LogLevel, watch = false) {
console.log('====================================')
console.log('Getting Vetur diagnostics')
let workspaceUri
Expand All @@ -48,15 +50,19 @@ export async function diagnostics(workspace: string | null, logLevel: LogLevel)
workspaceUri = URI.file(process.cwd())
}

const errCount = await getDiagnostics(workspaceUri, logLevel2Severity[logLevel])
const errCount = await getDiagnostics(workspaceUri, logLevel2Severity[logLevel], watch)
console.log('====================================')

if (errCount === 0) {
console.log(chalk.green(`VTI found no error`))
process.exit(0)
if (!watch) {
process.exit(0)
}
} else {
console.log(chalk.red(`VTI found ${errCount} ${errCount === 1 ? 'error' : 'errors'}`))
process.exit(1)
if (!watch) {
process.exit(1)
}
}
}

Expand Down Expand Up @@ -91,6 +97,13 @@ async function prepareClientConnection(workspaceUri: URI) {
new StreamMessageReader(up),
new StreamMessageWriter(down)
)

// NOTE: hijack sendDiagnostics
serverConnection.sendDiagnostics = (diagnostic) => {
if (!diagnostic.diagnostics.length) return
console.log(chalk.red(JSON.stringify(diagnostic, null, 2)))
}

const vls = new VLS(serverConnection as any)

serverConnection.onInitialize(async (params: InitializeParams): Promise<InitializeResult> => {
Expand Down Expand Up @@ -127,7 +140,7 @@ function range2Location(range: Range): SourceLocation {
}
}

async function getDiagnostics(workspaceUri: URI, severity: DiagnosticSeverity) {
async function getDiagnostics(workspaceUri: URI, severity: DiagnosticSeverity, watch: boolean) {
const clientConnection = await prepareClientConnection(workspaceUri)

const files = glob.sync('**/*.vue', { cwd: workspaceUri.fsPath, ignore: ['node_modules/**'] })
Expand All @@ -144,6 +157,24 @@ async function getDiagnostics(workspaceUri: URI, severity: DiagnosticSeverity) {

let errCount = 0

if (watch) {
chokidar
.watch(workspaceUri.fsPath, {
ignored: (path: string) => path.includes('node_modules'),
ignoreInitial: true,
})
.on('all', (event, path) => {
if (!path.endsWith('.vue')) return
clientConnection.sendNotification(DidChangeTextDocumentNotification.type, {
textDocument: {
uri: URI.file(path).toString(),
version: Date.now(),
},
contentChanges: [{ text: fs.readFileSync(path, 'utf-8') }],
})
})
}

for (const absFilePath of absFilePaths) {
const fileText = fs.readFileSync(absFilePath, 'utf-8')
clientConnection.sendNotification(DidOpenTextDocumentNotification.type, {
Expand Down
2 changes: 1 addition & 1 deletion presets/vls/src/initParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function getInitParams(workspaceUri: URI): InitializeParams {
return init
}

function getDefaultVLSConfig() {
export function getDefaultVLSConfig() {
return {
vetur: {
useWorkspaceDependencies: false,
Expand Down
15 changes: 12 additions & 3 deletions presets/vls/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import type { UserConfig, ViteDevServer } from 'vite'
import { CheckerFactory, CreateDiagnostic, PluginOptions } from 'vite-plugin-ts-checker'
import { diagnostics, logLevels } from './commands/diagnostics'

export const createDiagnostic: CreateDiagnostic = (userOptions = {}) => {
let overlay = true // Vite defaults to true

return {
config: (config: UserConfig) => {
// TODO:
const hmr = config.server?.hmr
const viteOverlay = !(typeof hmr === 'object' && hmr.overlay === false)

if (userOptions.overlay === false || !viteOverlay) {
overlay = false
}
},
configureServer(server: ViteDevServer) {
// TODO:
async configureServer(server: ViteDevServer) {
const workDir: string = userOptions.root ?? server.config.root
await diagnostics(workDir, 'WARN', true)
},
}
}
Expand Down

0 comments on commit 3e151cf

Please sign in to comment.