|
1 |
| -function init(modules: { typescript: typeof import('typescript/lib/tsserverlibrary') }) { |
| 1 | +import { dirname, resolve } from 'path'; |
| 2 | +import { decorateLanguageService } from './language-service'; |
| 3 | +import { Logger } from './logger'; |
| 4 | +import { patchModuleLoader } from './module-loader'; |
| 5 | +import { SvelteSnapshotManager } from './svelte-snapshots'; |
| 6 | +import type ts from 'typescript/lib/tsserverlibrary'; |
| 7 | + |
| 8 | +function init(modules: { typescript: typeof ts }) { |
2 | 9 | function create(info: ts.server.PluginCreateInfo) {
|
3 |
| - // TODO |
| 10 | + const logger = new Logger(info.project.projectService.logger); |
| 11 | + logger.log('Starting Svelte plugin'); |
| 12 | + |
| 13 | + const snapshotManager = new SvelteSnapshotManager( |
| 14 | + modules.typescript, |
| 15 | + info.project.projectService, |
| 16 | + logger, |
| 17 | + !!info.project.getCompilerOptions().strict |
| 18 | + ); |
| 19 | + |
| 20 | + patchCompilerOptions(info.project); |
| 21 | + patchModuleLoader( |
| 22 | + logger, |
| 23 | + snapshotManager, |
| 24 | + modules.typescript, |
| 25 | + info.languageServiceHost, |
| 26 | + info.project |
| 27 | + ); |
| 28 | + return decorateLanguageService(info.languageService, snapshotManager, logger); |
4 | 29 | }
|
5 | 30 |
|
6 | 31 | function getExternalFiles(project: ts.server.ConfiguredProject) {
|
7 |
| - // TODO |
| 32 | + // Needed so the ambient definitions are known inside the tsx files |
| 33 | + const svelteTsPath = dirname(require.resolve('svelte2tsx')); |
| 34 | + const svelteTsxFiles = [ |
| 35 | + './svelte-shims.d.ts', |
| 36 | + './svelte-jsx.d.ts', |
| 37 | + './svelte-native-jsx.d.ts' |
| 38 | + ].map((f) => modules.typescript.sys.resolvePath(resolve(svelteTsPath, f))); |
| 39 | + return svelteTsxFiles; |
| 40 | + } |
| 41 | + |
| 42 | + function patchCompilerOptions(project: ts.server.Project) { |
| 43 | + const compilerOptions = project.getCompilerOptions(); |
| 44 | + // Patch needed because svelte2tsx creates jsx/tsx files |
| 45 | + compilerOptions.jsx = modules.typescript.JsxEmit.Preserve; |
| 46 | + |
| 47 | + // detect which JSX namespace to use (svelte | svelteNative) if not specified or not compatible |
| 48 | + if (!compilerOptions.jsxFactory || !compilerOptions.jsxFactory.startsWith('svelte')) { |
| 49 | + // Default to regular svelte, this causes the usage of the "svelte.JSX" namespace |
| 50 | + // We don't need to add a switch for svelte-native because the jsx is only relevant |
| 51 | + // within Svelte files, which this plugin does not deal with. |
| 52 | + compilerOptions.jsxFactory = 'svelte.createElement'; |
| 53 | + } |
8 | 54 | }
|
9 | 55 |
|
10 | 56 | return { create, getExternalFiles };
|
|
0 commit comments