Skip to content

Commit 78df663

Browse files
authored
(feat) support ESM configs (#930)
Config loading switches to using dynamic import statements. Commit consists of: - rework of the config loader to make sure to not load the same config twice in parallel, and to load all possible sub-svelte-configs upon tsconfig initialization, so that all configs are available synchronously to all snapshots that are retrieved. throwing out cosmiconfig in that process. - resulting tedious "make this function async" modifications since the ts service initialization is now async
1 parent 562c944 commit 78df663

26 files changed

+555
-164
lines changed

.github/workflows/CI.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ jobs:
88
steps:
99
- uses: actions/checkout@v1
1010
- uses: actions/setup-node@v1
11+
with:
12+
node-version: "12.x"
1113

1214
- name: Get yarn cache directory path
1315
id: yarn-cache-dir-path
@@ -37,6 +39,8 @@ jobs:
3739
steps:
3840
- uses: actions/checkout@v1
3941
- uses: actions/setup-node@v1
42+
with:
43+
node-version: "12.x"
4044

4145
- name: Get yarn cache directory path
4246
id: yarn-cache-dir-path

docs/preprocessors/in-general.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
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.
66

7-
> NOTE: you **cannot** use the new `import x from y` and `export const` / `export default` syntax in `svelte.config.js`.
7+
> NOTE: Prior to `svelte-check 1.4.0` / `svelte-language-server 0.13.0` / `Svelte for VS Code 104.9.0` you **cannot** use the new `import x from y` and `export const` / `export default` syntax in `svelte.config.js`.
88
99
```js
1010
// svelte.config.js

packages/language-server/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-language-server",
3-
"version": "0.12.0",
3+
"version": "0.13.0",
44
"description": "A language server for Svelte",
55
"main": "dist/src/index.js",
66
"typings": "dist/src/index",
@@ -30,10 +30,13 @@
3030
"url": "https://github.com/sveltejs/language-tools/issues"
3131
},
3232
"homepage": "https://github.com/sveltejs/language-tools#readme",
33+
"engines": {
34+
"node": ">= 12.0.0"
35+
},
3336
"devDependencies": {
3437
"@tsconfig/node12": "^1.0.0",
35-
"@types/cosmiconfig": "^6.0.0",
3638
"@types/estree": "^0.0.42",
39+
"@types/glob": "^7.1.1",
3740
"@types/lodash": "^4.14.116",
3841
"@types/mocha": "^7.0.2",
3942
"@types/node": "^13.9.0",
@@ -47,8 +50,8 @@
4750
},
4851
"dependencies": {
4952
"chokidar": "^3.4.1",
50-
"cosmiconfig": "^7.0.0",
5153
"estree-walker": "^2.0.1",
54+
"glob": "^7.1.6",
5255
"lodash": "^4.17.19",
5356
"prettier": "2.2.1",
5457
"prettier-plugin-svelte": "~2.2.0",

packages/language-server/src/lib/documents/Document.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { urlToPath } from '../../utils';
22
import { WritableDocument } from './DocumentBase';
33
import { extractScriptTags, extractStyleTag, TagInformation } from './utils';
44
import { parseHtml } from './parseHtml';
5-
import { SvelteConfig, loadConfig } from './configLoader';
5+
import { SvelteConfig, configLoader } from './configLoader';
66
import { HTMLDocument } from 'vscode-html-languageservice';
77

88
/**
@@ -13,23 +13,42 @@ export class Document extends WritableDocument {
1313
scriptInfo: TagInformation | null = null;
1414
moduleScriptInfo: TagInformation | null = null;
1515
styleInfo: TagInformation | null = null;
16-
config!: SvelteConfig;
16+
configPromise: Promise<SvelteConfig | undefined>;
17+
config?: SvelteConfig;
1718
html!: HTMLDocument;
1819

1920
constructor(public url: string, public content: string) {
2021
super();
22+
this.configPromise = configLoader.awaitConfig(this.getFilePath() || '');
2123
this.updateDocInfo();
2224
}
2325

2426
private updateDocInfo() {
25-
if (!this.config || this.config.loadConfigError) {
26-
this.config = loadConfig(this.getFilePath() || '');
27-
}
2827
this.html = parseHtml(this.content);
2928
const scriptTags = extractScriptTags(this.content, this.html);
30-
this.scriptInfo = this.addDefaultLanguage(scriptTags?.script || null, 'script');
31-
this.moduleScriptInfo = this.addDefaultLanguage(scriptTags?.moduleScript || null, 'script');
32-
this.styleInfo = this.addDefaultLanguage(extractStyleTag(this.content, this.html), 'style');
29+
const update = (config: SvelteConfig | undefined) => {
30+
this.config = config;
31+
this.scriptInfo = this.addDefaultLanguage(config, scriptTags?.script || null, 'script');
32+
this.moduleScriptInfo = this.addDefaultLanguage(
33+
config,
34+
scriptTags?.moduleScript || null,
35+
'script'
36+
);
37+
this.styleInfo = this.addDefaultLanguage(
38+
config,
39+
extractStyleTag(this.content, this.html),
40+
'style'
41+
);
42+
};
43+
44+
const config = configLoader.getConfig(this.getFilePath() || '');
45+
if (config && !config.loadConfigError) {
46+
update(config);
47+
} else {
48+
this.configPromise = configLoader.awaitConfig(this.getFilePath() || '');
49+
update(undefined);
50+
this.configPromise.then((c) => update(c));
51+
}
3352
}
3453

3554
/**
@@ -76,17 +95,19 @@ export class Document extends WritableDocument {
7695
}
7796

7897
private addDefaultLanguage(
98+
config: SvelteConfig | undefined,
7999
tagInfo: TagInformation | null,
80100
tag: 'style' | 'script'
81101
): TagInformation | null {
82-
if (!tagInfo) {
83-
return null;
102+
if (!tagInfo || !config) {
103+
return tagInfo;
84104
}
85105

86-
const defaultLang = Array.isArray(this.config.preprocess)
87-
? this.config.preprocess.find((group) => group.defaultLanguages?.[tag])
88-
?.defaultLanguages?.[tag]
89-
: this.config.preprocess?.defaultLanguages?.[tag];
106+
const defaultLang = Array.isArray(config.preprocess)
107+
? config.preprocess.find((group) => group.defaultLanguages?.[tag])?.defaultLanguages?.[
108+
tag
109+
]
110+
: config.preprocess?.defaultLanguages?.[tag];
90111

91112
if (!tagInfo.attributes.lang && !tagInfo.attributes.type && defaultLang) {
92113
tagInfo.attributes.lang = defaultLang;

0 commit comments

Comments
 (0)