From 3d17ceaa1cd16bc8569ebd1757e2c816581aa6e0 Mon Sep 17 00:00:00 2001 From: Katja Lutz Date: Fri, 24 Jul 2020 20:07:25 +0200 Subject: [PATCH] fix: regard file extensions during path resolution (#133) Originally during the file path resolution, file extensions were removed without explicit reason. This commit changes the resolution logic to keep file extensions, with the goal to add support for modern non-js extensions like cjs. The change however keeps /xyz/index resolves as is as they still should resolve to /xyz. Refs 847d31475665166aa6f30dc01e759435f4fb13a4 --- src/filesystem.ts | 4 ---- src/match-path-async.ts | 7 +------ src/match-path-sync.ts | 4 +--- src/try-path.ts | 10 +++------- 4 files changed, 5 insertions(+), 20 deletions(-) diff --git a/src/filesystem.ts b/src/filesystem.ts index a6773a7..f97747f 100644 --- a/src/filesystem.ts +++ b/src/filesystem.ts @@ -81,7 +81,3 @@ export function fileExistsAsync( callback2(undefined, stats ? stats.isFile() : false); }); } - -export function removeExtension(path: string): string { - return path.substring(0, path.lastIndexOf(".")) || path; -} diff --git a/src/match-path-async.ts b/src/match-path-async.ts index f3e94c2..9673f2a 100644 --- a/src/match-path-async.ts +++ b/src/match-path-async.ts @@ -148,7 +148,6 @@ function findFirstExistingPath( return doneCallback(err); } if (exists) { - // Not sure why we don't just return the full path? Why strip it? return doneCallback(undefined, TryPath.getStrippedPath(tryPath)); } if (index === tryPaths.length - 1) { @@ -180,11 +179,7 @@ function findFirstExistingPath( return doneCallback(mainFieldErr); } if (mainFieldMappedFile) { - // Not sure why we don't just return the full path? Why strip it? - return doneCallback( - undefined, - Filesystem.removeExtension(mainFieldMappedFile) - ); + return doneCallback(undefined, mainFieldMappedFile); } // No field in package json was a valid option. Continue with the next path. diff --git a/src/match-path-sync.ts b/src/match-path-sync.ts index 2c644cf..d7f5bcc 100644 --- a/src/match-path-sync.ts +++ b/src/match-path-sync.ts @@ -118,7 +118,6 @@ function findFirstExistingPath( tryPath.type === "index" ) { if (fileExists(tryPath.path)) { - // Not sure why we don't just return the full path? Why strip it? return TryPath.getStrippedPath(tryPath); } } else if (tryPath.type === "package") { @@ -131,8 +130,7 @@ function findFirstExistingPath( fileExists ); if (mainFieldMappedFile) { - // Not sure why we don't just return the full path? Why strip it? - return Filesystem.removeExtension(mainFieldMappedFile); + return mainFieldMappedFile; } } } else { diff --git a/src/try-path.ts b/src/try-path.ts index bf72304..5ae73f5 100644 --- a/src/try-path.ts +++ b/src/try-path.ts @@ -1,7 +1,6 @@ import * as path from "path"; import { MappingEntry } from "./mapping-entry"; import { dirname } from "path"; -import { removeExtension } from "./filesystem"; export interface TryPath { readonly type: "file" | "extension" | "index" | "package"; @@ -60,15 +59,12 @@ export function getPathsToTry( return pathsToTry.length === 0 ? undefined : pathsToTry; } -// Not sure why we don't just return the full found path? export function getStrippedPath(tryPath: TryPath): string { return tryPath.type === "index" ? dirname(tryPath.path) - : tryPath.type === "file" - ? tryPath.path - : tryPath.type === "extension" - ? removeExtension(tryPath.path) - : tryPath.type === "package" + : tryPath.type === "file" || + tryPath.type === "extension" || + tryPath.type === "package" ? tryPath.path : exhaustiveTypeException(tryPath.type); }