@vitejs/devtools 0.0.0-alpha.2 → 0.0.0-alpha.21

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.
@@ -0,0 +1,96 @@
1
+ import { a as createApp, c as sendRedirect, d as ansis_default, i as getPort, l as toNodeListener, n as createDevToolsMiddleware, o as eventHandler, r as MARK_NODE, s as fromNodeMiddleware, t as DevTools, u as createDevToolsContext } from "./plugins-Chcj6ENu.js";
2
+ import { t as dirClientStandalone } from "./dirs-C0s1Ghvy.js";
3
+ import { resolveConfig } from "vite";
4
+ import process from "node:process";
5
+ import { existsSync } from "node:fs";
6
+ import sirv from "sirv";
7
+ import { join, relative, resolve } from "pathe";
8
+ import fs$1 from "node:fs/promises";
9
+ import { createServer } from "node:http";
10
+ import open from "open";
11
+
12
+ //#region src/node/standalone.ts
13
+ async function startStandaloneDevTools(options = {}) {
14
+ const { cwd = process.cwd(), command = "build", mode = "production" } = options;
15
+ const resolved = await resolveConfig({
16
+ configFile: options.config,
17
+ root: cwd,
18
+ plugins: [DevTools()]
19
+ }, command, mode);
20
+ dedupeVitePlugins(resolved.plugins, (plugin) => plugin.name?.startsWith("vite:devtools"));
21
+ return {
22
+ config: resolved,
23
+ context: await createDevToolsContext(resolved)
24
+ };
25
+ }
26
+ function dedupeVitePlugins(plugins, include) {
27
+ const toDelete = [];
28
+ const map = /* @__PURE__ */ new Map();
29
+ for (let i = 0; i < plugins.length; i++) {
30
+ const plugin = plugins[i];
31
+ if (!plugin || !include(plugin)) continue;
32
+ if (map.has(plugin.name)) toDelete.push(i);
33
+ else map.set(plugin.name, plugin);
34
+ }
35
+ toDelete.sort((a, b) => b - a);
36
+ for (const i of toDelete) plugins.splice(i, 1);
37
+ return plugins;
38
+ }
39
+
40
+ //#endregion
41
+ //#region src/node/cli-commands.ts
42
+ async function start(options) {
43
+ const { host } = options;
44
+ const port = await getPort({
45
+ host,
46
+ port: options.port == null ? void 0 : +options.port,
47
+ portRange: [9999, 15e3]
48
+ });
49
+ const devtools = await startStandaloneDevTools({ cwd: options.root });
50
+ const { h3 } = await createDevToolsMiddleware({
51
+ cwd: devtools.config.root,
52
+ hostWebSocket: host,
53
+ context: devtools.context
54
+ });
55
+ const app = createApp();
56
+ for (const { baseUrl, distDir } of devtools.context.views.buildStaticDirs) app.use(baseUrl, fromNodeMiddleware(sirv(distDir, {
57
+ dev: true,
58
+ single: true
59
+ })));
60
+ app.use("/.devtools/", h3.handler);
61
+ app.use("/", eventHandler(async (event) => {
62
+ if (event.node.req.url === "/") return sendRedirect(event, "/.devtools/");
63
+ }));
64
+ createServer(toNodeListener(app)).listen(port, host, async () => {
65
+ console.log(ansis_default.green`${MARK_NODE} Vite DevTools started at`, ansis_default.green(`http://${host === "127.0.0.1" ? "localhost" : host}:${port}`), "\n");
66
+ if (options.open) await open(`http://${host === "127.0.0.1" ? "localhost" : host}:${port}`);
67
+ });
68
+ }
69
+ async function build(options) {
70
+ console.log(ansis_default.cyan`${MARK_NODE} Building static Vite DevTools...`);
71
+ const devtools = await startStandaloneDevTools({
72
+ cwd: options.root,
73
+ config: options.config
74
+ });
75
+ const outDir = resolve(devtools.config.root, options.outDir);
76
+ if (existsSync(outDir)) await fs$1.rm(outDir, { recursive: true });
77
+ const devToolsRoot = join(outDir, ".devtools");
78
+ await fs$1.mkdir(devToolsRoot, { recursive: true });
79
+ await fs$1.cp(dirClientStandalone, devToolsRoot, { recursive: true });
80
+ for (const { baseUrl, distDir } of devtools.context.views.buildStaticDirs) {
81
+ console.log(ansis_default.cyan`${MARK_NODE} Copying static files from ${distDir} to ${join(outDir, baseUrl)}`);
82
+ await fs$1.mkdir(join(outDir, baseUrl), { recursive: true });
83
+ await fs$1.cp(distDir, join(outDir, baseUrl), { recursive: true });
84
+ }
85
+ await fs$1.mkdir(resolve(devToolsRoot, "api"), { recursive: true });
86
+ await fs$1.writeFile(resolve(devToolsRoot, ".vdt-connection.json"), JSON.stringify({ backend: "static" }, null, 2), "utf-8");
87
+ console.log(ansis_default.cyan`${MARK_NODE} Writing RPC dump to ${resolve(devToolsRoot, ".vdt-rpc-dump.json")}`);
88
+ const dump = {};
89
+ for (const [key, value] of Object.entries(devtools.context.rpc.functions)) if (value.type === "static") dump[key] = await value.handler?.();
90
+ await fs$1.writeFile(resolve(devToolsRoot, ".vdt-rpc-dump.json"), JSON.stringify(dump, null, 2), "utf-8");
91
+ console.log(ansis_default.green`${MARK_NODE} Built to ${relative(devtools.config.root, outDir)}`);
92
+ throw new Error("[Vite DevTools] Build mode of Vite DevTools is not yet complete");
93
+ }
94
+
95
+ //#endregion
96
+ export { start as n, build as t };
@@ -0,0 +1,18 @@
1
+ //#region src/node/cli-commands.d.ts
2
+ interface StartOptions {
3
+ root?: string;
4
+ config?: string;
5
+ host: string;
6
+ port?: string | number;
7
+ open?: boolean;
8
+ }
9
+ declare function start(options: StartOptions): Promise<void>;
10
+ interface BuildOptions {
11
+ root: string;
12
+ config?: string;
13
+ outDir: string;
14
+ base: string;
15
+ }
16
+ declare function build(options: BuildOptions): Promise<void>;
17
+ //#endregion
18
+ export { BuildOptions, StartOptions, build, start };
@@ -0,0 +1,4 @@
1
+ import "./plugins-Chcj6ENu.js";
2
+ import { n as start, t as build } from "./cli-commands-CkG1gOFc.js";
3
+
4
+ export { build, start };
package/dist/cli.js CHANGED
@@ -1,82 +1,15 @@
1
- import { MARK_NODE, ansis_default, createApp, createDevToolsContext, createDevToolsMiddleware, eventHandler, getPort, sendRedirect, toNodeListener } from "./server-B_q0HJ88.js";
2
- import { dirClientStandalone } from "./dirs-B7dOX6eI.js";
3
- import { existsSync } from "node:fs";
1
+ import "./plugins-Chcj6ENu.js";
2
+ import { n as start, t as build } from "./cli-commands-CkG1gOFc.js";
4
3
  import process from "node:process";
