Skip to content

Commit 74c1bc4

Browse files
fix: keep resolved root dir paths as is (#43)
* fix: keep resolved root dir paths as is
1 parent 592e243 commit 74c1bc4

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

src/resolver.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { execFile } from "node:child_process";
22
import process from "node:process";
3+
import path from "node:path";
34
import { fileURLToPath } from "node:url";
45
import { execAsync } from "./utils.js";
56

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

184185
// Vite can load this
185-
if (resolved.loader === null) return resolved.id;
186+
if (
187+
resolved.loader === null ||
188+
resolved.id.startsWith(path.resolve(root)) &&
189+
!path.relative(root, resolved.id).startsWith(".")
190+
) {
191+
return resolved.id;
192+
}
186193

187194
// We must load it
188195
return toDenoSpecifier(resolved.loader, id, resolved.id);

tests/fixture/mapped/foo.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// will be transformed by a plugin
2+
export const value = "it doesn't work";

tests/fixture/resolveInRootDir.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { value } from "mapped/foo.ts";
2+
3+
console.log(value);

tests/fixture/vite.config.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import { defineConfig } from "vite";
22
import deno from "../../src/index";
3+
import path from "node:path";
34

45
export default defineConfig({
5-
plugins: [deno()],
6+
plugins: [deno(), {
7+
name: "mapped-transform",
8+
// @ts-ignore not sure
9+
transform(code, id) {
10+
if (id.startsWith("\0")) return;
11+
if (!id.includes("mapped") || path.basename(id) !== "foo.ts") return;
12+
13+
return code.replace("it doesn't work", "it works");
14+
},
15+
}],
616
build: {
717
lib: {
818
formats: ["es"],
@@ -17,6 +27,7 @@ export default defineConfig({
1727
inlineNpm: "inlineNpm.ts",
1828
inlineJsr: "inlineJsr.ts",
1929
inlineHttp: "inlineHttp.ts",
30+
resolveInRootDir: "resolveInRootDir.ts",
2031
},
2132
},
2233
},

tests/plugin.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,9 @@ describe("Deno plugin", () => {
6161
await runTest(`inlineHttp.js`);
6262
});
6363
});
64+
65+
// https://github.com/denoland/deno-vite-plugin/issues/42
66+
it("resolve to file in root dir", async () => {
67+
await runTest(`resolveInRootDir.js`);
68+
});
6469
});

0 commit comments

Comments
 (0)