Skip to content

Commit d6bda3f

Browse files
committed
revert exports map stuff from #2478
1 parent ab81ce0 commit d6bda3f

File tree

6 files changed

+29
-62
lines changed

6 files changed

+29
-62
lines changed

packages/language-server/src/plugins/typescript/module-loader.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -293,22 +293,6 @@ export function createSvelteModuleLoader(
293293

294294
const snapshot = getSnapshot(resolvedFileName);
295295

296-
// Align with TypeScript behavior: If the Svelte file is not using TypeScript,
297-
// mark it as unresolved so that people need to provide a .d.ts file.
298-
// For backwards compatibility we're not doing this for files from packages
299-
// without an exports map, because that may break too many existing projects.
300-
if (
301-
resolvedModule.isExternalLibraryImport &&
302-
// TODO check what happens if this resolves to a real .d.svelte.ts file
303-
resolvedModule.extension === '.d.svelte.ts' && // this tells us it's from an exports map
304-
snapshot.scriptKind !== ts.ScriptKind.TS
305-
) {
306-
return {
307-
...resolvedModuleWithFailedLookup,
308-
resolvedModule: undefined
309-
};
310-
}
311-
312296
const resolvedSvelteModule: ts.ResolvedModuleFull = {
313297
extension: getExtensionFromScriptKind(snapshot && snapshot.scriptKind),
314298
resolvedFileName,

packages/language-server/src/plugins/typescript/service.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -736,13 +736,6 @@ async function createLanguageService(
736736
}
737737
}
738738

739-
// Necessary to be able to resolve export maps that only contain a "svelte" condition without an accompanying "types" condition
740-
// https://www.typescriptlang.org/tsconfig/#customConditions
741-
if (!compilerOptions.customConditions?.includes('svelte')) {
742-
compilerOptions.customConditions = compilerOptions.customConditions ?? [];
743-
compilerOptions.customConditions.push('svelte');
744-
}
745-
746739
const svelteConfigDiagnostics = checkSvelteInput(parsedConfig);
747740
if (svelteConfigDiagnostics.length > 0) {
748741
docContext.reportConfigError?.({

packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/exports-map-svelte/expectedv2.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
[
2+
{
3+
"code": 2307,
4+
"message": "Cannot find module 'package' or its corresponding type declarations.",
5+
"range": {
6+
"end": {
7+
"character": 45,
8+
"line": 1
9+
},
10+
"start": {
11+
"character": 36,
12+
"line": 1
13+
}
14+
},
15+
"severity": 1,
16+
"source": "ts",
17+
"tags": []
18+
},
219
{
320
"code": 2307,
421
"message": "Cannot find module 'package/y' or its corresponding type declarations.",

packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/exports-map-svelte/input.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import DefaultSvelteWithTS from 'package';
2+
import DefaultSvelteWithTS from 'package'; // with https://github.com/sveltejs/language-tools/pull/2478 this would work; needs decision if we want that
33
import SubWithDTS from 'package/x';
44
import SubWithoutDTSAndNotTS from 'package/y';
55
</script>

packages/language-server/test/plugins/typescript/service.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ describe('service', () => {
7575
strict: true,
7676
module: ts.ModuleKind.ESNext,
7777
moduleResolution: ts.ModuleResolutionKind.Node10,
78-
target: ts.ScriptTarget.ESNext,
79-
customConditions: ['svelte']
78+
target: ts.ScriptTarget.ESNext
8079
});
8180
});
8281

@@ -186,8 +185,7 @@ describe('service', () => {
186185
moduleResolution: ts.ModuleResolutionKind.Node10,
187186
noEmit: true,
188187
skipLibCheck: true,
189-
target: ts.ScriptTarget.ESNext,
190-
customConditions: ['svelte']
188+
target: ts.ScriptTarget.ESNext
191189
});
192190
});
193191

packages/typescript-plugin/src/module-loader.ts

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import { ensureRealSvelteFilePath, isSvelteFilePath, isVirtualSvelteFilePath } f
1111
class ModuleResolutionCache {
1212
constructor(private readonly projectService: ts.server.ProjectService) {}
1313

14-
private cache = new Map<string, ts.ResolvedModuleFull | null>();
14+
private cache = new Map<string, ts.ResolvedModuleFull>();
1515

1616
/**
1717
* Tries to get a cached module.
1818
*/
19-
get(moduleName: string, containingFile: string): ts.ResolvedModuleFull | null | undefined {
19+
get(moduleName: string, containingFile: string): ts.ResolvedModuleFull | undefined {
2020
return this.cache.get(this.getKey(moduleName, containingFile));
2121
}
2222

@@ -28,14 +28,10 @@ class ModuleResolutionCache {
2828
containingFile: string,
2929
resolvedModule: ts.ResolvedModuleFull | undefined
3030
) {
31-
if (!resolvedModule && moduleName[0] === '.') {
32-
// We cache unresolved modules for non-relative imports, too, because it's very likely that they don't change
33-
// and we don't want to resolve them every time. If they do change, the original resolution mode will notice
34-
// most of the time, and the only time this would result in a stale cache entry is if a node_modules package
35-
// is added with a "svelte" condition and no "types" condition, which is rare enough.
31+
if (!resolvedModule) {
3632
return;
3733
}
38-
this.cache.set(this.getKey(moduleName, containingFile), resolvedModule ?? null);
34+
this.cache.set(this.getKey(moduleName, containingFile), resolvedModule);
3935
}
4036

4137
/**
@@ -46,7 +42,6 @@ class ModuleResolutionCache {
4642
resolvedModuleName = this.projectService.toCanonicalFileName(resolvedModuleName);
4743
this.cache.forEach((val, key) => {
4844
if (
49-
val &&
5045
this.projectService.toCanonicalFileName(val.resolvedFileName) === resolvedModuleName
5146
) {
5247
this.cache.delete(key);
@@ -146,9 +141,7 @@ export function patchModuleLoader(
146141
return resolved.map((tsResolvedModule, idx) => {
147142
const moduleName = moduleNames[idx];
148143
if (
149-
// Only recheck relative Svelte imports or unresolved non-relative paths (which hint at node_modules,
150-
// where an exports map with "svelte" but not "types" could be present)
151-
(!isSvelteFilePath(moduleName) && (moduleName[0] === '.' || tsResolvedModule)) ||
144+
!isSvelteFilePath(moduleName) ||
152145
// corresponding .d.ts files take precedence over .svelte files
153146
tsResolvedModule?.resolvedFileName.endsWith('.d.ts') ||
154147
tsResolvedModule?.resolvedFileName.endsWith('.d.svelte.ts')
@@ -174,8 +167,7 @@ export function patchModuleLoader(
174167
const svelteResolvedModule = typescript.resolveModuleName(
175168
name,
176169
containingFile,
177-
// customConditions makes the TS algorithm look at the "svelte" condition in exports maps
178-
{ ...compilerOptions, customConditions: ['svelte'] },
170+
compilerOptions,
179171
svelteSys
180172
// don't set mode or else .svelte imports couldn't be resolved
181173
).resolvedModule;
@@ -238,9 +230,7 @@ export function patchModuleLoader(
238230
const resolvedModule = tsResolvedModule.resolvedModule;
239231

240232
if (
241-
// Only recheck relative Svelte imports or unresolved non-relative paths (which hint at node_modules,
242-
// where an exports map with "svelte" but not "types" could be present)
243-
(!isSvelteFilePath(moduleName) && (moduleName[0] === '.' || resolvedModule)) ||
233+
!isSvelteFilePath(moduleName) ||
244234
// corresponding .d.ts files take precedence over .svelte files
245235
resolvedModule?.resolvedFileName.endsWith('.d.ts') ||
246236
resolvedModule?.resolvedFileName.endsWith('.d.svelte.ts')
@@ -260,29 +250,14 @@ export function patchModuleLoader(
260250
options: ts.CompilerOptions
261251
) {
262252
const cachedModule = moduleCache.get(moduleName, containingFile);
263-
if (typeof cachedModule === 'object') {
253+
if (cachedModule) {
264254
return {
265-
resolvedModule: cachedModule ?? undefined
255+
resolvedModule: cachedModule
266256
};
267257
}
268258

269259
const resolvedModule = resolveSvelteModuleName(moduleName, containingFile, options);
270260

271-
// Align with TypeScript behavior: If the Svelte file is not using TypeScript,
272-
// mark it as unresolved so that people need to provide a .d.ts file.
273-
// For backwards compatibility we're not doing this for files from packages
274-
// without an exports map, because that may break too many existing projects.
275-
if (
276-
resolvedModule?.isExternalLibraryImport && // TODO how to check this is not from a non-exports map?
277-
// TODO check what happens if this resolves to a real .d.svelte.ts file
278-
resolvedModule.extension === '.ts' // this tells us it's from an exports map
279-
) {
280-
moduleCache.set(moduleName, containingFile, undefined);
281-
return {
282-
resolvedModule: undefined
283-
};
284-
}
285-
286261
moduleCache.set(moduleName, containingFile, resolvedModule);
287262
return {
288263
resolvedModule: resolvedModule

0 commit comments

Comments
 (0)