5
- import { loadConfigFromFile, resolveConfig } from "vite";
6
- import fs from "node:fs/promises";
7
- import { createServer } from "node:http";
8
4
  import cac from "cac";
9
- import open from "open";
10
- import { join, relative, resolve } from "pathe";
11
5
 
12
- //#region src/node/standalone.ts
13
- async function startStandaloneDevTools(options = {}) {
14
- const { cwd = process.cwd(), command = "build", mode = "production" } = options;
15
- const loaded = await loadConfigFromFile({
16
- command,
17
- mode
18
- }, options.config, cwd);
19
- const resolved = await resolveConfig(loaded?.config || {}, command, mode);
20
- const context = await createDevToolsContext(resolved);
21
- return {
22
- config: resolved,
23
- context
24
- };
25
- }
26
-
27
- //#endregion
28
6
  //#region src/node/cli.ts
29
7
  const cli = cac("vite-devtools");
30
8
  process.on("SIGINT", () => {
31
9
  process.exit(0);
32
10
  });
33
- cli.command("build", "Build devtools with current config file for static hosting").option("--root <root>", "Root directory", { default: process.cwd() }).option("--config <config>", "Vite config file").option("--base <baseURL>", "Base URL for deployment", { default: "/" }).option("--outDir <dir>", "Output directory", { default: ".vite-devtools" }).action(async (options) => {
34
- console.log(ansis_default.cyan`${MARK_NODE} Building static Vite DevTools...`);
35
- const devtools = await startStandaloneDevTools({
36
- cwd: options.root,
37
- config: options.config
38
- });
39
- const outDir = resolve(devtools.config.root, options.outDir);
40
- if (existsSync(outDir)) await fs.rm(outDir, { recursive: true });
41
- const devToolsRoot = join(outDir, "__vite_devtools__");
42
- await fs.mkdir(devToolsRoot, { recursive: true });
43
- await fs.cp(dirClientStandalone, devToolsRoot, { recursive: true });
44
- for (const { baseUrl, distDir } of devtools.context.staticDirs) {
45
- console.log(ansis_default.cyan`${MARK_NODE} Copying static files from ${distDir} to ${join(outDir, baseUrl)}`);
46
- await fs.mkdir(join(outDir, baseUrl), { recursive: true });
47
- await fs.cp(distDir, join(outDir, baseUrl), { recursive: true });
48
- }
49
- await fs.mkdir(resolve(devToolsRoot, "api"), { recursive: true });
50
- await fs.writeFile(resolve(devToolsRoot, "api/connection.json"), JSON.stringify({ backend: "static" }, null, 2), "utf-8");
51
- console.log(ansis_default.cyan`${MARK_NODE} Writing RPC dump to ${resolve(devToolsRoot, "api/rpc-dump.json")}`);
52
- const dump = {};
53
- for (const [key, value] of Object.entries(devtools.context.rpc.functions)) if (value.type === "static") dump[key] = await value.handler?.();
54
- await fs.writeFile(resolve(devToolsRoot, "api/rpc-dump.json"), JSON.stringify(dump, null, 2), "utf-8");
55
- console.log(ansis_default.green`${MARK_NODE} Built to ${relative(devtools.config.root, outDir)}`);
56
- throw new Error("[Vite DevTools] Build mode of Vite DevTools is not yet complete");
57
- });
58
- cli.command("", "Start devtools").option("--root <root>", "Root directory", { default: process.cwd() }).option("--config <config>", "Vite config file").option("--host <host>", "Host", { default: process.env.HOST || "127.0.0.1" }).option("--port <port>", "Port", { default: process.env.PORT || 9999 }).option("--open", "Open browser", { default: true }).action(async (options) => {
59
- const host = options.host;
60
- const port = await getPort({
61
- port: options.port,
62
- portRange: [9999, 15e3],
63
- host
64
- });
65
- const devtools = await startStandaloneDevTools({ cwd: options.root });
66
- const { h3 } = await createDevToolsMiddleware({
67
- cwd: devtools.config.root,
68
- context: devtools.context
69
- });
70
- const app = createApp();
71
- app.use("/__vite_devtools__", h3.handler);
72
- app.use("/", eventHandler(async (event) => {
73
- return sendRedirect(event, "/__vite_devtools__/");
74
- }));
75
- createServer(toNodeListener(app)).listen(port, host, async () => {
76
- console.log(ansis_default.green`${MARK_NODE} Vite DevTools started at`, ansis_default.green(`http://${host === "127.0.0.1" ? "localhost" : host}:${port}`), "\n");
77
- if (options.open) await open(`http://${host === "127.0.0.1" ? "localhost" : host}:${port}`);
78
- });
79
- });
11
+ cli.command("build", "Build devtools with current config file for static hosting").option("--root <root>", "Root directory", { default: process.cwd() }).option("--config <config>", "Vite config file").option("--base <baseURL>", "Base URL for deployment", { default: "/" }).option("--outDir <dir>", "Output directory", { default: ".vite-devtools" }).action(build);
12
+ cli.command("", "Start devtools").option("--root <root>", "Root directory", { default: process.cwd() }).option("--config <config>", "Vite config file").option("--host <host>", "Host", { default: process.env.HOST || "127.0.0.1" }).option("--port <port>", "Port", { default: process.env.PORT || 9999 }).option("--open", "Open browser", { default: true }).action(start);
80
13
  cli.help();
