Skip to content

Commit

Permalink
(fix) don't load ambient typings in non-svelte-projects (#1193)
Browse files Browse the repository at this point in the history
Hopefully solves part of #1149
  • Loading branch information
dummdidumm authored Oct 3, 2021
1 parent c1c61e6 commit 26d41c9
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/typescript-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import type ts from 'typescript/lib/tsserverlibrary';
function init(modules: { typescript: typeof ts }) {
function create(info: ts.server.PluginCreateInfo) {
const logger = new Logger(info.project.projectService.logger);

if (!isSvelteProject(info)) {
if (!isSvelteProject(info.project.getCompilerOptions())) {
logger.log('Detected that this is not a Svelte project, abort patching TypeScript');
return info.languageService;
}
Expand All @@ -34,6 +33,10 @@ function init(modules: { typescript: typeof ts }) {
}

function getExternalFiles(project: ts.server.ConfiguredProject) {
if (!isSvelteProject(project.getCompilerOptions())) {
return [];
}

// Needed so the ambient definitions are known inside the tsx files
const svelteTsPath = dirname(require.resolve('svelte2tsx'));
const svelteTsxFiles = [
Expand All @@ -58,9 +61,8 @@ function init(modules: { typescript: typeof ts }) {
}
}

function isSvelteProject(info: ts.server.PluginCreateInfo) {
function isSvelteProject(compilerOptions: ts.CompilerOptions) {
// Add more checks like "no Svelte file found" or "no config file found"?
const compilerOptions = info.project.getCompilerOptions();
const isNoJsxProject =
(!compilerOptions.jsx || compilerOptions.jsx === modules.typescript.JsxEmit.Preserve) &&
(!compilerOptions.jsxFactory || compilerOptions.jsxFactory.startsWith('svelte')) &&
Expand Down

0 comments on commit 26d41c9

Please sign in to comment.