Skip to content

Commit fce521e

Browse files
committed
refactor(vue-tsc): sync tsc.js override code from volar
1 parent c133cb2 commit fce521e

File tree

4 files changed

+65
-75
lines changed

4 files changed

+65
-75
lines changed

packages/vite-plugin-checker/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,6 @@
110110
"typescript": "~4.5.5",
111111
"vls": "^0.7.6",
112112
"vti": "^0.1.7",
113-
"vue-tsc": "1.0.14"
113+
"vue-tsc": "^1.0.24"
114114
}
115115
}

packages/vite-plugin-checker/src/checkers/vueTsc/prepareVueTsc.ts

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,20 @@ const _require = createRequire(import.meta.url)
1111
const _filename = fileURLToPath(import.meta.url)
1212
const _dirname = dirname(_filename)
1313

14-
let proxyPath: string
14+
let proxyApiPath: string
1515
let createProgramFunction: string
1616
try {
1717
// vue-tsc exposes the proxy in vue-tsc/out/index after v1.0.14
18-
proxyPath = _require.resolve('vue-tsc/out/index')
18+
proxyApiPath = _require.resolve('vue-tsc/out/index')
1919
createProgramFunction = 'createProgram'
2020
} catch (e) {
21+
// @deprecated
22+
// will be removed in 0.6.0
2123
// vue-tsc exposes the proxy in vue-tsc/out/proxy before v1.0.14
22-
proxyPath = _require.resolve('vue-tsc/out/proxy')
24+
proxyApiPath = _require.resolve('vue-tsc/out/proxy')
2325
createProgramFunction = 'createProgramProxy'
2426
}
2527