81
14
  cli.parse();
82
15
 
@@ -1,29 +1,112 @@
1
- import { useLocalStorage } from "../core-uTAXYiA1.js";
1
+ import { F as markRaw, I as reactive, L as ref, M as watchEffect, c as useLocalStorage, g as computed, i as BUILTIN_ENTRIES, n as createDockEntryState, r as useDocksEntries, t as DEFAULT_DOCK_PANEL_STORE, z as toRefs } from "../docks-BuqYBItp.js";
2
2
  import { getDevToolsRpcClient } from "@vitejs/devtools-kit/client";
3
3
 
4
+ //#region src/client/webcomponents/state/setup-script.ts
5
+ function _executeSetupScript(entry, context) {
6
+ const id = `${entry.type}:${entry.id}`;
7
+ return import(
8
+ /* @vite-ignore */
9
+ ["/.devtools", "imports"].join("-")
10
+ ).then((module) => {
11
+ const importFn = module.importsMap[id];
12
+ if (!importFn) return Promise.reject(/* @__PURE__ */ new Error(`[VITE DEVTOOLS] No import found for id: ${id}`));
13
+ return importFn().then((fn) => fn(context));
14
+ }).catch((error) => {
15
+ console.error("[VITE DEVTOOLS] Error executing import action", error);
16
+ return Promise.reject(error);
17
+ });
18
+ }
19
+ const _setupPromises = /* @__PURE__ */ new Map();
20
+ function executeSetupScript(entry, context) {
21
+ if (_setupPromises.has(entry.id)) return _setupPromises.get(entry.id);
22
+ const promise = _executeSetupScript(entry, context);
23
+ _setupPromises.set(entry.id, promise);
24
+ return promise;
25
+ }
26
+
27
+ //#endregion
28
+ //#region src/client/webcomponents/state/context.ts
29
+ let _docksContext;
30
+ async function createDocksContext(clientType, rpc, panelStore) {
31
+ if (_docksContext) return _docksContext;
32
+ const dockEntries = await useDocksEntries(rpc);
33
+ const selectedId = ref(null);
34
+ const selected = computed(() => dockEntries.value.find((entry) => entry.id === selectedId.value) ?? BUILTIN_ENTRIES.find((entry) => entry.id === selectedId.value) ?? null);
35
+ const dockEntryStateMap = reactive(/* @__PURE__ */ new Map());
36
+ watchEffect(() => {
37
+ for (const entry of dockEntries.value) {
38
+ if (dockEntryStateMap.has(entry.id)) {
39
+ dockEntryStateMap.get(entry.id).entryMeta = entry;
40
+ continue;
41
+ }
42
+ dockEntryStateMap.set(entry.id, createDockEntryState(entry, selected));
43
+ }
44
+ });
45
+ panelStore ||= ref(DEFAULT_DOCK_PANEL_STORE());
46
+ const switchEntry = async (id = null) => {
47
+ if (id == null) {
48
+ selectedId.value = null;
49
+ return true;
50
+ }
51
+ if (id === "~client-auth-notice") {
52
+ selectedId.value = id;
53
+ panelStore.value.open = true;
54
+ return true;
55
+ }
56
+ const entry = dockEntries.value.find((e) => e.id === id);
57
+ if (!entry) return false;
58
+ if (entry.type === "action" || entry.type === "custom-render" || entry.type === "iframe" && entry.clientScript) {
59
+ const current = dockEntryStateMap.get(id);
60
+ await executeSetupScript(entry, reactive({
61
+ ...toRefs(_docksContext),
62
+ current
63
+ }));
64
+ }
65
+ selectedId.value = entry.id;
66
+ panelStore.value.open = true;
67
+ return true;
68
+ };
69
+ const toggleEntry = async (id) => {
70
+ if (selectedId.value === id) return switchEntry(null);
71
+ return switchEntry(id);
72
+ };
73
+ _docksContext = reactive({
74
+ panel: {
75
+ store: panelStore,
76
+ isDragging: false,
77
+ isResizing: false,
78
+ isVertical: computed(() => panelStore.value.position === "left" || panelStore.value.position === "right")
79
+ },
80
+ docks: {
81
+ selectedId,
82
+ selected,
83
+ entries: dockEntries,
84
+ entryToStateMap: markRaw(dockEntryStateMap),
85
+ getStateById: (id) => dockEntryStateMap.get(id),
86
+ switchEntry,
87
+ toggleEntry
88
+ },
89
+ rpc,
90
+ clientType
91
+ });
92
+ return _docksContext;
93
+ }
94
+
95
+ //#endregion
4
96
  //#region src/client/inject/index.ts
