@@ -11,50 +11,20 @@ const _require = createRequire(import.meta.url)
11
11
const _filename = fileURLToPath ( import . meta. url )
12
12
const _dirname = dirname ( _filename )
13
13
14
- let proxyPath : string
14
+ let proxyApiPath : string
15
15
let createProgramFunction : string
16
16
try {
17
17
// 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' )
19
19
createProgramFunction = 'createProgram'
20
20
} catch ( e ) {
21
+ // @deprecated
22
+ // will be removed in 0.6.0
21
23
// 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' )
23
25
createProgramFunction = 'createProgramProxy'
24
26
}
25
27
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
-
58
28
export async function prepareVueTsc ( ) {
59
29
// 1. copy typescript to folder
60
30
const targetTsDir = path . resolve ( _dirname , 'typescript-vue-tsc' )
@@ -68,7 +38,7 @@ export async function prepareVueTsc() {
68
38
// check fixture versions before re-use
69
39
await access ( vueTscFlagFile )
70
40
const fixtureFlagContent = await readFile ( vueTscFlagFile , 'utf8' )
71
- if ( targetTsVersion === currTsVersion && fixtureFlagContent === proxyPath ) {
41
+ if ( targetTsVersion === currTsVersion && fixtureFlagContent === proxyApiPath ) {
72
42
shouldBuildFixture = false
73
43
}
74
44
} catch ( e ) {
@@ -81,24 +51,45 @@ export async function prepareVueTsc() {
81
51
await mkdir ( targetTsDir )
82
52
const sourceTsDir = path . resolve ( _require . resolve ( 'typescript' ) , '../..' )
83
53
await copy ( sourceTsDir , targetTsDir )
84
- await writeFile ( vueTscFlagFile , proxyPath )
54
+ await writeFile ( vueTscFlagFile , proxyApiPath )
85
55
86
56
// 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' ) ) )
89
58
}
90
59
91
60
return { targetTsDir }
92
61
}
93
62
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
+ }
102
73
}
103
- await writeFile ( filePath , newText )
74
+
75
+ // add *.vue files to allow extensions
76
+ tryReplace ( / s u p p o r t e d T S E x t e n s i o n s = .* (? = ; ) / , ( s ) => s + '.concat([[".vue"]])' )
77
+ tryReplace ( / s u p p o r t e d J S E x t e n s i o n s = .* (? = ; ) / , ( s ) => s + '.concat([[".vue"]])' )
78
+ tryReplace ( / a l l S u p p o r t e d E x t e n s i o n s = .* (? = ; ) / , ( s ) => s + '.concat([[".vue"]])' )
79
+
80
+ // proxy startTracing, dumpTracingLegend
81
+ tryReplace ( / = t r a c i n g E n a b l e d \. / g, ` = require(${ JSON . stringify ( proxyApiPath ) } ).loadTsLib().` )
82
+
83
+ // proxy createProgram apis
84
+ tryReplace (
85
+ / f u n c t i o n c r e a t e P r o g r a m \( .+ \) { / ,
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 )
104
95
}
0 commit comments