Skip to content

Commit

Permalink
feat: support TS v5 for svelte-tsc
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Nov 27, 2022
1 parent 185ba1a commit e1cc687
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 40 deletions.
63 changes: 26 additions & 37 deletions examples/svelte-tsc/bin/svelte-tsc.js
Expand Up @@ -6,43 +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"], [".svelte"]];`,
);
tsc = tsc.replace(
`ts.supportedJSExtensions = [[".js", ".jsx"], [".mjs"], [".cjs"]];`,
`ts.supportedJSExtensions = [[".js", ".jsx"], [".mjs"], [".cjs"], [".svelte"]];`,
);
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"], [".svelte"]];`,
);

// 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 *.svelte files to allow extensions
tryReplace(/supportedTSExtensions = .*(?=;)/, s => s + '.concat([[".svelte"]])');
tryReplace(/supportedJSExtensions = .*(?=;)/, s => s + '.concat([[".svelte"]])');
tryReplace(/allSupportedExtensions = .*(?=;)/, s => s + '.concat([[".svelte"]])');

// 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);
3 changes: 3 additions & 0 deletions examples/svelte-tsc/src/proxy.ts
Expand Up @@ -13,6 +13,9 @@ export function createProgramProxy(
if (!options.options.noEmit && !options.options.emitDeclarationOnly)
return doThrow('js emit is not supported');

if (!options.options.noEmit && options.options.noEmitOnError)
return doThrow('noEmitOnError is not supported');

if (!options.host)
return doThrow('!options.host');

Expand Down
6 changes: 3 additions & 3 deletions vue-language-tools/vue-tsc/src/proxy.ts
Expand Up @@ -66,7 +66,7 @@ export function createProgramProxy(
const vueTsLs = vueTs.createLanguageService(vueLsHost);

program = vueTsLs.getProgram();
program.__VLS_ctx = ctx;
program.__vue = ctx;

function getVueCompilerOptions(): vue.VueCompilerOptions {
const tsConfig = ctx.options.options.configFilePath;
Expand Down Expand Up @@ -109,8 +109,8 @@ export function createProgramProxy(
}
}
else {
program.__VLS_ctx.options = options;
program.__VLS_ctx.projectVersion++;
program.__vue.options = options;
program.__vue.projectVersion++;
}

for (const rootName of options.rootNames) {
Expand Down

0 comments on commit e1cc687

Please sign in to comment.