Skip to content

Commit

Permalink
feat(vue-tsc): support for TS v5
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Nov 8, 2022
1 parent 019417a commit a59ee4e
Showing 1 changed file with 26 additions and 40 deletions.
66 changes: 26 additions & 40 deletions vue-language-tools/vue-tsc/bin/vue-tsc.js
Expand Up @@ -6,46 +6,32 @@ const tscPath = require.resolve('typescript/lib/tsc');
const proxyPath = require.resolve('../out/proxy');

fs.readFileSync = (...args) => {
if (args[0] === tscPath) {
let tsc = readFileSync(...args);

// add *.vue files to allow extensions
tsc = tsc.replace(
`ts.supportedTSExtensions = [[".ts", ".tsx", ".d.ts"], [".cts", ".d.cts"], [".mts", ".d.mts"]];`,
// `ts.supportedTSExtensions = [[".ts", ".tsx", ".d.ts"], [".cts", ".d.cts"], [".mts", ".d.mts"], [".vue", ".md", ".html"]];`,
`ts.supportedTSExtensions = [[".ts", ".tsx", ".d.ts"], [".cts", ".d.cts"], [".mts", ".d.mts"], [".vue"]];`,
);
tsc = tsc.replace(
`ts.supportedJSExtensions = [[".js", ".jsx"], [".mjs"], [".cjs"]];`,
// `ts.supportedJSExtensions = [[".js", ".jsx"], [".mjs"], [".cjs"], [".vue", ".md", ".html"]];`,
`ts.supportedJSExtensions = [[".js", ".jsx"], [".mjs"], [".cjs"], [".vue"]];`,
);
tsc = tsc.replace(
`var allSupportedExtensions = [[".ts", ".tsx", ".d.ts", ".js", ".jsx"], [".cts", ".d.cts", ".cjs"], [".mts", ".d.mts", ".mjs"]];`,
// `var allSupportedExtensions = [[".ts", ".tsx", ".d.ts", ".js", ".jsx"], [".cts", ".d.cts", ".cjs"], [".mts", ".d.mts", ".mjs"], [".vue", ".md", ".html"]];`,
`var allSupportedExtensions = [[".ts", ".tsx", ".d.ts", ".js", ".jsx"], [".cts", ".d.cts", ".cjs"], [".mts", ".d.mts", ".mjs"], [".vue"]];`,
);

// proxy createProgram apis
tsc = tsc.replace(
`function createProgram(rootNamesOrOptions, _options, _host, _oldProgram, _configFileParsingDiagnostics) {`,
`function createProgram(rootNamesOrOptions, _options, _host, _oldProgram, _configFileParsingDiagnostics) { return require(${JSON.stringify(proxyPath)}).createProgramProxy(...arguments);`,
);

// proxy tracing
tsc = tsc.replace(
`ts.startTracing = tracingEnabled.startTracing;`,
`ts.startTracing = require(${JSON.stringify(proxyPath)}).loadTsLib().startTracing;`,
);

tsc = tsc.replace(
`ts.dumpTracingLegend = tracingEnabled.dumpLegend;`,
`ts.dumpTracingLegend = require(${JSON.stringify(proxyPath)}).loadTsLib().dumpTracingLegend;`,
);

return tsc;
}
return readFileSync(...args);
if (args[0] === tscPath) {
let tsc = readFileSync(...args);

// add *.vue files to allow extensions
tryReplace(/supportedTSExtensions = .*(?=;)/, s => s + '.concat([[".vue"]])');
tryReplace(/supportedJSExtensions = .*(?=;)/, s => s + '.concat([[".vue"]])');
tryReplace(/allSupportedExtensions = .*(?=;)/, s => s + '.concat([[".vue"]])');

// proxy startTracing, dumpTracingLegend
tryReplace(/ = tracingEnabled\./g, ` = require(${JSON.stringify(proxyPath)}).loadTsLib().`);

// proxy createProgram apis
tryReplace(/function createProgram\(.+\) {/, s => s + ` return require(${JSON.stringify(proxyPath)}).createProgramProxy(...arguments);`);

return tsc;

function tryReplace(search, replace) {
const before = tsc;
tsc = tsc.replace(search, replace);
const after = tsc;
if (after === before) {
throw 'Search string not found: ' + JSON.stringify(search.toString());
}
}
}
return readFileSync(...args);
};

require(tscPath);

0 comments on commit a59ee4e

Please sign in to comment.