26-
const textToReplace: { target: string; replacement: string }[] = [
27-
{
28-
target: `ts.supportedTSExtensions = [[".ts", ".tsx", ".d.ts"], [".cts", ".d.cts"], [".mts", ".d.mts"]];`,
29-
replacement: `ts.supportedTSExtensions = [[".ts", ".tsx", ".d.ts"], [".cts", ".d.cts"], [".mts", ".d.mts"], [".vue"]];`,
30-
},
31-
{
32-
target: `ts.supportedJSExtensions = [[".js", ".jsx"], [".mjs"], [".cjs"]];`,
33-
replacement: `ts.supportedJSExtensions = [[".js", ".jsx"], [".mjs"], [".cjs"], [".vue"]];`,
34-
},
35-
36-
{
37-
target: `var allSupportedExtensions = [[".ts", ".tsx", ".d.ts", ".js", ".jsx"], [".cts", ".d.cts", ".cjs"], [".mts", ".d.mts", ".mjs"]];`,
38-
replacement: `var allSupportedExtensions = [[".ts", ".tsx", ".d.ts", ".js", ".jsx"], [".cts", ".d.cts", ".cjs"], [".mts", ".d.mts", ".mjs"], [".vue"]];`,
39-
},
40-
41-
// proxy createProgram apis
42-
{
43-
target: `function createIncrementalProgram(_a) {`,
44-
replacement: `function createIncrementalProgram(_a) { console.error('incremental mode is not yet supported'); throw 'incremental mode is not yet supported';`,
45-
},
46-
{
47-
target: `function createProgram(rootNamesOrOptions, _options, _host, _oldProgram, _configFileParsingDiagnostics) {`,
48-
replacement: `function createProgram(rootNamesOrOptions, _options, _host, _oldProgram, _configFileParsingDiagnostics) { return require(${JSON.stringify(
49-
proxyPath
50-
)}).${createProgramFunction}(...arguments);`,
51-
},
52-
{
53-
target: `ts.executeCommandLine(ts.sys, ts.noop, ts.sys.args);`,
54-
replacement: `module.exports = ts`,
55-
},
56-
]
57-
5828
export async function prepareVueTsc() {
5929
// 1. copy typescript to folder
6030
const targetTsDir = path.resolve(_dirname, 'typescript-vue-tsc')
@@ -68,7 +38,7 @@ export async function prepareVueTsc() {
6838
// check fixture versions before re-use
6939
await access(vueTscFlagFile)
7040
const fixtureFlagContent = await readFile(vueTscFlagFile, 'utf8')
71-
if (targetTsVersion === currTsVersion && fixtureFlagContent === proxyPath) {
41+
if (targetTsVersion === currTsVersion && fixtureFlagContent === proxyApiPath) {
7242
shouldBuildFixture = false
7343
}
7444
} catch (e) {
@@ -81,24 +51,45 @@ export async function prepareVueTsc() {
8151
await mkdir(targetTsDir)
8252
const sourceTsDir = path.resolve(_require.resolve('typescript'), '../..')
8353
await copy(sourceTsDir, targetTsDir)
84-
await writeFile(vueTscFlagFile, proxyPath)
54+
await writeFile(vueTscFlagFile, proxyApiPath)
8555

8656
// 2. sync modification of lib/tsc.js with vue-tsc
87-
const tscJs = _require.resolve(path.resolve(targetTsDir, 'lib/tsc.js'))
88-
await modifyFileText(tscJs, textToReplace)
57+
await overrideTscJs(_require.resolve(path.resolve(targetTsDir, 'lib/tsc.js')))
8958
}
9059

9160
return { targetTsDir }
9261
}
9362

94-
async function modifyFileText(
95-
filePath: string,
96-
textToReplace: { target: string; replacement: string }[]
97-
) {
98-
const text = await readFile(filePath, 'utf8')
99-
let newText = text
100-
for (const { target, replacement } of textToReplace) {
101-
newText = newText.replace(target, replacement)
63+
async function overrideTscJs(tscJsPath: string) {
64+
let result = await readFile(tscJsPath, 'utf8')
65+
// #region copied from https://github.com/johnsoncodehk/volar/blob/54f7186485d79bc0e9b7ec59ecbc01d681ee5310/vue-language-tools/vue-tsc/bin/vue-tsc.js
66+
const tryReplace = (search: RegExp | string, replace: string | ((v: string) => string)) => {
67+
const before = result
68+
// @ts-ignore
69+
result = result.replace(search, replace)
70+
if (before === result) {
71+
throw 'Search string not found: ' + JSON.stringify(search.toString())
72+
}
10273
}
103-
await writeFile(filePath, newText)
74+
75+
// add *.vue files to allow extensions
76+
tryReplace(/supportedTSExtensions = .*(?=;)/, (s) => s + '.concat([[".vue"]])')
77+
tryReplace(/supportedJSExtensions = .*(?=;)/, (s) => s + '.concat([[".vue"]])')
78+
tryReplace(/allSupportedExtensions = .*(?=;)/, (s) => s + '.concat([[".vue"]])')
79+
80+
// proxy startTracing, dumpTracingLegend
81+
tryReplace(/ = tracingEnabled\./g, ` = require(${JSON.stringify(proxyApiPath)}).loadTsLib().`)
82+
83+
// proxy createProgram apis
84+
tryReplace(
85+
/function createProgram\(.+\) {/,
86+
(s) =>
87+
s + ` return require(${JSON.stringify(proxyApiPath)}).${createProgramFunction}(...arguments);` // tweak for compatibility, will be removed in 0.6.0
88+
)
89+
// #endregion
90+
91+
// change tsc command to module.exports
92+
tryReplace(`ts.executeCommandLine(ts.sys, ts.noop, ts.sys.args);`, `module.exports = ts`)
93+
94+
await writeFile(tscJsPath, result)
10495
}

playground/vue-tsc-vue3/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
"typescript": "~4.5.5",
1717
"vite": "^3.0.4",
1818
"vite-plugin-checker": "workspace:*",
19-
"vue-tsc": "^1.0.14"
19+
"vue-tsc": "^1.0.24"
2020
}
2121
}

pnpm-lock.yaml

Lines changed: 24 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)