Skip to content

Commit

Permalink
fix: show ts errors in the terminal with --incremental & bump volar
Browse files Browse the repository at this point in the history
  • Loading branch information
wangshunnn committed Apr 5, 2024
1 parent da5b9dd commit 78daab3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 72 deletions.
12 changes: 3 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
"mpx-tsc": "bin/mpx-tsc.js"
},
"dependencies": {
"@volar/language-core": "2.2.0-alpha.2",
"@volar/typescript": "2.2.0-alpha.2",
"@volar/language-core": "2.2.0-alpha.5",
"@volar/typescript": "2.2.0-alpha.5",
"@vue/compiler-dom": "^3.4.21",
"@vue/language-core": "2.0.7",
"@vue/language-core": "2.0.10",
"computeds": "^0.0.1",
"minimatch": "^9.0.3",
"path-browserify": "^1.0.1",
Expand All @@ -54,11 +54,5 @@
"@types/node": "latest",
"@types/path-browserify": "^1.0.2",
"vitest": "latest"
},
"pnpm": {
"overrides": {
"@volar/language-core": "2.2.0-alpha.2",
"@volar/typescript": "2.2.0-alpha.2"
}
}
}
39 changes: 17 additions & 22 deletions pnpm-lock.yaml

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

63 changes: 25 additions & 38 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as vue from "@vue/language-core";
import { runTsc } from "@volar/typescript/lib/quickstart/runTsc";
import type * as TypeTS from "typescript";
import { createVueLanguagePlugin } from "./languageModule";

const windowsPathReg = /\\/g;
Expand All @@ -22,11 +21,30 @@ export function run() {
).vueOptions
: vue.resolveVueCompilerOptions({});
vueOptions.extensions = runExtensions;
const fakeGlobalTypesHolder = createFakeGlobalTypesHolder(options);

const writeFile = options.host!.writeFile.bind(options.host);
options.host!.writeFile = (fileName, contents, ...args) => {
if (
fileName.endsWith(".d.ts") &&
vueLanguagePlugin
.getCanonicalFileName(fileName.replace(windowsPathReg, "/"))
.slice(0, -5) ===
vueLanguagePlugin.pluginContext.globalTypesHolder
) {
contents = removeEmitGlobalTypes(contents);
}
return writeFile(fileName, contents, ...args);
};

const vueLanguagePlugin = createVueLanguagePlugin(
ts,
(id) => id,
(fileName) => fileName === fakeGlobalTypesHolder,
options.host?.useCaseSensitiveFileNames?.() ?? false,
() => "",
() =>
options.rootNames.map((rootName) =>
rootName.replace(windowsPathReg, "/")
),
options.options,
vueOptions,
false
Expand All @@ -48,42 +66,11 @@ export function run() {
}
}

export function createFakeGlobalTypesHolder(
options: TypeTS.CreateProgramOptions
) {
const firstVueFile = options.rootNames.find((fileName) =>
fileName.endsWith(".mpx")
export function removeEmitGlobalTypes(dts: string) {
return dts.replace(
/[^\n]*__VLS_globalTypesStart[\w\W]*__VLS_globalTypesEnd[^\n]*\n/,
""
);
if (firstVueFile) {
const fakeFileName = firstVueFile + "__VLS_globalTypes.mpx";

(options.rootNames as string[]).push(fakeFileName);

const fileExists = options.host!.fileExists.bind(options.host);
const readFile = options.host!.readFile.bind(options.host);
const writeFile = options.host!.writeFile.bind(options.host);

options.host!.fileExists = (fileName) => {
if (fileName.endsWith("__VLS_globalTypes.mpx")) {
return true;
}
return fileExists(fileName);
};
options.host!.readFile = (fileName) => {
if (fileName.endsWith("__VLS_globalTypes.mpx")) {
return '<script setup lang="ts"></script>';
}
return readFile(fileName);
};
options.host!.writeFile = (fileName, ...args) => {
if (fileName.endsWith("__VLS_globalTypes.mpx.d.ts")) {
return;
}
return writeFile(fileName, ...args);
};

return fakeFileName.replace(windowsPathReg, "/");
}
}

export function resolveCommonLanguageId(fileNameOrUri: string) {
Expand Down
28 changes: 25 additions & 3 deletions src/languageModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,21 @@ function getFileRegistryKey(
return JSON.stringify(values);
}

interface _Plugin extends LanguagePlugin<VueGeneratedCode> {
getCanonicalFileName: (fileName: string) => string;
pluginContext: Parameters<VueLanguagePlugin>[0];
}

export function createVueLanguagePlugin(
ts: typeof import("typescript"),
getFileName: (fileId: string) => string,
isValidGlobalTypesHolder: (fileName: string) => boolean,
useCaseSensitiveFileNames: boolean,
getProjectVersion: () => string,
getScriptFileNames: () => string[] | Set<string>,
compilerOptions: ts.CompilerOptions,
vueCompilerOptions: VueCompilerOptions,
codegenStack: boolean = false
): LanguagePlugin<VueGeneratedCode> {
): _Plugin {
const allowLanguageIds = new Set(["mpx"]);
const pluginContext: Parameters<VueLanguagePlugin>[0] = {
modules: {
Expand All @@ -93,13 +100,28 @@ export function createVueLanguagePlugin(
allowLanguageIds.add("html");
}

const getCanonicalFileName = useCaseSensitiveFileNames
? (fileName: string) => fileName
: (fileName: string) => fileName.toLowerCase();
let canonicalRootFileNames = new Set<string>();
let canonicalRootFileNamesVersion: string | undefined;

return {
getCanonicalFileName,
pluginContext,
createVirtualCode(fileId, languageId, snapshot) {
if (allowLanguageIds.has(languageId)) {
const fileName = getFileName(fileId);
const projectVersion = getProjectVersion();
if (projectVersion !== canonicalRootFileNamesVersion) {
canonicalRootFileNames = new Set(
[...getScriptFileNames()].map(getCanonicalFileName)
);
canonicalRootFileNamesVersion = projectVersion;
}
if (
!pluginContext.globalTypesHolder &&
isValidGlobalTypesHolder(fileName)
canonicalRootFileNames.has(getCanonicalFileName(fileName))
) {
pluginContext.globalTypesHolder = fileName;
}
Expand Down

0 comments on commit 78daab3

Please sign in to comment.