Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: keep resolved root dir paths as is #43

Merged
merged 11 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`);
});
});