From 627f0a1a528cf244fce2fe3687a5a4be2e28c97b Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 14 Feb 2025 11:00:57 +0100 Subject: [PATCH 01/11] fix: keep resolved root dir paths as is --- src/resolver.ts | 8 +++++++- tests/fixture/mapped/foo.ts | 2 ++ tests/fixture/resolveInRootDir.ts | 3 +++ tests/fixture/vite.config.ts | 13 ++++++++++++- tests/plugin.test.ts | 5 +++++ 5 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 tests/fixture/mapped/foo.ts create mode 100644 tests/fixture/resolveInRootDir.ts diff --git a/src/resolver.ts b/src/resolver.ts index 51bb3cd..07a80c4 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -1,5 +1,6 @@ import { execFile } from "node:child_process"; import process from "node:process"; +import path from "node:path"; import { fileURLToPath } from "node:url"; import { execAsync } from "./utils.js"; @@ -182,7 +183,12 @@ export async function resolveViteSpecifier( cache.set(resolved.id, resolved); // Vite can load this - if (resolved.loader === null) return resolved.id; + if ( + resolved.loader === null || + !path.relative(root, resolved.id).startsWith(".") + ) { + return resolved.id; + } // We must load it return toDenoSpecifier(resolved.loader, id, resolved.id); diff --git a/tests/fixture/mapped/foo.ts b/tests/fixture/mapped/foo.ts new file mode 100644 index 0000000..70776b0 --- /dev/null +++ b/tests/fixture/mapped/foo.ts @@ -0,0 +1,2 @@ +// will be transformed by a plugin +export const value = "it doesn't work"; diff --git a/tests/fixture/resolveInRootDir.ts b/tests/fixture/resolveInRootDir.ts new file mode 100644 index 0000000..96c4ad0 --- /dev/null +++ b/tests/fixture/resolveInRootDir.ts @@ -0,0 +1,3 @@ +import { value } from "mapped/foo.ts"; + +console.log(value); diff --git a/tests/fixture/vite.config.ts b/tests/fixture/vite.config.ts index 17c1e7a..f8f4394 100644 --- a/tests/fixture/vite.config.ts +++ b/tests/fixture/vite.config.ts @@ -1,8 +1,18 @@ import { defineConfig } from "vite"; import deno from "../../src/index"; +import path from "node:path"; export default defineConfig({ - plugins: [deno()], + plugins: [deno(), { + name: "mapped-transform", + // @ts-ignore not sure + transform(code, id) { + if (id.startsWith("\0")) return; + if (!id.includes("mapped") || path.basename(id) !== "foo.ts") return; + + return code.replace("it doesn't work", "it works"); + }, + }], build: { lib: { formats: ["es"], @@ -17,6 +27,7 @@ export default defineConfig({ inlineNpm: "inlineNpm.ts", inlineJsr: "inlineJsr.ts", inlineHttp: "inlineHttp.ts", + resolveInRootDir: "resolveInRootDir.ts", }, }, }, diff --git a/tests/plugin.test.ts b/tests/plugin.test.ts index daf4e0a..6c6f986 100644 --- a/tests/plugin.test.ts +++ b/tests/plugin.test.ts @@ -61,4 +61,9 @@ describe("Deno plugin", () => { await runTest(`inlineHttp.js`); }); }); + + // https://github.com/denoland/deno-vite-plugin/issues/42 + it("resolve to file in root dir", async () => { + await runTest(`resolveInRootDir.js`); + }); }); From 1819e254c58fee5042ef9b56993e5265eda9cd3f Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 14 Feb 2025 11:11:14 +0100 Subject: [PATCH 02/11] test --- .github/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dda5f87..0a780b1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,6 +37,9 @@ jobs: - run: npm i - run: npm run build --if-present + - name: Install fixture deps + working-directory: ./tests/fixture + run: deno install - name: Build fixture working-directory: ./tests/fixture run: npx vite build --debug From 6d90752d51aa00a2f8c8702514f9f3d2ee7c71e2 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 14 Feb 2025 11:16:16 +0100 Subject: [PATCH 03/11] debug --- src/resolver.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/resolver.ts b/src/resolver.ts index 07a80c4..825eb63 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -187,6 +187,7 @@ export async function resolveViteSpecifier( resolved.loader === null || !path.relative(root, resolved.id).startsWith(".") ) { + console.log("kepp", JSON.stringify(id), resolved); return resolved.id; } From 7d56c4aa36c82cc019e39c2af5a4a35845132b5f Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 14 Feb 2025 11:19:11 +0100 Subject: [PATCH 04/11] debug --- src/resolver.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/resolver.ts b/src/resolver.ts index 825eb63..57edfc1 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -187,7 +187,12 @@ export async function resolveViteSpecifier( resolved.loader === null || !path.relative(root, resolved.id).startsWith(".") ) { - console.log("kepp", JSON.stringify(id), resolved); + console.log( + "keep", + JSON.stringify(id), + path.relative(root, resolved.id), + resolved, + ); return resolved.id; } From 1f8e45a8ddf643320ed5680a29a8eefd543912be Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 14 Feb 2025 11:19:28 +0100 Subject: [PATCH 05/11] debug --- src/resolver.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/resolver.ts b/src/resolver.ts index 57edfc1..d4e0228 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -190,6 +190,7 @@ export async function resolveViteSpecifier( console.log( "keep", JSON.stringify(id), + root, path.relative(root, resolved.id), resolved, ); From 7c384ff0dad16352a09c475adbd6801b3bb9c4fa Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 14 Feb 2025 11:21:55 +0100 Subject: [PATCH 06/11] fix --- src/resolver.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/resolver.ts b/src/resolver.ts index d4e0228..2e472c0 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -185,7 +185,8 @@ export async function resolveViteSpecifier( // Vite can load this if ( resolved.loader === null || - !path.relative(root, resolved.id).startsWith(".") + resolved.id.startsWith(root) && + !path.relative(root, resolved.id).startsWith(".") ) { console.log( "keep", From 1c74a0d8030056f8646850b3ceb12be2a375b285 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 14 Feb 2025 11:24:09 +0100 Subject: [PATCH 07/11] try again --- src/resolver.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/resolver.ts b/src/resolver.ts index 2e472c0..d4e0228 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -185,8 +185,7 @@ export async function resolveViteSpecifier( // Vite can load this if ( resolved.loader === null || - resolved.id.startsWith(root) && - !path.relative(root, resolved.id).startsWith(".") + !path.relative(root, resolved.id).startsWith(".") ) { console.log( "keep", From bc9058faefc8363c5175cd3a484294b9fd522173 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 14 Feb 2025 11:27:45 +0100 Subject: [PATCH 08/11] try --- src/resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resolver.ts b/src/resolver.ts index d4e0228..fc42671 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -189,8 +189,8 @@ export async function resolveViteSpecifier( ) { console.log( "keep", - JSON.stringify(id), root, + resolved.id, path.relative(root, resolved.id), resolved, ); From b50bda76e24f543c34f9a890e524c6291b3c8ae8 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 14 Feb 2025 11:31:33 +0100 Subject: [PATCH 09/11] debug --- src/resolver.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/resolver.ts b/src/resolver.ts index fc42671..103b398 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -188,11 +188,12 @@ export async function resolveViteSpecifier( !path.relative(root, resolved.id).startsWith(".") ) { console.log( - "keep", - root, - resolved.id, - path.relative(root, resolved.id), - resolved, + { + root, + resolved: resolved.id, + rootr: path.resolve(root), + resolvedR: path.resolve(resolved.id), + }, ); return resolved.id; } From 030fa8b88252b701ea6405d6ada75a758736e570 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 14 Feb 2025 11:33:27 +0100 Subject: [PATCH 10/11] try --- src/resolver.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/resolver.ts b/src/resolver.ts index 103b398..6e213e6 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -185,7 +185,8 @@ export async function resolveViteSpecifier( // Vite can load this if ( resolved.loader === null || - !path.relative(root, resolved.id).startsWith(".") + resolved.id.startsWith(path.resolve(root)) && + !path.relative(root, resolved.id).startsWith(".") ) { console.log( { From 126bb7e10a1f87b2747d82c3eb096c8fa02f4153 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 14 Feb 2025 11:35:10 +0100 Subject: [PATCH 11/11] try --- .github/workflows/ci.yml | 3 --- src/resolver.ts | 8 -------- 2 files changed, 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0a780b1..dda5f87 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,9 +37,6 @@ jobs: - run: npm i - run: npm run build --if-present - - name: Install fixture deps - working-directory: ./tests/fixture - run: deno install - name: Build fixture working-directory: ./tests/fixture run: npx vite build --debug diff --git a/src/resolver.ts b/src/resolver.ts index 6e213e6..a70720a 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -188,14 +188,6 @@ export async function resolveViteSpecifier( resolved.id.startsWith(path.resolve(root)) && !path.relative(root, resolved.id).startsWith(".") ) { - console.log( - { - root, - resolved: resolved.id, - rootr: path.resolve(root), - resolvedR: path.resolve(resolved.id), - }, - ); return resolved.id; }