sveltekit-autoimport icon indicating copy to clipboard operation
sveltekit-autoimport copied to clipboard

VSCode support

Open NextReactionary opened this issue 2 years ago • 8 comments

I absolutely love not having to constantly manage my imports (very Swift-like), but - sadly - working with this plugin results in a somewhat cumbersome workflow due to the lack of IDE support. In particular: while it's possible to suppress Svelte's compiler warnings and VSCode's built-in auto importing features, you end up without any component IntelliSense due to the latter.

Has anyone managed to create a nice workflow for themselves despite this? I'd love to hear about your setup.

NextReactionary avatar Aug 01 '22 21:08 NextReactionary

imho this would be my number 1 issue, my first priority, to get this being absolutely awesome!

timephy avatar Aug 07 '22 22:08 timephy

This workaround works "fine", but it's a hassle, not only finding the video in the first place... https://www.youtube.com/watch?v=JXvKBtTPr64

It should given some vague idea of what is out there and therefore possible. But it should be way nicer than this to have it usable...

timephy avatar Aug 07 '22 22:08 timephy

Summary of https://www.youtube.com/watch?v=JXvKBtTPr64 to work with TS/VS Code:

In your src/global.d.ts add a declaration for each of your component that you want to auto-import:

declare const YourComponentName: typeof import('path/to/your/components/YourComponentName.svelte')['default']

In your .vscode/settings.json add:

    "svelte.plugin.svelte.compilerWarnings": {
        "missing-declaration": "ignore"
    }

Restart svelte language server for the changes to take effect immediately (search and execute from cmd palette).

devidw avatar Sep 20 '22 07:09 devidw

Putting the same autoimport() function in preprocess of svelte.config.js will fix the warnings:

/* svelte.config.js */

import autoImport from 'sveltekit-autoimport';

export default {
  preprocess: [
    autoImport({
      components: [
        './src/components',
      ],
    }),
  ],
} 

yuanchuan avatar Dec 16 '22 02:12 yuanchuan

@yuanchuan is this still accurate? I moved our autoImport from the vite.config to the svelte config and it won't find our components. Having our preprocess in our vite config is working, but svelte-check can't find the components and throws a warning, even with the d.ts file.

import preprocess from 'svelte-preprocess'
import adapter from '@sveltejs/adapter-node'
import nested from 'postcss-nested'
import postcssEnvFunction from 'postcss-env-function'
import minmax from 'postcss-media-minmax'
import postcssCustomMedia from 'postcss-custom-media'
import atImport from 'postcss-import'
import autoImport from 'sveltekit-autoimport'

/** @type {import('@sveltejs/kit').Config} */
const config = {
	preprocess: [
		autoImport({
			components: ['./src/elements'],

			module: {
				svelte: ['onMount'],
			},
		})
	],

	// ssr: process.env.VITE_ENV === 'production',
	kit: {
		adapter: adapter(),
		// ssr: process.env.VITE_ENV !== 'testing',
	},
}

export default config

stolinski avatar Jan 05 '23 20:01 stolinski

@stolinski That's strange, maybe it was the situation when the syntax plugin in the editor didn't work when I switched configurations using VSCode, I took it as it being able to recognize it.

But I tried it again just now and the result was the same as yours!

yuanchuan avatar Jan 06 '23 10:01 yuanchuan

Summary of https://www.youtube.com/watch?v=JXvKBtTPr64 to work with TS/VS Code:

In your src/global.d.ts add a declaration for each of your component that you want to auto-import:

declare const YourComponentName: typeof import('path/to/your/components/YourComponentName.svelte')['default']

In your .vscode/settings.json add:

    "svelte.plugin.svelte.compilerWarnings": {
        "missing-declaration": "ignore"
    }

Restart svelte language server for the changes to take effect immediately (search and execute from cmd palette).

The unimport plugin has an option which generates another d.ts with the same global declarations and makes vscode play nice. Viable solution here?

yfi avatar Feb 23 '23 16:02 yfi

The unimport plugin has an option which generates another d.ts with the same global declarations and makes vscode play nice. Viable solution here?

They even have a preset for svelte.

https://github.com/unjs/unimport/blob/main/src/presets/svelte.ts

marfalkov avatar Aug 28 '23 15:08 marfalkov