Skip to content

Commit 57d8299

Browse files
authored
fix(typescript): resolve the shim used for tsc in Typescript v5.7 and up (#252)
1 parent 960b838 commit 57d8299

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

packages/typescript/lib/quickstart/runTsc.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as fs from 'fs';
2+
import * as path from 'path';
23
import type * as ts from 'typescript';
34
import type { Language, LanguagePlugin } from '@volar/language-core';
45

@@ -18,25 +19,37 @@ export function runTsc(
1819
) {
1920
getLanguagePlugins = _getLanguagePlugins;
2021

22+
let extraSupportedExtensions: string[];
23+
let extraExtensionsToRemove: string[];
24+
25+
if (Array.isArray(options)) {
26+
extraSupportedExtensions = options;
27+
extraExtensionsToRemove = [];
28+
}
29+
else {
30+
extraSupportedExtensions = options.extraSupportedExtensions;
31+
extraExtensionsToRemove = options.extraExtensionsToRemove;
32+
}
33+
2134
const proxyApiPath = require.resolve('../node/proxyCreateProgram');
2235
const readFileSync = fs.readFileSync;
2336

2437
(fs as any).readFileSync = (...args: any[]) => {
2538
if (args[0] === tscPath) {
2639
let tsc = (readFileSync as any)(...args) as string;
27-
28-
let extraSupportedExtensions: string[];
29-
let extraExtensionsToRemove: string[];
30-
if (Array.isArray(options)) {
31-
extraSupportedExtensions = options;
32-
extraExtensionsToRemove = [];
33-
}
34-
else {
35-
extraSupportedExtensions = options.extraSupportedExtensions;
36-
extraExtensionsToRemove = options.extraExtensionsToRemove;
40+
try {
41+
return transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove, __filename, typescriptObject);
42+
} catch {
43+
// Support the tsc shim used in Typescript v5.7 and up
44+
const requireRegex = /module\.exports\s*=\s*require\((?:"|')(?<path>\.\/\w+\.js)(?:"|')\)/;
45+
const requirePath = requireRegex.exec(tsc)?.groups?.path;
46+
if (requirePath) {
47+
tsc = readFileSync(path.join(path.dirname(tscPath), requirePath), 'utf8');
48+
return transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove, __filename, typescriptObject);
49+
} else {
50+
throw new Error('Failed to locate tsc module path from shim');
51+
}
3752
}
38-
39-
return transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove, __filename, typescriptObject);
4053
}
4154
return (readFileSync as any)(...args);
4255
};
@@ -112,7 +125,7 @@ function replace(text: string, ...[search, replace]: Parameters<String['replace'
112125
text = text.replace(search, replace);
113126
const after = text;
114127
if (after === before) {
115-
throw 'Search string not found: ' + JSON.stringify(search.toString());
128+
throw new Error('Failed to replace: ' + search);
116129
}
117130
return after;
118131
}

0 commit comments

Comments
 (0)