Skip to content

Commit a494073

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

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
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+

playground/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { reactClickToComponent } from "../dist/index.js";
44
import restart from "vite-plugin-restart";
55

66
export default defineConfig({
7+
base: "/base",
78
plugins: [
89
react(),
910
reactClickToComponent(),

src/client.ts

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

3939
const root = "__ROOT__";
40+
const base = "__BASE__";
41+
const originWithBase = window.location.origin + base.slice(0, -1);
4042
let currentTarget: HTMLElement | undefined;
4143
let hasMenu = false;
4244
const menuElement = document.createElement("div");
@@ -123,7 +125,9 @@ window.addEventListener("contextmenu", (event) => {
123125
spanR.textContent = layer.path.replace(`${root}/`, "");
124126
item.appendChild(spanR);
125127
item.addEventListener("click", () => {
126-
void fetch(`/__open-in-editor?file=${encodeURIComponent(layer.path)}`);
128+
void fetch(
129+
`${base}__open-in-editor?file=${encodeURIComponent(layer.path)}`,
130+
);
127131
cleanUp();
128132
});
129133
menuElement.appendChild(item);
@@ -160,6 +164,7 @@ const getLayersForElement = (element: Element) => {
160164
const layers: { name: string; path: string }[] = [];
161165
while (instance) {
162166
const path = getPath(instance);
167+
console.log(path);
163168
if (path) {
164169
const name =
165170
typeof instance.type === "string"
@@ -169,21 +174,26 @@ const getLayersForElement = (element: Element) => {
169174
?? instance.type.render?.name
170175
?? "undefined");
171176
layers.push({ name, path });
177+
} else {
178+
console.debug("Couldn't find a React instance for the element", instance);
172179
}
173180
instance = instance._debugOwner;
174181
}
175-
176182
return layers;
177183
};
178184

179185
const getPath = (fiber: Fiber) => {
180-
const source = fiber._debugSource ?? fiber._debugInfo;
181-
if (!source) {
182-
console.debug("Couldn't find a React instance for the element", fiber);
183-
return;
186+
if (fiber._debugSource) {
187+
const { columnNumber = 1, fileName, lineNumber = 1 } = fiber._debugSource;
188+
return `${fileName}:${lineNumber}:${columnNumber}`;
189+
} else if (fiber._debugStack) {
190+
const componentLine = fiber._debugStack.stack?.split("\n")[2];
191+
if (!componentLine) return;
192+
if (!componentLine.endsWith(")")) return;
193+
const startBracketIndex = componentLine.indexOf("(");
194+
const url = componentLine.slice(startBracketIndex + 1, -1);
195+
return url.replace(originWithBase, root);
184196
}
185-
const { columnNumber = 1, fileName, lineNumber = 1 } = source;
186-
return `${fileName}:${lineNumber}:${columnNumber}`;
187197
};
188198

189199
type Source = {
@@ -193,8 +203,8 @@ type Source = {
193203
};
194204
type Fiber = {
195205
_debugSource?: Source;
196-
_debugInfo?: Source; // Injected by React jsxDev patch for React 19
197206
_debugOwner?: Fiber;
207+
_debugStack?: Error;
198208
type:
199209
| string
200210
| { displayName?: string; name?: string; render?: () => unknown };

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)