Skip to content

Commit

Permalink
(fix) nodenext resolution for svelte files (#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Jun 19, 2022
1 parent e3951f8 commit 09cd61c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
Expand Up @@ -73,10 +73,16 @@ class ImpliedNodeFormatResolver {
private alreadyResolved = new Map<string, ReturnType<typeof ts.getModeForResolutionAtIndex>>();

resolve(
importPath: string,
importIdxInFile: number,
sourceFile: ts.SourceFile | undefined,
compilerOptions: ts.CompilerOptions
) {
if (isSvelteFilePath(importPath)) {
// Svelte imports should use the old resolution algorithm, else they are not found
return undefined;
}

let mode = undefined;
if (sourceFile) {
if (!sourceFile.impliedNodeFormat && isSvelteFilePath(sourceFile.fileName)) {
Expand Down Expand Up @@ -167,6 +173,7 @@ export function createSvelteModuleLoader(
index: number
): ts.ResolvedModule | undefined {
const mode = impliedNodeFormatResolver.resolve(
name,
index,
containingSourceFile,
compilerOptions
Expand Down
@@ -1,6 +1,6 @@
[
{
"range": { "start": { "line": 4, "character": 22 }, "end": { "line": 4, "character": 29 } },
"range": { "start": { "line": 5, "character": 22 }, "end": { "line": 5, "character": 29 } },
"severity": 1,
"source": "ts",
"message": "Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './bar.js'?",
Expand Down
@@ -1,6 +1,6 @@
[
{
"range": { "start": { "line": 4, "character": 22 }, "end": { "line": 4, "character": 29 } },
"range": { "start": { "line": 5, "character": 22 }, "end": { "line": 5, "character": 29 } },
"severity": 1,
"source": "ts",
"message": "Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './bar.js'?",
Expand Down
@@ -1,8 +1,9 @@
<script lang="ts">
// valid
import { foo } from './bar.js';
import Other from './other.svelte';
// invalid
import { baz } from './bar';
foo;baz;
foo;baz;Other;
</script>
@@ -0,0 +1,8 @@
<script lang="ts">
// valid
import { foo } from './bar.js';
// invalid
import { baz } from './bar';
foo;baz;
</script>
13 changes: 7 additions & 6 deletions packages/typescript-plugin/src/module-loader.ts
Expand Up @@ -121,26 +121,27 @@ export function patchModuleLoader(
return cachedModule;
}

const resolvedModule = resolveModuleName(fileName, containingFile, compilerOptions);
const resolvedModule = resolveSvelteModuleName(
fileName,
containingFile,
compilerOptions
);
moduleCache.set(fileName, containingFile, resolvedModule);
return resolvedModule;
});
}

function resolveModuleName(
function resolveSvelteModuleName(
name: string,
containingFile: string,
compilerOptions: ts.CompilerOptions
): ts.ResolvedModule | undefined {
// If we were to do this correctly, we'd need to check the module resolution mode for
// Node16/NodeNext in order to determine whether or not someone needs to have .js file
// endings in relative import paths. But because we don't do diagnostics within Svelte
// files in this TS plugin, we can ignore this and deal with some false positive resolutions
const svelteResolvedModule = typescript.resolveModuleName(
name,
containingFile,
compilerOptions,
svelteSys
// don't set mode or else .svelte imports couldn't be resolved
).resolvedModule;
if (
!svelteResolvedModule ||
Expand Down

0 comments on commit 09cd61c

Please sign in to comment.