-
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: copy global types into hidden folder in IDE contexts (#2561)
Since the global types now reference `svelte/...` imports, they need to be placed in the same context the users' Svelte package lives in. Else these imports would load the types of the Svelte version that comes bundled with the IDE. #2493 (side note: we had this problem before in #2109 when loading `svelte/elements`, but that solution is not applicable here)
- Loading branch information
1 parent
02b6b06
commit 6180537
Showing
9 changed files
with
114 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
dist/ | ||
.vscode/ | ||
node_modules/ | ||
!test/plugins/typescript/features/diagnostics/fixtures/exports-map-svelte/node_modules/ | ||
!test/plugins/typescript/features/diagnostics/fixtures/exports-map-svelte/node_modules/package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { basename, dirname, join, resolve } from 'path'; | ||
import type ts from 'typescript'; | ||
|
||
/** | ||
* Returns the path to the global svelte2tsx files that should be included in the project. | ||
* Creates a hidden folder in the user's node_modules if `hiddenFolderPath` is provided. | ||
*/ | ||
export function get_global_types( | ||
tsSystem: ts.System, | ||
isSvelte3: boolean, | ||
sveltePath: string, | ||
typesPath: string, | ||
hiddenFolderPath?: string | ||
): string[] { | ||
const svelteHtmlPath = isSvelte3 ? undefined : join(sveltePath, 'svelte-html.d.ts'); | ||
const svelteHtmlPathExists = svelteHtmlPath && tsSystem.fileExists(svelteHtmlPath); | ||
const svelteHtmlFile = svelteHtmlPathExists ? svelteHtmlPath : './svelte-jsx-v4.d.ts'; | ||
|
||
let svelteTsxFiles: string[]; | ||
if (isSvelte3) { | ||
svelteTsxFiles = ['./svelte-shims.d.ts', './svelte-jsx.d.ts', './svelte-native-jsx.d.ts']; | ||
} else { | ||
svelteTsxFiles = ['./svelte-shims-v4.d.ts', './svelte-native-jsx.d.ts']; | ||
if (!svelteHtmlPathExists) { | ||
svelteTsxFiles.push(svelteHtmlPath); | ||
} | ||
} | ||
svelteTsxFiles = svelteTsxFiles.map((f) => tsSystem.resolvePath(resolve(typesPath, f))); | ||
|
||
if (hiddenFolderPath) { | ||
try { | ||
// IDE context - the `import('svelte')` statements inside the d.ts files will load the Svelte version of | ||
// the extension, which can cause all sorts of problems. Therefore put the files into a hidden folder in | ||
// the user's node_modules, preferably next to the Svelte package. | ||
let path = dirname(sveltePath); | ||
|
||
if (!tsSystem.directoryExists(resolve(path, 'node_modules'))) { | ||
path = hiddenFolderPath; | ||
|
||
while (path && !tsSystem.directoryExists(resolve(path, 'node_modules'))) { | ||
const parent = dirname(path); | ||
if (path === parent) { | ||
path = ''; | ||
break; | ||
} | ||
path = parent; | ||
} | ||
} | ||
|
||
if (path) { | ||
const hiddenPath = resolve(path, 'node_modules/.svelte2tsx-language-server-files'); | ||
const newFiles = []; | ||
for (const f of svelteTsxFiles) { | ||
const hiddenFile = resolve(hiddenPath, basename(f)); | ||
const existing = tsSystem.readFile(hiddenFile); | ||
const toWrite = tsSystem.readFile(f) || ''; | ||
if (existing !== toWrite) { | ||
tsSystem.writeFile(hiddenFile, toWrite); | ||
} | ||
newFiles.push(hiddenFile); | ||
} | ||
svelteTsxFiles = newFiles; | ||
} | ||
} catch (e) {} | ||
} | ||
|
||
if (svelteHtmlPathExists) { | ||
svelteTsxFiles.push(tsSystem.resolvePath(resolve(typesPath, svelteHtmlFile))); | ||
} | ||
|
||
return svelteTsxFiles; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters