|
| 1 | +import os from 'os' |
| 2 | +import path from 'path' |
| 3 | +import * as fs from 'fs' |
| 4 | +import invariant from 'tiny-invariant' |
| 5 | +import { URI } from 'vscode-uri' |
| 6 | +import { watch } from 'chokidar' |
| 7 | +import { SvelteCheck } from 'svelte-language-server' |
| 8 | +import { parentPort } from 'worker_threads' |
| 9 | + |
| 10 | +import { Checker } from '../../Checker' |
| 11 | +import { |
| 12 | + diagnosticToTerminalLog, |
| 13 | + diagnosticToViteError, |
| 14 | + normalizeLspDiagnostic, |
| 15 | +} from '../../logger' |
| 16 | + |
| 17 | +import type { CreateDiagnostic } from '../../types' |
| 18 | + |
| 19 | +// based off the class in svelte-check/src/index.ts |
| 20 | +class DiagnosticsWatcher { |
| 21 | + private updateDiagnostics: any |
| 22 | + private svelteCheck: SvelteCheck |
| 23 | + private overlay: boolean |
| 24 | + |
| 25 | + public constructor(root: string, overlay: boolean) { |
| 26 | + this.overlay = overlay |
| 27 | + this.svelteCheck = new SvelteCheck(root, { |
| 28 | + compilerWarnings: {}, |
| 29 | + diagnosticSources: ['js', 'svelte', 'css'], |
| 30 | + }) |
| 31 | + |
| 32 | + watch(`${root}/**/*.{svelte,d.ts,ts,js}`, { |
| 33 | + ignored: ['node_modules'].map((ignore) => path.join(root, ignore)), |
| 34 | + ignoreInitial: false, |
| 35 | + }) |
| 36 | + .on('add', (path) => this.updateDocument(path, true)) |
| 37 | + .on('unlink', (path) => this.removeDocument(path)) |
| 38 | + .on('change', (path) => this.updateDocument(path, false)) |
| 39 | + } |
| 40 | + |
| 41 | + private async updateDocument(path: string, isNew: boolean) { |
| 42 | + const text = fs.readFileSync(path, 'utf-8') |
| 43 | + await this.svelteCheck.upsertDocument({ text, uri: URI.file(path).toString() }, isNew) |
| 44 | + this.scheduleDiagnostics() |
| 45 | + } |
| 46 | + |
| 47 | + private async removeDocument(path: string) { |
| 48 | + await this.svelteCheck.removeDocument(URI.file(path).toString()) |
| 49 | + this.scheduleDiagnostics() |
| 50 | + } |
| 51 | + |
| 52 | + private scheduleDiagnostics() { |
| 53 | + clearTimeout(this.updateDiagnostics) |
| 54 | + this.updateDiagnostics = setTimeout(async () => { |
| 55 | + let logChunk = '' |
| 56 | + try { |
| 57 | + const ds = await this.svelteCheck.getDiagnostics() |
| 58 | + let currErr = null |
| 59 | + |
| 60 | + for (const { filePath, text, diagnostics } of ds) { |
| 61 | + for (const diagnostic of diagnostics) { |
| 62 | + const formattedDiagnostic = normalizeLspDiagnostic({ |
| 63 | + diagnostic, |
| 64 | + absFilePath: filePath, |
| 65 | + fileText: text, |
| 66 | + checker: 'svelte', |
| 67 | + }) |
| 68 | + |
| 69 | + if (currErr === null) { |
| 70 | + currErr = diagnosticToViteError(formattedDiagnostic) |
| 71 | + } |
| 72 | + logChunk += os.EOL + diagnosticToTerminalLog(formattedDiagnostic, 'svelte') |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (this.overlay) { |
| 77 | + parentPort?.postMessage({ |
| 78 | + type: 'ERROR', |
| 79 | + payload: { |
| 80 | + type: 'error', |
| 81 | + err: currErr, |
| 82 | + }, |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + console.log(logChunk) |
| 87 | + } catch (err) { |
| 88 | + if (this.overlay) { |
| 89 | + parentPort?.postMessage({ |
| 90 | + type: 'ERROR', |
| 91 | + payload: { |
| 92 | + type: 'error', |
| 93 | + err: err.message, |
| 94 | + }, |
| 95 | + }) |
| 96 | + } |
| 97 | + console.error(err.message) |
| 98 | + } |
| 99 | + }, 1000) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +const createDiagnostic: CreateDiagnostic<'svelte'> = (pluginConfig) => { |
| 104 | + let overlay = true // Vite defaults to true |
| 105 | + |
| 106 | + return { |
| 107 | + config: ({ hmr }) => { |
| 108 | + const viteOverlay = !(typeof hmr === 'object' && hmr.overlay === false) |
| 109 | + |
| 110 | + if (pluginConfig.overlay === false || !viteOverlay) { |
| 111 | + overlay = false |
| 112 | + } |
| 113 | + }, |
| 114 | + configureServer({ root }) { |
| 115 | + invariant(pluginConfig.svelte, 'config.svelte should be `false`') |
| 116 | + let svelteRoot = root |
| 117 | + |
| 118 | + if (pluginConfig.svelte !== true && pluginConfig.svelte.root) { |
| 119 | + svelteRoot = pluginConfig.svelte.root |
| 120 | + } |
| 121 | + |
| 122 | + let watcher = new DiagnosticsWatcher(svelteRoot, overlay) |
| 123 | + return watcher |
| 124 | + }, |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +export class SvelteChecker extends Checker<'svelte'> { |
| 129 | + public constructor() { |
| 130 | + super({ |
| 131 | + name: 'svelte', |
| 132 | + absFilePath: __filename, |
| 133 | + build: { |
| 134 | + buildBin: (_config) => { |
| 135 | + return ['svelte-check', []] |
| 136 | + }, |
| 137 | + }, |
| 138 | + createDiagnostic, |
| 139 | + }) |
| 140 | + } |
| 141 | + |
| 142 | + public init() { |
| 143 | + const createServeAndBuild = super.initMainThread() |
| 144 | + module.exports.createServeAndBuild = createServeAndBuild |
| 145 | + |
| 146 | + super.initWorkerThread() |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +const svelteChecker = new SvelteChecker() |
| 151 | +svelteChecker.prepare() |
| 152 | +svelteChecker.init() |
0 commit comments