From a3b0f225b4423f1687405b4145130360f59c59d4 Mon Sep 17 00:00:00 2001 From: Jan-Henrik Damaschke Date: Wed, 2 Oct 2024 20:55:11 +0200 Subject: [PATCH] fix: Adjusted url join to avoid double slashes --- pnpm-lock.yaml | 3 --- src/resolve.ts | 2 +- test/resolve.test.ts | 41 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 74732fb..7aa6b51 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1278,12 +1278,10 @@ packages: glob@7.2.3: resolution: {integrity: sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==} - deprecated: Glob versions prior to v9 are no longer supported glob@8.1.0: resolution: {integrity: sha512-r8hpEjiQEYlF2QU0df3dS+nxxSIreXQS1qRhMJM0Q5NDdR386C7jb7Hwwod8Fgiuex+k0GFjgft18yvxm5XoCQ==} engines: {node: '>=12'} - deprecated: Glob versions prior to v9 are no longer supported globals@11.12.0: resolution: {integrity: sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==} @@ -1369,7 +1367,6 @@ packages: inflight@1.0.6: resolution: {integrity: sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==} - deprecated: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful. inherits@2.0.4: resolution: {integrity: sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==} diff --git a/src/resolve.ts b/src/resolve.ts index ceb9416..6c72f17 100644 --- a/src/resolve.ts +++ b/src/resolve.ts @@ -122,7 +122,7 @@ function _resolve(id: string | URL, options: ResolveOptions = {}): string { for (const prefix of ["", "/index"]) { for (const extension of options.extensions || DEFAULT_EXTENSIONS) { resolved = _tryModuleResolve( - id + prefix + extension, + joinURL(id, prefix) + extension, url, conditionsSet, ); diff --git a/test/resolve.test.ts b/test/resolve.test.ts index e23750b..13cd5ee 100644 --- a/test/resolve.test.ts +++ b/test/resolve.test.ts @@ -1,6 +1,7 @@ import { existsSync } from "node:fs"; -import { describe, it, expect } from "vitest"; +import { describe, it, expect, vi, afterEach } from "vitest"; import { resolveSync, resolvePathSync, fileURLToPath } from "../src"; +import { parseFilename } from "ufo"; const tests = [ // Resolve to path @@ -14,6 +15,44 @@ const tests = [ { input: "/non/existent", action: "throws" }, ] as const; +afterEach(() => { + vi.restoreAllMocks(); +}); + +const { mockedResolve } = await vi.hoisted(async () => { + const importMetaResolve = await vi.importActual< + Record unknown> + >("import-meta-resolve"); + return { + mockedResolve: vi.fn((id, url, conditions) => { + return importMetaResolve.moduleResolve(id, url, conditions); + }), + }; +}); + +vi.mock("import-meta-resolve", () => { + return { + moduleResolve: mockedResolve, + }; +}); + +describe("tryModuleResolve", () => { + it("should create correct url", () => { + expect(() => + resolvePathSync("tslib/", { + url: import.meta.url.replace( + parseFilename(import.meta.url, { strict: false }) || "", + "", + ), + }), + ).toThrow(); + expect(mockedResolve).toHaveBeenCalled(); + expect( + mockedResolve.mock.calls.some((call) => call[0].includes("//")), + ).toBe(false); + }); +}); + describe("resolveSync", () => { for (const test of tests) { it(`${test.input} should ${test.action}`, () => {