Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) nodenext resolution for svelte files #1532

Merged
merged 1 commit into from Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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