5
97
  async function init() {
6
98
  console.log("[VITE DEVTOOLS] Client injected");
7
- const { rpc } = await getDevToolsRpcClient();
8
- console.log("[VITE DEVTOOLS] RPC", rpc);
9
- const docks = await rpc["vite:core:list-dock-entries"]();
10
- console.log("[VITE DEVTOOLS] Docks", docks);
11
- const rpcFunctions = await rpc["vite:core:list-rpc-functions"]();
12
- console.log("[VITE DEVTOOLS] RPC Functions", rpcFunctions);
13
- const state = useLocalStorage("vite-devtools-dock-state", {
99
+ const context = await createDocksContext("embedded", await getDevToolsRpcClient(), useLocalStorage("vite-devtools-dock-state", {
14
100
  width: 80,
15
101
  height: 80,
16
102
  top: 0,
17
103
  left: 0,
18
104
  position: "left",
19
105
  open: false,
20
- minimizePanelInactive: 3e3
21
- }, { mergeDefaults: true });
106
+ inactiveTimeout: 3e3
107
+ }, { mergeDefaults: true }));
22
108
  const { DockEmbedded } = await import("@vitejs/devtools/client/webcomponents");
23
- const dockEl = new DockEmbedded({
24
- state,
25
- docks
26
- });
109
+ const dockEl = new DockEmbedded({ context });
27
110
  document.body.appendChild(dockEl);
28
111
  }
29
112
  if (window.parent !== window) console.log("[VITE DEVTOOLS] Skipping in iframe");