Skip to content

Commit 3826f20

Browse files
committed
Add support for Vite base path
1 parent a7cae83 commit 3826f20

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Add support for Vite base path
6+
37
## 4.0.0
48

59
- Align with Vite 7: ESM only, node 20.19+

src/client.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ style.innerHTML = `[data-click-to-component-target] {
3737
document.head.appendChild(style);
3838

3939
const root = "__ROOT__";
40+
const base = "__BASE__";
4041
let currentTarget: HTMLElement | undefined;
4142
let hasMenu = false;
4243
const menuElement = document.createElement("div");
@@ -123,7 +124,9 @@ window.addEventListener("contextmenu", (event) => {
123124
spanR.textContent = layer.path.replace(`${root}/`, "");
124125
item.appendChild(spanR);
125126
item.addEventListener("click", () => {
126-
void fetch(`/__open-in-editor?file=${encodeURIComponent(layer.path)}`);
127+
void fetch(
128+
`${base}__open-in-editor?file=${encodeURIComponent(layer.path)}`,
129+
);
127130
cleanUp();
128131
});
129132
menuElement.appendChild(item);

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ import { join } from "node:path";
33
import type { PluginOption } from "vite";
44

55
let root = "";
6+
let base = "";
67

78
export const reactClickToComponent = (): PluginOption => ({
89
name: "react-click-to-component",
910
apply: "serve",
1011
configResolved(config) {
1112
root = config.root;
13+
base = config.base;
1214
},
1315
transformIndexHtml: () => [
1416
{
1517
tag: "script",
1618
attrs: { type: "module" },
17-
children: readFileSync(
18-
join(import.meta.dirname, "client.js"),
19-
"utf-8",
20-
).replace("__ROOT__", root),
19+
children: readFileSync(join(import.meta.dirname, "client.js"), "utf-8")
20+
.replace("__ROOT__", root)
21+
.replace("__BASE__", base),
2122
},
2223
],
2324
transform(code, id) {

0 commit comments

Comments
 (0)