@sallyqalawi/devlink 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -2
- package/bin/devlink.js +5 -0
- package/dist/cli.cjs +617 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +590 -0
- package/dist/cli.js.map +1 -0
- package/dist/client.iife.js +238 -0
- package/dist/client.iife.js.map +1 -0
- package/dist/index.cjs +592 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +44 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +542 -0
- package/dist/index.js.map +1 -0
- package/dist/nuxt.cjs +544 -0
- package/dist/nuxt.cjs.map +1 -0
- package/dist/nuxt.d.cts +12 -0
- package/dist/nuxt.d.ts +12 -0
- package/dist/nuxt.js +511 -0
- package/dist/nuxt.js.map +1 -0
- package/dist/unplugin-CQjGqwFT.d.cts +53 -0
- package/dist/unplugin-CQjGqwFT.d.ts +53 -0
- package/dist/vite.cjs +548 -0
- package/dist/vite.cjs.map +1 -0
- package/dist/vite.d.cts +23 -0
- package/dist/vite.d.ts +23 -0
- package/dist/vite.js +511 -0
- package/dist/vite.js.map +1 -0
- package/dist/webpack.cjs +540 -0
- package/dist/webpack.cjs.map +1 -0
- package/dist/webpack.d.cts +12 -0
- package/dist/webpack.d.ts +12 -0
- package/dist/webpack.js +507 -0
- package/dist/webpack.js.map +1 -0
- package/package.json +89 -4
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,592 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
DEVLINK_ATTR: () => DEVLINK_ATTR,
|
|
34
|
+
DEVLINK_PREFIX: () => DEVLINK_PREFIX,
|
|
35
|
+
createDevlinkBridge: () => createDevlinkBridge,
|
|
36
|
+
default: () => src_default,
|
|
37
|
+
devlinkVite: () => devlink,
|
|
38
|
+
getBridge: () => getBridge,
|
|
39
|
+
isProduction: () => isProduction,
|
|
40
|
+
normalizeLocKey: () => normalizeLocKey,
|
|
41
|
+
readPortFile: () => readPortFile,
|
|
42
|
+
resolveLoc: () => resolveLoc,
|
|
43
|
+
tagHtml: () => tagHtml,
|
|
44
|
+
tagSource: () => tagSource,
|
|
45
|
+
unplugin: () => unplugin,
|
|
46
|
+
unpluginFactory: () => unpluginFactory,
|
|
47
|
+
writePortFile: () => writePortFile
|
|
48
|
+
});
|
|
49
|
+
module.exports = __toCommonJS(src_exports);
|
|
50
|
+
|
|
51
|
+
// src/tagger/index.ts
|
|
52
|
+
var import_parser = require("@babel/parser");
|
|
53
|
+
var import_traverse = __toESM(require("@babel/traverse"), 1);
|
|
54
|
+
var import_magic_string = __toESM(require("magic-string"), 1);
|
|
55
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
|
56
|
+
|
|
57
|
+
// src/utils.ts
|
|
58
|
+
var import_node_fs = __toESM(require("fs"), 1);
|
|
59
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
60
|
+
var import_node_os = __toESM(require("os"), 1);
|
|
61
|
+
var DEVLINK_ATTR = "data-devlink";
|
|
62
|
+
var DEVLINK_PREFIX = "/__devlink";
|
|
63
|
+
var PORT_FILE = ".devlink/port";
|
|
64
|
+
function isProduction() {
|
|
65
|
+
return process.env.NODE_ENV === "production" || process.env.DEVLINK === "0" || process.env.DEVLINK === "false";
|
|
66
|
+
}
|
|
67
|
+
function toPosix(p) {
|
|
68
|
+
return p.split(import_node_path.default.sep).join("/");
|
|
69
|
+
}
|
|
70
|
+
function resolveLoc(loc, root) {
|
|
71
|
+
const match = loc.match(/^(.+?):(\d+)(?::(\d+))?$/);
|
|
72
|
+
if (!match) {
|
|
73
|
+
throw new Error(`Invalid loc format: ${loc}. Expected file:line or file:line:column`);
|
|
74
|
+
}
|
|
75
|
+
const [, filePart, lineStr, colStr] = match;
|
|
76
|
+
const file = import_node_path.default.isAbsolute(filePart) ? filePart : import_node_path.default.resolve(root, filePart);
|
|
77
|
+
return {
|
|
78
|
+
file,
|
|
79
|
+
line: Number(lineStr),
|
|
80
|
+
column: colStr ? Number(colStr) : 1
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function getPortFilePath(root) {
|
|
84
|
+
const base = root ?? process.cwd();
|
|
85
|
+
return import_node_path.default.join(base, PORT_FILE);
|
|
86
|
+
}
|
|
87
|
+
function writePortFile(port, root) {
|
|
88
|
+
const payload = JSON.stringify(
|
|
89
|
+
{ port, host: "localhost", pid: process.pid, updatedAt: Date.now() },
|
|
90
|
+
null,
|
|
91
|
+
0
|
|
92
|
+
);
|
|
93
|
+
const targets = [
|
|
94
|
+
getPortFilePath(root),
|
|
95
|
+
// Parent folder (monorepo root when Vite root is playground/)
|
|
96
|
+
getPortFilePath(import_node_path.default.resolve(root, "..")),
|
|
97
|
+
import_node_path.default.join(import_node_os.default.tmpdir(), "devlink-port.json")
|
|
98
|
+
];
|
|
99
|
+
for (const file of targets) {
|
|
100
|
+
try {
|
|
101
|
+
import_node_fs.default.mkdirSync(import_node_path.default.dirname(file), { recursive: true });
|
|
102
|
+
import_node_fs.default.writeFileSync(file, payload, "utf8");
|
|
103
|
+
} catch {
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function readPortFile(root) {
|
|
108
|
+
const candidates = [
|
|
109
|
+
getPortFilePath(root),
|
|
110
|
+
getPortFilePath(process.cwd()),
|
|
111
|
+
import_node_path.default.join(import_node_os.default.tmpdir(), "devlink-port.json")
|
|
112
|
+
];
|
|
113
|
+
for (const file of candidates) {
|
|
114
|
+
try {
|
|
115
|
+
if (import_node_fs.default.existsSync(file)) {
|
|
116
|
+
const data = JSON.parse(import_node_fs.default.readFileSync(file, "utf8"));
|
|
117
|
+
if (data.port) return { port: data.port, host: data.host ?? "localhost" };
|
|
118
|
+
}
|
|
119
|
+
} catch {
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
function normalizeLocKey(loc) {
|
|
125
|
+
const match = loc.match(/^(.+?):(\d+)(?::\d+)?$/);
|
|
126
|
+
if (!match) return loc.replace(/\\/g, "/");
|
|
127
|
+
return `${match[1].replace(/\\/g, "/")}:${match[2]}`;
|
|
128
|
+
}
|
|
129
|
+
function isJsxFile(id) {
|
|
130
|
+
return /\.[jt]sx$/.test(id) || /\.vue$/.test(id) || /\.svelte$/.test(id);
|
|
131
|
+
}
|
|
132
|
+
function isHtmlFile(id) {
|
|
133
|
+
return /\.html?$/.test(id);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/tagger/index.ts
|
|
137
|
+
var traverse = typeof import_traverse.default === "function" ? import_traverse.default : import_traverse.default.default;
|
|
138
|
+
var PARSER_PLUGINS = [
|
|
139
|
+
"jsx",
|
|
140
|
+
"typescript",
|
|
141
|
+
"classProperties",
|
|
142
|
+
"decorators-legacy",
|
|
143
|
+
"dynamicImport",
|
|
144
|
+
"importMeta",
|
|
145
|
+
"topLevelAwait"
|
|
146
|
+
];
|
|
147
|
+
function tagSource(options) {
|
|
148
|
+
const { root, filename, code } = options;
|
|
149
|
+
const relative = toPosix(import_node_path2.default.relative(root, filename));
|
|
150
|
+
if (relative.startsWith("node_modules") || relative.includes("node_modules/")) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
if (!/[.<]?\w/.test(code) || !code.includes("<") && !code.includes("jsx")) {
|
|
154
|
+
if (!/\.[jt]sx$/.test(filename)) return null;
|
|
155
|
+
}
|
|
156
|
+
let ast;
|
|
157
|
+
try {
|
|
158
|
+
ast = (0, import_parser.parse)(code, {
|
|
159
|
+
sourceType: "module",
|
|
160
|
+
plugins: [...PARSER_PLUGINS],
|
|
161
|
+
errorRecovery: true,
|
|
162
|
+
ranges: true
|
|
163
|
+
});
|
|
164
|
+
} catch {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
const s = new import_magic_string.default(code);
|
|
168
|
+
let mutated = false;
|
|
169
|
+
traverse(ast, {
|
|
170
|
+
JSXOpeningElement(nodePath) {
|
|
171
|
+
const node = nodePath.node;
|
|
172
|
+
if (!node.loc) return;
|
|
173
|
+
const hasAttr = node.attributes.some(
|
|
174
|
+
(attr) => attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && attr.name.name === DEVLINK_ATTR
|
|
175
|
+
);
|
|
176
|
+
if (hasAttr) return;
|
|
177
|
+
if (node.name.type === "JSXIdentifier" && node.name.name === "Fragment") return;
|
|
178
|
+
if (node.name.type === "JSXMemberExpression" && node.name.property.type === "JSXIdentifier" && node.name.property.name === "Fragment") {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const line = node.loc.start.line;
|
|
182
|
+
const locValue = `${relative}:${line}`;
|
|
183
|
+
const nameEnd = node.name.end;
|
|
184
|
+
if (nameEnd == null) return;
|
|
185
|
+
s.appendLeft(nameEnd, ` ${DEVLINK_ATTR}="${locValue}"`);
|
|
186
|
+
mutated = true;
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
if (!mutated && /\.html?$/.test(filename)) {
|
|
190
|
+
return tagHtml(code, relative);
|
|
191
|
+
}
|
|
192
|
+
if (!mutated) return null;
|
|
193
|
+
return {
|
|
194
|
+
code: s.toString(),
|
|
195
|
+
map: s.generateMap({
|
|
196
|
+
source: filename,
|
|
197
|
+
includeContent: true,
|
|
198
|
+
hires: true
|
|
199
|
+
})
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function tagHtml(code, relative) {
|
|
203
|
+
const skip = /* @__PURE__ */ new Set([
|
|
204
|
+
"html",
|
|
205
|
+
"head",
|
|
206
|
+
"body",
|
|
207
|
+
"script",
|
|
208
|
+
"style",
|
|
209
|
+
"meta",
|
|
210
|
+
"link",
|
|
211
|
+
"title",
|
|
212
|
+
"noscript",
|
|
213
|
+
"base",
|
|
214
|
+
"template",
|
|
215
|
+
"!doctype"
|
|
216
|
+
]);
|
|
217
|
+
const s = new import_magic_string.default(code);
|
|
218
|
+
let mutated = false;
|
|
219
|
+
const openTagRe = /<([A-Za-z][\w-]*)(\s[^>]*?)?(\/?)>/g;
|
|
220
|
+
let m;
|
|
221
|
+
while ((m = openTagRe.exec(code)) !== null) {
|
|
222
|
+
const tagName = m[1].toLowerCase();
|
|
223
|
+
if (skip.has(tagName)) continue;
|
|
224
|
+
const attrs = m[2] ?? "";
|
|
225
|
+
if (attrs.includes(DEVLINK_ATTR)) continue;
|
|
226
|
+
const before = code.slice(0, m.index);
|
|
227
|
+
const lineNum = before.split(/\r?\n/).length;
|
|
228
|
+
const insertAt = m.index + 1 + m[1].length;
|
|
229
|
+
s.appendLeft(insertAt, ` ${DEVLINK_ATTR}="${relative}:${lineNum}"`);
|
|
230
|
+
mutated = true;
|
|
231
|
+
}
|
|
232
|
+
if (!mutated) return null;
|
|
233
|
+
return {
|
|
234
|
+
code: s.toString(),
|
|
235
|
+
map: s.generateMap({ source: relative, includeContent: true, hires: true })
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// src/middleware/index.ts
|
|
240
|
+
var import_node_fs3 = __toESM(require("fs"), 1);
|
|
241
|
+
var import_node_path4 = __toESM(require("path"), 1);
|
|
242
|
+
var import_ws = require("ws");
|
|
243
|
+
var import_launch_editor = __toESM(require("launch-editor"), 1);
|
|
244
|
+
|
|
245
|
+
// src/ide-bridge.ts
|
|
246
|
+
var import_node_fs2 = __toESM(require("fs"), 1);
|
|
247
|
+
var import_node_path3 = __toESM(require("path"), 1);
|
|
248
|
+
var import_node_os2 = __toESM(require("os"), 1);
|
|
249
|
+
function readIdeBridge(root) {
|
|
250
|
+
const candidates = [
|
|
251
|
+
root ? import_node_path3.default.join(root, ".devlink/ide.json") : null,
|
|
252
|
+
import_node_path3.default.join(process.cwd(), ".devlink/ide.json"),
|
|
253
|
+
import_node_path3.default.join(process.cwd(), "../.devlink/ide.json"),
|
|
254
|
+
import_node_path3.default.join(import_node_os2.default.tmpdir(), "devlink-ide.json")
|
|
255
|
+
].filter(Boolean);
|
|
256
|
+
for (const file of candidates) {
|
|
257
|
+
try {
|
|
258
|
+
if (!import_node_fs2.default.existsSync(file)) continue;
|
|
259
|
+
const data = JSON.parse(import_node_fs2.default.readFileSync(file, "utf8"));
|
|
260
|
+
if (data.port) return { port: data.port, host: data.host ?? "127.0.0.1" };
|
|
261
|
+
} catch {
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
async function revealInIde(file, line, column, root) {
|
|
267
|
+
const ide = readIdeBridge(root);
|
|
268
|
+
if (!ide) return false;
|
|
269
|
+
const hosts = Array.from(/* @__PURE__ */ new Set([ide.host, "localhost", "127.0.0.1"]));
|
|
270
|
+
for (const host of hosts) {
|
|
271
|
+
const url = `http://${host}:${ide.port}/reveal?file=${encodeURIComponent(file)}&line=${encodeURIComponent(String(line))}&column=${encodeURIComponent(String(column))}`;
|
|
272
|
+
try {
|
|
273
|
+
const res = await fetch(url, { signal: AbortSignal.timeout(1500) });
|
|
274
|
+
if (!res.ok) continue;
|
|
275
|
+
const data = await res.json();
|
|
276
|
+
if (data.ok) return true;
|
|
277
|
+
} catch {
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// src/middleware/index.ts
|
|
284
|
+
function findClientScriptPath() {
|
|
285
|
+
const candidates = [
|
|
286
|
+
import_node_path4.default.join(process.cwd(), "node_modules/@sallyqalawi/devlink/dist/client.iife.js"),
|
|
287
|
+
import_node_path4.default.join(process.cwd(), "../packages/devlink/dist/client.iife.js"),
|
|
288
|
+
import_node_path4.default.join(process.cwd(), "packages/devlink/dist/client.iife.js"),
|
|
289
|
+
import_node_path4.default.join(process.cwd(), "dist/client.iife.js")
|
|
290
|
+
];
|
|
291
|
+
for (const c of candidates) {
|
|
292
|
+
try {
|
|
293
|
+
const resolved = import_node_path4.default.resolve(c);
|
|
294
|
+
if (import_node_fs3.default.existsSync(resolved)) return resolved;
|
|
295
|
+
} catch {
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
function loadClientScript() {
|
|
301
|
+
const scriptPath = findClientScriptPath();
|
|
302
|
+
if (scriptPath && scriptPath.endsWith(".js")) {
|
|
303
|
+
return import_node_fs3.default.readFileSync(scriptPath, "utf8");
|
|
304
|
+
}
|
|
305
|
+
return getInlineClientFallback();
|
|
306
|
+
}
|
|
307
|
+
function createDevlinkBridge(options) {
|
|
308
|
+
const root = options.root;
|
|
309
|
+
const prefix = options.prefix ?? DEVLINK_PREFIX;
|
|
310
|
+
const wsClients = /* @__PURE__ */ new Set();
|
|
311
|
+
const sseClients = /* @__PURE__ */ new Set();
|
|
312
|
+
let wss = null;
|
|
313
|
+
function broadcast(msg) {
|
|
314
|
+
const data = JSON.stringify(msg);
|
|
315
|
+
for (const client of wsClients) {
|
|
316
|
+
if (client.readyState === import_ws.WebSocket.OPEN) client.send(data);
|
|
317
|
+
}
|
|
318
|
+
const chunk = `data: ${data}
|
|
319
|
+
|
|
320
|
+
`;
|
|
321
|
+
for (const res of sseClients) {
|
|
322
|
+
try {
|
|
323
|
+
res.write(chunk);
|
|
324
|
+
} catch {
|
|
325
|
+
sseClients.delete(res);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
function flash(loc) {
|
|
330
|
+
const normalized = normalizeLocKey(loc);
|
|
331
|
+
broadcast({ type: "flash", loc: normalized });
|
|
332
|
+
for (const variant of locVariants(normalized, root)) {
|
|
333
|
+
if (variant !== normalized) broadcast({ type: "flash", loc: variant });
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
function getClientScript() {
|
|
337
|
+
return loadClientScript();
|
|
338
|
+
}
|
|
339
|
+
function injectHtml(html) {
|
|
340
|
+
if (isProduction()) return html;
|
|
341
|
+
if (html.includes("/__devlink/client.js") || html.includes("__devlink-styles")) return html;
|
|
342
|
+
const tag = `<script src="${prefix}/client.js" defer></script>`;
|
|
343
|
+
if (html.includes("</body>")) {
|
|
344
|
+
return html.replace("</body>", `${tag}
|
|
345
|
+
</body>`);
|
|
346
|
+
}
|
|
347
|
+
return html + tag;
|
|
348
|
+
}
|
|
349
|
+
function sendJson(res, status, body) {
|
|
350
|
+
res.statusCode = status;
|
|
351
|
+
res.setHeader("Content-Type", "application/json");
|
|
352
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
353
|
+
res.end(JSON.stringify(body));
|
|
354
|
+
}
|
|
355
|
+
function middleware(req, res, next) {
|
|
356
|
+
if (isProduction()) return next();
|
|
357
|
+
const url = new URL(req.url || "/", `http://${req.headers.host || "localhost"}`);
|
|
358
|
+
if (!url.pathname.startsWith(prefix)) return next();
|
|
359
|
+
if (req.method === "OPTIONS") {
|
|
360
|
+
res.statusCode = 204;
|
|
361
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
362
|
+
res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
|
|
363
|
+
res.end();
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (url.pathname === `${prefix}/client.js`) {
|
|
367
|
+
res.statusCode = 200;
|
|
368
|
+
res.setHeader("Content-Type", "application/javascript; charset=utf-8");
|
|
369
|
+
res.setHeader("Cache-Control", "no-store");
|
|
370
|
+
res.end(getClientScript());
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
if (url.pathname === `${prefix}/events`) {
|
|
374
|
+
res.writeHead(200, {
|
|
375
|
+
"Content-Type": "text/event-stream",
|
|
376
|
+
"Cache-Control": "no-cache, no-transform",
|
|
377
|
+
Connection: "keep-alive",
|
|
378
|
+
"Access-Control-Allow-Origin": "*"
|
|
379
|
+
});
|
|
380
|
+
res.write(`data: ${JSON.stringify({ type: "connected", version: "0.1.0" })}
|
|
381
|
+
|
|
382
|
+
`);
|
|
383
|
+
sseClients.add(res);
|
|
384
|
+
const keepAlive = setInterval(() => {
|
|
385
|
+
try {
|
|
386
|
+
res.write(": ping\n\n");
|
|
387
|
+
} catch {
|
|
388
|
+
clearInterval(keepAlive);
|
|
389
|
+
sseClients.delete(res);
|
|
390
|
+
}
|
|
391
|
+
}, 15e3);
|
|
392
|
+
req.on("close", () => {
|
|
393
|
+
clearInterval(keepAlive);
|
|
394
|
+
sseClients.delete(res);
|
|
395
|
+
});
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
if (url.pathname === `${prefix}/open`) {
|
|
399
|
+
const loc = url.searchParams.get("loc");
|
|
400
|
+
if (!loc) return sendJson(res, 400, { ok: false, error: "Missing loc" });
|
|
401
|
+
try {
|
|
402
|
+
const { file, line, column } = resolveLoc(loc, root);
|
|
403
|
+
if (!import_node_fs3.default.existsSync(file)) {
|
|
404
|
+
return sendJson(res, 404, { ok: false, error: `File not found: ${file}` });
|
|
405
|
+
}
|
|
406
|
+
void revealInIde(file, line, column, root).then((handled) => {
|
|
407
|
+
if (!handled) (0, import_launch_editor.default)(`${file}:${line}:${column}`);
|
|
408
|
+
});
|
|
409
|
+
return sendJson(res, 200, { ok: true, file, line, column });
|
|
410
|
+
} catch (err) {
|
|
411
|
+
return sendJson(res, 400, { ok: false, error: String(err) });
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
if (url.pathname === `${prefix}/flash`) {
|
|
415
|
+
const loc = url.searchParams.get("loc");
|
|
416
|
+
if (!loc) return sendJson(res, 400, { ok: false, error: "Missing loc" });
|
|
417
|
+
flash(loc);
|
|
418
|
+
return sendJson(res, 200, {
|
|
419
|
+
ok: true,
|
|
420
|
+
loc,
|
|
421
|
+
clients: wsClients.size + sseClients.size,
|
|
422
|
+
sse: sseClients.size,
|
|
423
|
+
ws: wsClients.size
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
if (url.pathname === `${prefix}/health`) {
|
|
427
|
+
return sendJson(res, 200, {
|
|
428
|
+
ok: true,
|
|
429
|
+
clients: wsClients.size + sseClients.size,
|
|
430
|
+
sse: sseClients.size,
|
|
431
|
+
ws: wsClients.size
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
next();
|
|
435
|
+
}
|
|
436
|
+
function attach(server) {
|
|
437
|
+
if (isProduction()) return;
|
|
438
|
+
wss = new import_ws.WebSocketServer({ noServer: true });
|
|
439
|
+
server.on("upgrade", (req, socket, head) => {
|
|
440
|
+
const url = new URL(req.url || "/", `http://${req.headers.host || "localhost"}`);
|
|
441
|
+
if (url.pathname !== `${prefix}/ws`) return;
|
|
442
|
+
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
443
|
+
wsClients.add(ws);
|
|
444
|
+
ws.on("close", () => wsClients.delete(ws));
|
|
445
|
+
ws.send(JSON.stringify({ type: "connected", version: "0.1.0" }));
|
|
446
|
+
});
|
|
447
|
+
});
|
|
448
|
+
const tryWritePort = () => {
|
|
449
|
+
const addr = server.address();
|
|
450
|
+
if (addr && typeof addr === "object" && addr.port) {
|
|
451
|
+
writePortFile(addr.port, root);
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
if (server.listening) tryWritePort();
|
|
455
|
+
else server.once("listening", tryWritePort);
|
|
456
|
+
}
|
|
457
|
+
return { middleware, attach, broadcast, flash, getClientScript, injectHtml };
|
|
458
|
+
}
|
|
459
|
+
function locVariants(loc, root) {
|
|
460
|
+
const match = loc.match(/^(.+):(\d+)$/);
|
|
461
|
+
if (!match) return [loc];
|
|
462
|
+
const [, file, line] = match;
|
|
463
|
+
const variants = /* @__PURE__ */ new Set([loc]);
|
|
464
|
+
const parts = file.split("/");
|
|
465
|
+
if (parts.length > 1) {
|
|
466
|
+
variants.add(`${parts.slice(1).join("/")}:${line}`);
|
|
467
|
+
}
|
|
468
|
+
variants.add(`${parts[parts.length - 1]}:${line}`);
|
|
469
|
+
if (import_node_path4.default.isAbsolute(file)) {
|
|
470
|
+
const rel = import_node_path4.default.relative(root, file).split(import_node_path4.default.sep).join("/");
|
|
471
|
+
if (rel && !rel.startsWith("..")) variants.add(`${rel}:${line}`);
|
|
472
|
+
}
|
|
473
|
+
return [...variants];
|
|
474
|
+
}
|
|
475
|
+
function getInlineClientFallback() {
|
|
476
|
+
return `(()=>{const A="data-devlink",P="/__devlink/open",E="/__devlink/events",HC="__devlink-highlight",FC="__devlink-flash",TID="__devlink-tooltip";let alt=false,cur=null,tip=null;function styles(){if(document.getElementById("__devlink-styles"))return;const s=document.createElement("style");s.id="__devlink-styles";s.textContent=\`.\${HC}{outline:2px solid rgba(56,189,248,.9)!important;outline-offset:2px!important;cursor:alias!important}.\${FC}{outline:2px solid rgba(52,211,153,.95)!important;outline-offset:3px!important;animation:__devlink-pulse 1.5s ease-out}@keyframes __devlink-pulse{0%{outline-offset:6px}40%{outline-offset:3px}100%{outline-color:transparent;outline-offset:0}}#\${TID}{position:fixed;z-index:2147483647;pointer-events:none;background:rgba(15,23,42,.92);color:#e2e8f0;font:12px/1.4 ui-monospace,monospace;padding:4px 8px;border-radius:4px;display:none}\`;document.head.appendChild(s)}function flash(loc){styles();const want=String(loc).replace(/\\\\/g,"/").replace(/:(\\\\d+):\\\\d+$/,":$1");const wf=want.replace(/:\\\\d+$/,"");const wl=want.slice(wf.length+1);const targets=[...document.querySelectorAll("["+A+"]")].filter(el=>{const v=(el.getAttribute(A)||"").replace(/\\\\/g,"/").replace(/:(\\\\d+):\\\\d+$/,":$1");const vf=v.replace(/:\\\\d+$/,"");const vl=v.slice(vf.length+1);return vl===wl&&(v===want||vf===wf||vf.endsWith("/"+wf)||wf.endsWith("/"+vf)||vf.split("/").pop()===wf.split("/").pop())});if(!targets.length)return;targets[0].scrollIntoView({behavior:"smooth",block:"center"});for(const el of targets){el.classList.remove(FC);void el.offsetWidth;el.classList.add(FC);setTimeout(()=>el.classList.remove(FC),1500)}}function connect(){try{const es=new EventSource(E);es.onmessage=(ev)=>{try{const m=JSON.parse(ev.data);if(m.type==="flash"&&m.loc)flash(m.loc)}catch{}};es.onerror=()=>{es.close();setTimeout(connect,2000)}}catch{setTimeout(connect,2000)}}document.addEventListener("keydown",e=>{if(e.key==="Alt")alt=true},true);document.addEventListener("keyup",e=>{if(e.key==="Alt"){alt=false;if(cur){cur.classList.remove(HC);cur=null}}},true);document.addEventListener("mousemove",e=>{if(!alt&&!e.altKey)return;alt=true;const el=e.target instanceof Element?e.target.closest("["+A+"]"):null;if(!el)return;styles();if(cur&&cur!==el)cur.classList.remove(HC);cur=el;el.classList.add(HC)},true);document.addEventListener("click",e=>{if(!e.altKey)return;const el=e.target instanceof Element?e.target.closest("["+A+"]"):null;if(!el)return;e.preventDefault();e.stopPropagation();const loc=el.getAttribute(A);if(loc)fetch(P+"?loc="+encodeURIComponent(loc)).catch(()=>{})},true);connect();window.__devlinkFlash=flash})();`;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// src/plugin/unplugin.ts
|
|
480
|
+
var import_unplugin = require("unplugin");
|
|
481
|
+
function matches(id, patterns) {
|
|
482
|
+
if (!patterns) return false;
|
|
483
|
+
const list = Array.isArray(patterns) ? patterns : [patterns];
|
|
484
|
+
return list.some((re) => re.test(id));
|
|
485
|
+
}
|
|
486
|
+
var bridgeSingleton = null;
|
|
487
|
+
function getBridge() {
|
|
488
|
+
return bridgeSingleton;
|
|
489
|
+
}
|
|
490
|
+
var unpluginFactory = (options = {}) => {
|
|
491
|
+
let enabled = options.enabled ?? !isProduction();
|
|
492
|
+
let root = options.root ?? process.cwd();
|
|
493
|
+
const filter = (id) => {
|
|
494
|
+
if (!enabled) return false;
|
|
495
|
+
if (id.includes("node_modules")) return false;
|
|
496
|
+
if (id.includes("\0")) return false;
|
|
497
|
+
if (options.exclude && matches(id, options.exclude)) return false;
|
|
498
|
+
if (options.include) return matches(id, options.include);
|
|
499
|
+
const clean = id.split("?")[0];
|
|
500
|
+
return isJsxFile(clean) || isHtmlFile(clean);
|
|
501
|
+
};
|
|
502
|
+
return {
|
|
503
|
+
name: "unplugin-devlink",
|
|
504
|
+
enforce: "pre",
|
|
505
|
+
transformInclude(id) {
|
|
506
|
+
return filter(id);
|
|
507
|
+
},
|
|
508
|
+
transform(code, id) {
|
|
509
|
+
if (!enabled) return null;
|
|
510
|
+
const filename = id.split("?")[0];
|
|
511
|
+
const result = tagSource({ root, filename, code });
|
|
512
|
+
if (!result) return null;
|
|
513
|
+
return { code: result.code, map: result.map };
|
|
514
|
+
},
|
|
515
|
+
vite: {
|
|
516
|
+
configResolved(config) {
|
|
517
|
+
root = options.root ?? config.root;
|
|
518
|
+
if (options.enabled === void 0 && config.command === "build") {
|
|
519
|
+
enabled = false;
|
|
520
|
+
}
|
|
521
|
+
if (options.enabled === void 0 && config.command === "serve") {
|
|
522
|
+
enabled = true;
|
|
523
|
+
}
|
|
524
|
+
},
|
|
525
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
526
|
+
configureServer(server) {
|
|
527
|
+
if (!enabled) return;
|
|
528
|
+
root = options.root ?? server.config.root;
|
|
529
|
+
bridgeSingleton = createDevlinkBridge({ root });
|
|
530
|
+
server.middlewares.use(bridgeSingleton.middleware);
|
|
531
|
+
const attach = () => {
|
|
532
|
+
const httpServer = server.httpServer;
|
|
533
|
+
if (httpServer) bridgeSingleton?.attach(httpServer);
|
|
534
|
+
};
|
|
535
|
+
if (server.httpServer) attach();
|
|
536
|
+
else {
|
|
537
|
+
const check = setInterval(() => {
|
|
538
|
+
if (server.httpServer) {
|
|
539
|
+
clearInterval(check);
|
|
540
|
+
attach();
|
|
541
|
+
}
|
|
542
|
+
}, 50);
|
|
543
|
+
setTimeout(() => clearInterval(check), 1e4);
|
|
544
|
+
}
|
|
545
|
+
},
|
|
546
|
+
transformIndexHtml: {
|
|
547
|
+
order: "post",
|
|
548
|
+
handler(html) {
|
|
549
|
+
if (!enabled) return html;
|
|
550
|
+
if (!bridgeSingleton) bridgeSingleton = createDevlinkBridge({ root });
|
|
551
|
+
return bridgeSingleton.injectHtml(html);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
},
|
|
555
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
556
|
+
webpack(compiler) {
|
|
557
|
+
if (options.enabled === void 0 && compiler.options.mode === "production") {
|
|
558
|
+
enabled = false;
|
|
559
|
+
}
|
|
560
|
+
if (!enabled) return;
|
|
561
|
+
root = options.root ?? compiler.options.context ?? process.cwd();
|
|
562
|
+
bridgeSingleton = createDevlinkBridge({ root });
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
};
|
|
566
|
+
var unplugin = (0, import_unplugin.createUnplugin)(unpluginFactory);
|
|
567
|
+
|
|
568
|
+
// src/vite.ts
|
|
569
|
+
function devlink(options) {
|
|
570
|
+
return unplugin.vite(options);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// src/index.ts
|
|
574
|
+
var src_default = devlink;
|
|
575
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
576
|
+
0 && (module.exports = {
|
|
577
|
+
DEVLINK_ATTR,
|
|
578
|
+
DEVLINK_PREFIX,
|
|
579
|
+
createDevlinkBridge,
|
|
580
|
+
devlinkVite,
|
|
581
|
+
getBridge,
|
|
582
|
+
isProduction,
|
|
583
|
+
normalizeLocKey,
|
|
584
|
+
readPortFile,
|
|
585
|
+
resolveLoc,
|
|
586
|
+
tagHtml,
|
|
587
|
+
tagSource,
|
|
588
|
+
unplugin,
|
|
589
|
+
unpluginFactory,
|
|
590
|
+
writePortFile
|
|
591
|
+
});
|
|
592
|
+
//# sourceMappingURL=index.cjs.map
|