Skip to content

Commit

Permalink
fix: keep resolved root dir paths as is (#43)
Browse files Browse the repository at this point in the history
* fix: keep resolved root dir paths as is
  • Loading branch information
marvinhagemeister authored Feb 14, 2025
1 parent 592e243 commit 74c1bc4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/resolver.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -182,7 +183,13 @@ export async function resolveViteSpecifier(
cache.set(resolved.id, resolved);

// Vite can load this
if (resolved.loader === null) return resolved.id;
if (
resolved.loader === null ||
resolved.id.startsWith(path.resolve(root)) &&
!path.relative(root, resolved.id).startsWith(".")
) {
return resolved.id;
}

// We must load it
return toDenoSpecifier(resolved.loader, id, resolved.id);
Expand Down
2 changes: 2 additions & 0 deletions tests/fixture/mapped/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// will be transformed by a plugin
export const value = "it doesn't work";
3 changes: 3 additions & 0 deletions tests/fixture/resolveInRootDir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { value } from "mapped/foo.ts";

console.log(value);
13 changes: 12 additions & 1 deletion tests/fixture/vite.config.ts
Original file line number Diff line number Diff line change
@@ -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"],
Expand All @@ -17,6 +27,7 @@ export default defineConfig({
inlineNpm: "inlineNpm.ts",
inlineJsr: "inlineJsr.ts",
inlineHttp: "inlineHttp.ts",
resolveInRootDir: "resolveInRootDir.ts",
},
},
},
Expand Down
5 changes: 5 additions & 0 deletions tests/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
});

0 comments on commit 74c1bc4

Please sign in to comment.