|
| 1 | +import { basename, dirname, join, resolve } from 'path'; |
| 2 | +import type ts from 'typescript'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Returns the path to the global svelte2tsx files that should be included in the project. |
| 6 | + * Creates a hidden folder in the user's node_modules if `hiddenFolderPath` is provided. |
| 7 | + */ |
| 8 | +export function get_global_types( |
| 9 | + tsSystem: ts.System, |
| 10 | + isSvelte3: boolean, |
| 11 | + sveltePath: string, |
| 12 | + typesPath: string, |
| 13 | + hiddenFolderPath?: string |
| 14 | +): string[] { |
| 15 | + const svelteHtmlPath = isSvelte3 ? undefined : join(sveltePath, 'svelte-html.d.ts'); |
| 16 | + const svelteHtmlPathExists = svelteHtmlPath && tsSystem.fileExists(svelteHtmlPath); |
| 17 | + const svelteHtmlFile = svelteHtmlPathExists ? svelteHtmlPath : './svelte-jsx-v4.d.ts'; |
| 18 | + |
| 19 | + let svelteTsxFiles: string[]; |
| 20 | + if (isSvelte3) { |
| 21 | + svelteTsxFiles = ['./svelte-shims.d.ts', './svelte-jsx.d.ts', './svelte-native-jsx.d.ts']; |
| 22 | + } else { |
| 23 | + svelteTsxFiles = ['./svelte-shims-v4.d.ts', './svelte-native-jsx.d.ts']; |
| 24 | + if (!svelteHtmlPathExists) { |
| 25 | + svelteTsxFiles.push(svelteHtmlPath); |
| 26 | + } |
| 27 | + } |
| 28 | + svelteTsxFiles = svelteTsxFiles.map((f) => tsSystem.resolvePath(resolve(typesPath, f))); |
| 29 | + |
| 30 | + if (hiddenFolderPath) { |
| 31 | + try { |
| 32 | + // IDE context - the `import('svelte')` statements inside the d.ts files will load the Svelte version of |
| 33 | + // the extension, which can cause all sorts of problems. Therefore put the files into a hidden folder in |
| 34 | + // the user's node_modules, preferably next to the Svelte package. |
| 35 | + let path = dirname(sveltePath); |
| 36 | + |
| 37 | + if (!tsSystem.directoryExists(resolve(path, 'node_modules'))) { |
| 38 | + path = hiddenFolderPath; |
| 39 | + |
| 40 | + while (path && !tsSystem.directoryExists(resolve(path, 'node_modules'))) { |
| 41 | + const parent = dirname(path); |
| 42 | + if (path === parent) { |
| 43 | + path = ''; |
| 44 | + break; |
| 45 | + } |
| 46 | + path = parent; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if (path) { |
| 51 | + const hiddenPath = resolve(path, 'node_modules/.svelte2tsx-language-server-files'); |
| 52 | + const newFiles = []; |
| 53 | + for (const f of svelteTsxFiles) { |
| 54 | + const hiddenFile = resolve(hiddenPath, basename(f)); |
| 55 | + const existing = tsSystem.readFile(hiddenFile); |
| 56 | + const toWrite = tsSystem.readFile(f) || ''; |
| 57 | + if (existing !== toWrite) { |
| 58 | + tsSystem.writeFile(hiddenFile, toWrite); |
| 59 | + } |
| 60 | + newFiles.push(hiddenFile); |
| 61 | + } |
| 62 | + svelteTsxFiles = newFiles; |
| 63 | + } |
| 64 | + } catch (e) {} |
| 65 | + } |
| 66 | + |
| 67 | + if (svelteHtmlPathExists) { |
| 68 | + svelteTsxFiles.push(tsSystem.resolvePath(resolve(typesPath, svelteHtmlFile))); |
| 69 | + } |
| 70 | + |
| 71 | + return svelteTsxFiles; |
| 72 | +} |
0 commit comments