Skip to content

Commit eb19ef1

Browse files
authored
(docs) restructure, more faq/troubleshooting (#275)
* (docs) restructure, more faq/troubleshooting - The docs now live at `docs`, to prevent duplication - Added troubleshooting/faq section to typescript - Added link to docs in `svelte-check` #271, #272 * bump svelte-preprocess version
1 parent 5224fd6 commit eb19ef1

File tree

9 files changed

+233
-203
lines changed

9 files changed

+233
-203
lines changed

docs/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Svelte Language Server
2+
3+
Powering `svelte-check`, `Svelte for VS Code` and other IDE extensions who use it.
4+
5+
## Setup
6+
7+
Do you want to use TypeScript/SCSS/Less/..? See [Using with preprocessors](#using-with-preprocessors).
8+
9+
### Using with preprocessors
10+
11+
#### Language specific setup
12+
13+
- [SCSS/Less](./preprocessors/scss-less.md)
14+
- [TypeScript](./preprocessors/typescript.md)
15+
16+
#### Generic setup
17+
18+
If a svelte file contains some language other than `html`, `css` or `javascript`, `svelte-vscode` needs to know how to [preprocess](https://svelte.dev/docs#svelte_preprocess) it. This can be achieved by creating a `svelte.config.js` file at the root of your project which exports a svelte options object (similar to `svelte-loader` and `rollup-plugin-svelte`). It's recommended to use the official [svelte-preprocess](https://github.com/sveltejs/svelte-preprocess) package which can handle many languages.
19+
20+
```js
21+
// svelte.config.js - NOTE: you cannot use the new "import x from y" and "export const" syntax in here.
22+
const sveltePreprocess = require('svelte-preprocess');
23+
24+
module.exports = {
25+
preprocess: sveltePreprocess(),
26+
// ...other svelte options
27+
};
28+
```
29+
30+
It's also necessary to add a `type="text/language-name"` or `lang="language-name"` to your `style` and `script` tags, which defines how that code should be interpreted by the extension.
31+
32+
```html
33+
<div>
34+
<h1>Hello, world!</h1>
35+
</div>
36+
37+
<style type="text/scss">
38+
div {
39+
h1 {
40+
color: red;
41+
}
42+
}
43+
</style>
44+
```
45+
46+
## Troubleshooting / FAQ
47+
48+
### Using TypeScript? See [this section](./preprocessors/typescript.md#troubleshooting--faq)
49+
50+
### Using SCSS or Less? See [this section](./preprocessors/scss-less.md#troubleshooting--faq)
51+
52+
## Internals
53+
54+
- [Notes about deployment](./internal/deployment.md)
File renamed without changes.

packages/svelte-vscode/docs/preprocessors/scss.md renamed to docs/preprocessors/scss-less.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ You will need to tell svelte-vscode to restart the svelte language server in ord
7070

7171
Hit `ctrl-shift-p` or `cmd-shift-p` on mac, type `svelte restart`, and select `Svelte: Restart Language Server`. Any errors you were seeing should now go away and you're now all set up!
7272

73-
## SCSS: Still having errors?
73+
## Troubleshooting / FAQ
74+
75+
### SCSS: Still having errors?
7476

7577
The `node-sass` package is very sensitive to node versions. It may be possible that this plugin runs on a different version than your application. Then it is necessary to set the `svelte.language-server.runtime` setting to the path of your node runtime. E.g. `"svelte.language-server.runtime": "/<LOCAL_PATH>/bin/node"`.

docs/preprocessors/typescript.md

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# TypeScript Support
2+
3+
## Getting it to work in the editor
4+
5+
To tell us to treat your script tags as typescript, add a `type` or `lang` attribute to your script tags like so:
6+
7+
```html
8+
<!-- Add type="text/typescript" -->
9+
<script type="text/typescript">
10+
export let name: string;
11+
</script>
12+
13+
<!-- Or add lang="typescript" or lang="ts" -->
14+
<script lang="typescript">
15+
export let name: string;
16+
</script>
17+
```
18+
19+
You may optionally want to add a `svelte.config.js` file (see below) - but it is not required as long as you only use TypeScript.
20+
21+
## Getting it to work for your build
22+
23+
For the editor, this is already enough - nothing more to do. But you also need to enhance your build config. Using Rollup, this will work with Svelte and TypeScript as long as you enable `svelte-preprocess` and `@rollup/plugin-typescript`:
24+
25+
- Install these packages `npm i -D svelte-preprocess typescript tslib @rollup/plugin-typescript`
26+
- Add these lines to `rollup.config.js`:
27+
28+
```js
29+
// ...
30+
import sveltePreprocess from 'svelte-preprocess';
31+
import typescript from '@rollup/plugin-typescript';
32+
33+
// ...
34+
plugins: [
35+
svelte({
36+
// ...
37+
preprocess: sveltePreprocess(), // <--
38+
}),
39+
40+
// ...
41+
commonjs(),
42+
typescript(), // <-- added below commonjs
43+
// ...
44+
```
45+
46+
- Add a `tsconfig.json` with these lines:
47+
48+
```json
49+
{
50+
"include": ["src/**/*"],
51+
"exclude": ["node_modules/*", "__sapper__/*", "public/*"],
52+
"compilerOptions": {
53+
"moduleResolution": "node",
54+
"sourceMap": true,
55+
"target": "es2017",
56+
"types": ["svelte"]
57+
}
58+
}
59+
```
60+
61+
And this should work to enable full TypeScript checking in your Svelte files. For further discussion and a clonable template [see this issue](https://github.com/sveltejs/language-tools/issues/161).
62+
63+
## Example configuration for the editor
64+
65+
#### Using [svelte-preprocess](https://github.com/sveltejs/svelte-preprocess)
66+
67+
##### Install
68+
69+
```sh
70+
npm i -D svelte-preprocess typescript
71+
```
72+
73+
<details>
74+
<summary>Yarn</summary>
75+
76+
```sh
77+
yarn add --dev svelte-preprocess typescript
78+
```
79+
80+
</details>
81+
82+
##### Set up `svelte.config.js`
83+
84+
```js
85+
const sveltePreprocess = require('svelte-preprocess');
86+
87+
module.exports = {
88+
preprocess: sveltePreprocess(),
89+
};
90+
```
91+
92+
##### Restart the svelte language server
93+
94+
You will need to tell svelte-vscode to restart the svelte language server in order to pick up the new configuration.
95+
96+
Hit `ctrl-shift-p` or `cmd-shift-p` on mac, type `svelte restart`, and select `Svelte: Restart Language Server`. Any errors you were seeing should now go away and you're now all set up!
97+
98+
## Troubleshooting / FAQ
99+
100+
### How do I type reactive assignments? / I get an "implicitly has type 'any' error"
101+
102+
The following code may throw an error like `Variable 'show' implicitly has type 'any' in some locations where its type cannot be determined.`, if you have stricter type settings:
103+
104+
```html
105+
<script lang="typescript">
106+
export let data: { someKey: string | null };
107+
108+
$: show = !!data.someKey; // <-- `show` has type `any`
109+
</script>
110+
111+
{#if show}hey{/if}
112+
```
113+
114+
To type the variable, do this:
115+
116+
```ts
117+
let show: boolean; // <--- added above the reactive assignment
118+
$: show = !!data.someKey; // <-- `show` now has type `boolean`
119+
```
120+
121+
### How do I import interfaces into my Svelte components? I get errors after transpilation!
122+
123+
- If you use `svelte-preprocess` BELOW `v4.x` and did NOT set `transpileOnly: true`, then make sure to have at least `v3.9.3` installed, which fixes this.
124+
- If you don't use `svelte-preprocess` OR use `transpileOnly: true` (which makes transpilation faster) OR use `v4.x`, import interfaces like this: `import type { SomeInterface } from './MyModule.ts'`. You need a least TypeScript 3.8 for this.
125+
126+
### Can I use TypeScript syntax inside the template/mustache tags?
127+
128+
At the moment, you cannot. Only `script`/`style` tags are preprocessed/transpiled. See [this issue](https://github.com/sveltejs/svelte/issues/4701) for more info.
129+
130+
### I get weird type errors on my html tags
131+
132+
This may be due to some library you are using having installed typings for `react`. These are picked up by the TypeScript compiler. Because we internally transform svelte to jsx, there is a clash and said error occurs.
133+
134+
![image](https://user-images.githubusercontent.com/374638/85633868-72697280-b67a-11ea-8f8c-7fe2b4702339.png)
135+
136+
The underlying [issue in TypeScript](https://github.com/microsoft/TypeScript/issues/18588) is yet to be fixed but in the meantime, one way to work around it is as follows:
137+
138+
1. Add a `sink.d.ts` with content `declare module 'react' {}`
139+
2. Go to your `tsconfig.json`
140+
3. If you do not have such a setting already, enhance `compilerOptions` with `"baseUrl": "."`
141+
4. Enhance `compilerOptions` with `"paths": { "react": ["<path to your sink.d.ts, relative to the baseUrl>"] }`
142+
143+
`tsconfig.json`
144+
145+
```json
146+
{
147+
"compilerOptions": {
148+
"baseUrl": ".",
149+
"paths": {
150+
"react": ["sink.d.ts"]
151+
}
152+
}
153+
}
154+
```
155+
156+
`sink.d.ts:`
157+
158+
```ts
159+
declare module 'react';
160+
```
161+
162+
For more info see [this issue](https://github.com/sveltejs/language-tools/issues/228)

packages/language-server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"prettier-plugin-svelte": "1.1.0",
5555
"source-map": "^0.7.3",
5656
"svelte": "3.23.0",
57-
"svelte-preprocess": "~3.7.4",
57+
"svelte-preprocess": "~3.9.11",
5858
"svelte2tsx": "*",
5959
"typescript": "*",
6060
"vscode-css-languageservice": "4.1.0",

packages/svelte-check/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ Usage:
5454

5555
`--output <human|human-verbose|machine>`
5656

57+
### More docs, preprocessor setup and troubleshooting
58+
59+
[See here](/docs/README.md).
60+
5761
### Machine-Readable Output
5862

5963
Setting the `--output` to `machine` will format output in a way that is easier to read

packages/svelte-vscode/README.md

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ Provides syntax highlighting and rich intellisense for Svelte components in VS C
44

55
## Setup
66

7-
Do you want to use Typescript/SCSS/Less/..? See "Using with preprocessors" below.
8-
97
If you added `"files.associations": {"*.svelte": "html" }` to your VSCode settings, remove it.
108

9+
Do you want to use TypeScript/SCSS/Less/..? [See the docs](/docs/README.md#language-specific-setup).
10+
11+
More docs and troubleshooting: [See here](/docs/README.md).
12+
1113
## Features
1214

1315
- Svelte
@@ -37,79 +39,6 @@ If you added `"files.associations": {"*.svelte": "html" }` to your VSCode settin
3739
- Go to definition
3840
- Code Actions
3941

40-
### Using with preprocessors
41-
42-
#### Language specific setup
43-
44-
- [SCSS/Less](/packages/svelte-vscode/docs/preprocessors/scss.md)
45-
- [TypeScript](/packages/svelte-vscode/docs/preprocessors/typescript.md)
46-
47-
#### Generic setup
48-
49-
If a svelte file contains some language other than `html`, `css` or `javascript`, `svelte-vscode` needs to know how to [preprocess](https://svelte.dev/docs#svelte_preprocess) it. This can be achieved by creating a `svelte.config.js` file at the root of your project which exports a svelte options object (similar to `svelte-loader` and `rollup-plugin-svelte`). It's recommended to use the official [svelte-preprocess](https://github.com/sveltejs/svelte-preprocess) package which can handle many languages.
50-
51-
```js
52-
// svelte.config.js - NOTE: you cannot use the new "import x from y" and "export const" syntax in here.
53-
const sveltePreprocess = require('svelte-preprocess');
54-
55-
module.exports = {
56-
preprocess: sveltePreprocess(),
57-
// ...other svelte options
58-
};
59-
```
60-
61-
It's also necessary to add a `type="text/language-name"` or `lang="language-name"` to your `style` and `script` tags, which defines how that code should be interpreted by the extension.
62-
63-
```html
64-
<div>
65-
<h1>Hello, world!</h1>
66-
</div>
67-
68-
<style type="text/scss">
69-
div {
70-
h1 {
71-
color: red;
72-
}
73-
}
74-
</style>
75-
```
76-
77-
### Troubleshooting
78-
79-
#### I get weird type errors on my html tags
80-
81-
This may be due to some library you are using having installed typings for `react`. These are picked up by the TypeScript compiler. Because we internally transform svelte to jsx, there is a clash and said error occurs.
82-
83-
![image](https://user-images.githubusercontent.com/374638/85633868-72697280-b67a-11ea-8f8c-7fe2b4702339.png)
84-
85-
The underlying [issue in TypeScript](https://github.com/microsoft/TypeScript/issues/18588) is yet to be fixed but in the meantime, one way to work around it is as follows:
86-
87-
1. Add a `sink.d.ts` with content `declare module 'react' {}`
88-
2. Go to your `tsconfig.json`
89-
3. If you do not have such a setting already, enhance `compilerOptions` with `"baseUrl": "."`
90-
4. Enhance `compilerOptions` with `"paths": { "react": ["<path to your sink.d.ts, relative to the baseUrl>"] }`
91-
92-
`tsconfig.json`
93-
94-
```json
95-
{
96-
"compilerOptions": {
97-
"baseUrl": ".",
98-
"paths": {
99-
"react": ["sink.d.ts"]
100-
}
101-
}
102-
}
103-
```
104-
105-
`sink.d.ts:`
106-
107-
```ts
108-
declare module 'react';
109-
```
110-
111-
For more info see [this issue](https://github.com/sveltejs/language-tools/issues/228)
112-
11342
### Settings
11443

11544
##### `svelte.language-server.runtime`

0 commit comments

Comments
 (0)