@pubinfo/vite 2.0.12 → 2.0.14
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/dist/index.d.ts +32 -1
- package/dist/index.js +410 -12
- package/package.json +7 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,26 @@
|
|
|
1
1
|
import { UserConfig, UserConfigFnObject } from "rolldown-vite";
|
|
2
|
+
import * as vite_plugin_vue_devtools0 from "vite-plugin-vue-devtools";
|
|
3
|
+
import { VitePluginVueDevToolsOptions } from "vite-plugin-vue-devtools";
|
|
2
4
|
import * as _pubinfo_unplugin_openapi0 from "@pubinfo/unplugin-openapi";
|
|
3
5
|
import { Options } from "@pubinfo/unplugin-openapi";
|
|
4
6
|
export * from "rolldown-vite";
|
|
5
7
|
export * from "vite-plugin-fake-server/client";
|
|
6
8
|
|
|
9
|
+
//#region src/plugins/built-in/chrome-devtools.d.ts
|
|
10
|
+
/**
|
|
11
|
+
* Chrome DevTools 插件配置选项
|
|
12
|
+
*/
|
|
13
|
+
interface ChromeDevtoolsOptions {
|
|
14
|
+
/** 是否启用插件 */
|
|
15
|
+
enabled?: boolean;
|
|
16
|
+
/** 自定义端口,如果不指定则自动分配 */
|
|
17
|
+
port?: number;
|
|
18
|
+
/** 是否自动注入调试脚本到页面 */
|
|
19
|
+
autoInject?: boolean;
|
|
20
|
+
/** DevTools 面板访问路径 */
|
|
21
|
+
panelPath?: string;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
7
24
|
//#region src/plugins/built-in/inject-auto.d.ts
|
|
8
25
|
interface InjectAutoOptions {
|
|
9
26
|
id: string;
|
|
@@ -54,6 +71,12 @@ interface PubinfoConfig {
|
|
|
54
71
|
openapi?: Options;
|
|
55
72
|
/** `resolver` */
|
|
56
73
|
resolver?: ResolverPluginOptions;
|
|
74
|
+
/** devtools */
|
|
75
|
+
devtools?: Omit<VitePluginVueDevToolsOptions, 'enabled'> & {
|
|
76
|
+
enabled?: boolean;
|
|
77
|
+
};
|
|
78
|
+
/** chrome devtools */
|
|
79
|
+
chromeDevtools?: ChromeDevtoolsOptions;
|
|
57
80
|
/** `commitlint` */
|
|
58
81
|
commitlint?: boolean | CommitlintOptions;
|
|
59
82
|
}
|
|
@@ -72,6 +95,10 @@ declare function definePubinfoConfig(config: PubinfoConfig): {
|
|
|
72
95
|
vite: UserConfigFnObject;
|
|
73
96
|
openapi?: _pubinfo_unplugin_openapi0.Options;
|
|
74
97
|
resolver?: ResolverPluginOptions;
|
|
98
|
+
devtools?: Omit<vite_plugin_vue_devtools0.VitePluginVueDevToolsOptions, "enabled"> & {
|
|
99
|
+
enabled?: boolean;
|
|
100
|
+
};
|
|
101
|
+
chromeDevtools?: ChromeDevtoolsOptions;
|
|
75
102
|
commitlint?: boolean | CommitlintOptions;
|
|
76
103
|
};
|
|
77
104
|
/**
|
|
@@ -82,7 +109,11 @@ declare function defineModuleConfig(config: ModuleConfig): {
|
|
|
82
109
|
moduleId: InjectAutoOptions["id"];
|
|
83
110
|
openapi?: _pubinfo_unplugin_openapi0.Options;
|
|
84
111
|
resolver?: ResolverPluginOptions;
|
|
112
|
+
devtools?: Omit<vite_plugin_vue_devtools0.VitePluginVueDevToolsOptions, "enabled"> & {
|
|
113
|
+
enabled?: boolean;
|
|
114
|
+
};
|
|
115
|
+
chromeDevtools?: ChromeDevtoolsOptions;
|
|
85
116
|
commitlint?: boolean | CommitlintOptions;
|
|
86
117
|
};
|
|
87
118
|
//#endregion
|
|
88
|
-
export { CommitlintOptions, ModuleConfig, PubinfoConfig, ResolverPluginOptions, defineModuleConfig, definePubinfoConfig };
|
|
119
|
+
export { ChromeDevtoolsOptions, CommitlintOptions, ModuleConfig, PubinfoConfig, ResolverPluginOptions, defineModuleConfig, definePubinfoConfig };
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
import { defineConfig, loadEnv, mergeConfig, normalizePath } from "rolldown-vite";
|
|
3
|
-
import { cwd } from "node:process";
|
|
3
|
+
import process, { cwd } from "node:process";
|
|
4
4
|
import chalk from "chalk";
|
|
5
5
|
import consola from "consola";
|
|
6
|
-
import { dirname, join, posix, relative, resolve } from "node:path";
|
|
6
|
+
import path, { dirname, join, posix, relative, resolve } from "node:path";
|
|
7
7
|
import vue from "@vitejs/plugin-vue";
|
|
8
8
|
import vueJsx from "@vitejs/plugin-vue-jsx";
|
|
9
|
+
import fs from "fs-extra";
|
|
9
10
|
import boxen from "boxen";
|
|
10
11
|
import MagicString from "magic-string";
|
|
11
12
|
import fg from "fast-glob";
|
|
12
13
|
import { merge } from "lodash-es";
|
|
13
14
|
import picomatch from "picomatch";
|
|
15
|
+
import "micromatch";
|
|
16
|
+
import { execSync } from "node:child_process";
|
|
14
17
|
import dayjs from "dayjs";
|
|
15
18
|
import { readPackageJSON } from "pkg-types";
|
|
16
19
|
import { existsSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
|
@@ -40,7 +43,7 @@ export * from "vite-plugin-fake-server/client"
|
|
|
40
43
|
//#region src/helper/alias.ts
|
|
41
44
|
function alias(root) {
|
|
42
45
|
const resolvePath = (name) => join(root, name);
|
|
43
|
-
const resolveDeps = (name, path) => join(createRequire(import.meta.url).resolve(name), path);
|
|
46
|
+
const resolveDeps = (name, path$1) => join(createRequire(import.meta.url).resolve(name), path$1);
|
|
44
47
|
return {
|
|
45
48
|
"@": resolvePath("src"),
|
|
46
49
|
"#": resolvePath("types"),
|
|
@@ -66,13 +69,379 @@ function getServerProxy(env, isProxy) {
|
|
|
66
69
|
else serverProxy[pk] = {
|
|
67
70
|
target: url,
|
|
68
71
|
changeOrigin: true,
|
|
69
|
-
rewrite: (path) => path.replace(pk, ""),
|
|
72
|
+
rewrite: (path$1) => path$1.replace(pk, ""),
|
|
70
73
|
secure: false
|
|
71
74
|
};
|
|
72
75
|
}
|
|
73
76
|
return serverProxy;
|
|
74
77
|
}
|
|
75
78
|
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/plugins/built-in/chrome-devtools.ts
|
|
81
|
+
const GLOBAL_STATE_KEY = "__pubinfoChromeDevtoolsState__";
|
|
82
|
+
const DEFAULT_VITE_PORT = 5173;
|
|
83
|
+
const DEFAULT_HOST = "localhost";
|
|
84
|
+
const RANDOM_PORT_RANGE = {
|
|
85
|
+
min: 56e3,
|
|
86
|
+
max: 64e3
|
|
87
|
+
};
|
|
88
|
+
function getGlobalState() {
|
|
89
|
+
const globalRef = globalThis;
|
|
90
|
+
if (!globalRef[GLOBAL_STATE_KEY]) globalRef[GLOBAL_STATE_KEY] = {};
|
|
91
|
+
return globalRef[GLOBAL_STATE_KEY];
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 管理 chii (Chrome DevTools 后端) 的生命周期与日志。
|
|
95
|
+
*/
|
|
96
|
+
var ChiiServiceManager = class {
|
|
97
|
+
port;
|
|
98
|
+
isStarted = false;
|
|
99
|
+
loggingPatched = false;
|
|
100
|
+
globalState;
|
|
101
|
+
constructor() {
|
|
102
|
+
this.globalState = getGlobalState();
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* 启动 chii 服务,自动处理端口复用、冲突检测与启动失败兜底。
|
|
106
|
+
*/
|
|
107
|
+
async start(customPort, vitePort, viteHost, panelPath) {
|
|
108
|
+
if (this.isStarted && this.port) return this.port;
|
|
109
|
+
const reusedPort = await this.tryReuseExistingService(customPort, vitePort);
|
|
110
|
+
if (reusedPort) {
|
|
111
|
+
this.registerPort(reusedPort);
|
|
112
|
+
return reusedPort;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
const { start } = await this.importChii();
|
|
116
|
+
const portCandidate = customPort || this.generatePort(vitePort);
|
|
117
|
+
const port = await this.findAvailablePort(portCandidate, customPort === void 0);
|
|
118
|
+
if (port == null) {
|
|
119
|
+
console.warn("[pubinfo][chrome-devtools] 未找到可用的 DevTools 端口,跳过启动");
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
start({ port });
|
|
123
|
+
this.registerPort(port);
|
|
124
|
+
return port;
|
|
125
|
+
} catch (error) {
|
|
126
|
+
console.warn("[pubinfo][chrome-devtools] 启动 Chii 失败:", error);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
getPort() {
|
|
131
|
+
return this.port;
|
|
132
|
+
}
|
|
133
|
+
isServiceStarted() {
|
|
134
|
+
return this.isStarted && !!this.port;
|
|
135
|
+
}
|
|
136
|
+
registerPort(port) {
|
|
137
|
+
this.port = port;
|
|
138
|
+
this.isStarted = true;
|
|
139
|
+
this.globalState.port = port;
|
|
140
|
+
}
|
|
141
|
+
async tryReuseExistingService(customPort, vitePort) {
|
|
142
|
+
if (this.globalState.port != null) {
|
|
143
|
+
if (await this.isChiiRunning(this.globalState.port)) return this.globalState.port;
|
|
144
|
+
this.globalState.port = void 0;
|
|
145
|
+
}
|
|
146
|
+
if (customPort != null) {
|
|
147
|
+
if (await this.isChiiRunning(customPort)) return customPort;
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (vitePort) {
|
|
151
|
+
const generatedPort = this.generatePort(vitePort);
|
|
152
|
+
if (await this.isChiiRunning(generatedPort)) return generatedPort;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
generatePort(vitePort) {
|
|
156
|
+
if (vitePort) {
|
|
157
|
+
const prefixedCandidate = Number(`6${vitePort}`);
|
|
158
|
+
if (Number.isFinite(prefixedCandidate) && prefixedCandidate <= 65535) return prefixedCandidate;
|
|
159
|
+
const highRangeCandidate = 6e4 + vitePort % 1e3;
|
|
160
|
+
if (highRangeCandidate <= 65535) return highRangeCandidate;
|
|
161
|
+
}
|
|
162
|
+
const { min, max } = RANDOM_PORT_RANGE;
|
|
163
|
+
return min + Math.floor(Math.random() * (max - min + 1));
|
|
164
|
+
}
|
|
165
|
+
async findAvailablePort(startPort, allowIncrement) {
|
|
166
|
+
let port = startPort;
|
|
167
|
+
for (let attempt = 0; attempt < 20; attempt += 1) {
|
|
168
|
+
if (await this.isPortAvailable(port)) return port;
|
|
169
|
+
if (!allowIncrement) return;
|
|
170
|
+
port += 1;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
async isPortAvailable(port) {
|
|
174
|
+
const net = await this.importNet();
|
|
175
|
+
return await new Promise((resolve$1) => {
|
|
176
|
+
const tester = net.createServer();
|
|
177
|
+
tester.once("error", () => {
|
|
178
|
+
try {
|
|
179
|
+
tester.close();
|
|
180
|
+
} catch {}
|
|
181
|
+
resolve$1(false);
|
|
182
|
+
});
|
|
183
|
+
tester.once("listening", () => {
|
|
184
|
+
tester.close(() => resolve$1(true));
|
|
185
|
+
});
|
|
186
|
+
tester.listen(port);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
async isChiiRunning(port) {
|
|
190
|
+
try {
|
|
191
|
+
const controller = new AbortController();
|
|
192
|
+
const timeout = setTimeout(() => controller.abort(), 300);
|
|
193
|
+
try {
|
|
194
|
+
const response = await fetch(`http://127.0.0.1:${port}/targets`, { signal: controller.signal });
|
|
195
|
+
if (!response.ok) return false;
|
|
196
|
+
const data = await response.json();
|
|
197
|
+
return Array.isArray(data?.targets);
|
|
198
|
+
} finally {
|
|
199
|
+
clearTimeout(timeout);
|
|
200
|
+
}
|
|
201
|
+
} catch {
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
async importChii() {
|
|
206
|
+
await this.patchChiiLogger();
|
|
207
|
+
return await import(
|
|
208
|
+
/* @vite-ignore */
|
|
209
|
+
"chii"
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
async importNet() {
|
|
213
|
+
return await import("node:net");
|
|
214
|
+
}
|
|
215
|
+
async patchChiiLogger() {
|
|
216
|
+
if (this.loggingPatched) return;
|
|
217
|
+
try {
|
|
218
|
+
const utilModule = createRequire(import.meta.url)("chii/server/lib/util.js");
|
|
219
|
+
const utilTarget = typeof utilModule.log === "function" ? utilModule : utilModule.default;
|
|
220
|
+
if (!utilTarget || typeof utilTarget.log !== "function") {
|
|
221
|
+
console.warn("[pubinfo][chrome-devtools] 未检测到 chii util.log,跳过日志定制");
|
|
222
|
+
this.loggingPatched = true;
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
const descriptor = Object.getOwnPropertyDescriptor(utilTarget, "log");
|
|
226
|
+
if (descriptor && descriptor.writable === false) Object.defineProperty(utilTarget, "log", {
|
|
227
|
+
...descriptor,
|
|
228
|
+
writable: true,
|
|
229
|
+
configurable: true
|
|
230
|
+
});
|
|
231
|
+
const patchedLog = () => {};
|
|
232
|
+
const targetExports = utilTarget;
|
|
233
|
+
const moduleExports = utilModule;
|
|
234
|
+
const defaultExports = utilModule.default;
|
|
235
|
+
targetExports.log = patchedLog;
|
|
236
|
+
if (moduleExports !== targetExports) moduleExports.log = patchedLog;
|
|
237
|
+
if (defaultExports && defaultExports !== targetExports) defaultExports.log = patchedLog;
|
|
238
|
+
this.loggingPatched = true;
|
|
239
|
+
} catch (error) {
|
|
240
|
+
console.warn("[pubinfo][chrome-devtools] 自定义 DevTools 日志失败:", error);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
/**
|
|
245
|
+
* 处理 /__chrome_devtools 面板访问跳转:实时查询 chii targets,生成前端面板地址。
|
|
246
|
+
*/
|
|
247
|
+
var DevToolsPanelHandler = class {
|
|
248
|
+
panelPath;
|
|
249
|
+
serviceManager;
|
|
250
|
+
constructor(panelPath, serviceManager) {
|
|
251
|
+
this.panelPath = panelPath;
|
|
252
|
+
this.serviceManager = serviceManager;
|
|
253
|
+
}
|
|
254
|
+
createMiddleware(server) {
|
|
255
|
+
server.middlewares.use(async (req, res, next) => {
|
|
256
|
+
if (!this.isTargetPath(req.url)) return next();
|
|
257
|
+
const port = this.serviceManager.getPort();
|
|
258
|
+
if (!port) {
|
|
259
|
+
this.sendErrorResponse(res, "DevTools 后端服务未启动");
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
try {
|
|
263
|
+
const devtoolsUrl = await this.getDevToolsUrl(port);
|
|
264
|
+
this.sendRedirectResponse(res, devtoolsUrl);
|
|
265
|
+
} catch (error) {
|
|
266
|
+
console.warn("[pubinfo][chrome-devtools] 获取 DevTools URL 失败:", error);
|
|
267
|
+
this.sendRedirectResponse(res, `http://127.0.0.1:${port}/`);
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
isTargetPath(url) {
|
|
272
|
+
if (!url) return false;
|
|
273
|
+
return url === this.panelPath || url === `${this.panelPath}/`;
|
|
274
|
+
}
|
|
275
|
+
async getDevToolsUrl(port) {
|
|
276
|
+
const firstTarget = (await (await fetch(`http://127.0.0.1:${port}/targets`)).json())?.targets?.[0];
|
|
277
|
+
if (firstTarget) {
|
|
278
|
+
const wsId = Math.random().toString(36).slice(2, 10);
|
|
279
|
+
return `http://127.0.0.1:${port}/front_end/chii_app.html?ws=${encodeURIComponent(`127.0.0.1:${port}/client/${wsId}?target=${firstTarget.id}`)}&rtc=false`;
|
|
280
|
+
}
|
|
281
|
+
return `http://127.0.0.1:${port}/`;
|
|
282
|
+
}
|
|
283
|
+
sendErrorResponse(res, message) {
|
|
284
|
+
res.statusCode = 503;
|
|
285
|
+
res.end(message);
|
|
286
|
+
}
|
|
287
|
+
sendRedirectResponse(res, location) {
|
|
288
|
+
res.statusCode = 302;
|
|
289
|
+
res.setHeader("Location", location);
|
|
290
|
+
res.end();
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
/**
|
|
294
|
+
* 负责把 target.js 注入到 HTML 中,使页面成为可调试目标。
|
|
295
|
+
*/
|
|
296
|
+
var HtmlInjector = class {
|
|
297
|
+
serviceManager;
|
|
298
|
+
constructor(serviceManager) {
|
|
299
|
+
this.serviceManager = serviceManager;
|
|
300
|
+
}
|
|
301
|
+
injectScript(html) {
|
|
302
|
+
const port = this.serviceManager.getPort();
|
|
303
|
+
if (!port) return html;
|
|
304
|
+
const injectedMarker = `//__chrome_devtools_target_${port}`;
|
|
305
|
+
if (html.includes(injectedMarker)) return html;
|
|
306
|
+
const scriptTag = this.createScriptTag(port, injectedMarker);
|
|
307
|
+
return this.insertScriptTag(html, scriptTag);
|
|
308
|
+
}
|
|
309
|
+
createScriptTag(port, marker) {
|
|
310
|
+
return `<script>${marker}<\/script>\n<script src="http://127.0.0.1:${port}/target.js" data-pubinfo-devtools="chrome"><\/script>`;
|
|
311
|
+
}
|
|
312
|
+
insertScriptTag(html, scriptTag) {
|
|
313
|
+
if (/<head[\s\S]*?>/i.test(html)) return html.replace(/<head([\s\S]*?)>/i, (match) => `${match}\n${scriptTag}`);
|
|
314
|
+
return html.replace(/<body(.*?)>/i, (match) => `${match}\n${scriptTag}`);
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
/**
|
|
318
|
+
* 管理 Vite Dev Server 与 chii 服务之间的协作:监听端口、控制打印、确保服务可用。
|
|
319
|
+
*/
|
|
320
|
+
var DevServerOrchestrator = class {
|
|
321
|
+
server;
|
|
322
|
+
lastResolvedServerInfo;
|
|
323
|
+
constructor(serviceManager, panelHandler, panelPath, customPort) {
|
|
324
|
+
this.serviceManager = serviceManager;
|
|
325
|
+
this.panelHandler = panelHandler;
|
|
326
|
+
this.panelPath = panelPath;
|
|
327
|
+
this.customPort = customPort;
|
|
328
|
+
}
|
|
329
|
+
attach(server) {
|
|
330
|
+
this.server = server;
|
|
331
|
+
this.lastResolvedServerInfo = this.resolveServerInfo();
|
|
332
|
+
this.panelHandler.createMiddleware(server);
|
|
333
|
+
this.bindStartupHooks(server);
|
|
334
|
+
}
|
|
335
|
+
async ensureReady() {
|
|
336
|
+
const info = this.resolveServerInfo();
|
|
337
|
+
await this.serviceManager.start(this.customPort, info.port, info.host, this.panelPath);
|
|
338
|
+
}
|
|
339
|
+
async ensureReadyIfNeeded() {
|
|
340
|
+
if (!this.serviceManager.isServiceStarted()) await this.ensureReady();
|
|
341
|
+
}
|
|
342
|
+
bindStartupHooks(server) {
|
|
343
|
+
const scheduleStart = () => {
|
|
344
|
+
(async () => {
|
|
345
|
+
await this.ensureReady();
|
|
346
|
+
})();
|
|
347
|
+
};
|
|
348
|
+
if (server.httpServer?.listening) scheduleStart();
|
|
349
|
+
else server.httpServer?.once("listening", () => {
|
|
350
|
+
this.lastResolvedServerInfo = void 0;
|
|
351
|
+
scheduleStart();
|
|
352
|
+
});
|
|
353
|
+
const originalPrintUrls = server.printUrls;
|
|
354
|
+
server.printUrls = () => {
|
|
355
|
+
originalPrintUrls();
|
|
356
|
+
(async () => {
|
|
357
|
+
await this.ensureReady();
|
|
358
|
+
})();
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
resolveServerInfo() {
|
|
362
|
+
const info = this.computeServerInfo();
|
|
363
|
+
this.lastResolvedServerInfo = info;
|
|
364
|
+
return info;
|
|
365
|
+
}
|
|
366
|
+
computeServerInfo() {
|
|
367
|
+
const server = this.server;
|
|
368
|
+
if (!server) return {
|
|
369
|
+
host: DEFAULT_HOST,
|
|
370
|
+
port: DEFAULT_VITE_PORT
|
|
371
|
+
};
|
|
372
|
+
const localUrl = server.resolvedUrls?.local?.[0];
|
|
373
|
+
if (localUrl) try {
|
|
374
|
+
const url = new URL(localUrl);
|
|
375
|
+
if (url.hostname && url.port) return {
|
|
376
|
+
host: this.normalizeHost(url.hostname),
|
|
377
|
+
port: Number(url.port) || DEFAULT_VITE_PORT
|
|
378
|
+
};
|
|
379
|
+
} catch {}
|
|
380
|
+
const configuredHost = typeof server.config.server.host === "string" ? server.config.server.host : DEFAULT_HOST;
|
|
381
|
+
const address = server.httpServer?.address();
|
|
382
|
+
if (address && typeof address === "object" && "port" in address && typeof address.port === "number") return {
|
|
383
|
+
host: this.normalizeHost(configuredHost),
|
|
384
|
+
port: address.port
|
|
385
|
+
};
|
|
386
|
+
return {
|
|
387
|
+
host: this.normalizeHost(configuredHost),
|
|
388
|
+
port: server.config.server.port || DEFAULT_VITE_PORT
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
normalizeHost(host) {
|
|
392
|
+
if (!host || host === "0.0.0.0" || host === "::") return DEFAULT_HOST;
|
|
393
|
+
return host;
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
function createChromeDevtools(options = {}) {
|
|
397
|
+
const { enabled = true, port, autoInject = true, panelPath = "/__chrome_devtools" } = options;
|
|
398
|
+
if (!enabled) return null;
|
|
399
|
+
const serviceManager = new ChiiServiceManager();
|
|
400
|
+
const panelHandler = new DevToolsPanelHandler(panelPath, serviceManager);
|
|
401
|
+
const htmlInjector = new HtmlInjector(serviceManager);
|
|
402
|
+
const orchestrator = new DevServerOrchestrator(serviceManager, panelHandler, panelPath, port);
|
|
403
|
+
return {
|
|
404
|
+
name: "pubinfo:chrome-devtools",
|
|
405
|
+
apply: "serve",
|
|
406
|
+
enforce: "post",
|
|
407
|
+
async configureServer(server) {
|
|
408
|
+
orchestrator.attach(server);
|
|
409
|
+
},
|
|
410
|
+
async transformIndexHtml(html) {
|
|
411
|
+
if (!autoInject) return html;
|
|
412
|
+
await orchestrator.ensureReadyIfNeeded();
|
|
413
|
+
return htmlInjector.injectScript(html);
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
//#endregion
|
|
419
|
+
//#region src/plugins/built-in/clean-build.ts
|
|
420
|
+
function createCleanBuild() {
|
|
421
|
+
let config;
|
|
422
|
+
return {
|
|
423
|
+
name: "vite-plugin-clean-build",
|
|
424
|
+
apply: "build",
|
|
425
|
+
enforce: "post",
|
|
426
|
+
configResolved(resolvedConfig) {
|
|
427
|
+
config = resolvedConfig;
|
|
428
|
+
},
|
|
429
|
+
buildStart() {
|
|
430
|
+
const outDir = config.build.outDir;
|
|
431
|
+
const root = config.root;
|
|
432
|
+
const outputPath = path.resolve(root, outDir);
|
|
433
|
+
if (fs.existsSync(outputPath)) try {
|
|
434
|
+
fs.rmSync(outputPath, {
|
|
435
|
+
recursive: true,
|
|
436
|
+
force: true
|
|
437
|
+
});
|
|
438
|
+
} catch (error) {
|
|
439
|
+
consola.error(`[vite-plugin-clean-build] Failed to clean build directory: ${error}`);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
|
|
76
445
|
//#endregion
|
|
77
446
|
//#region src/plugins/built-in/info.ts
|
|
78
447
|
var Ctx$1 = class {
|
|
@@ -166,8 +535,8 @@ function getPatternBase(pattern) {
|
|
|
166
535
|
}
|
|
167
536
|
return baseParts.join("/");
|
|
168
537
|
}
|
|
169
|
-
function normalizePath$1(path) {
|
|
170
|
-
return posix.normalize(path.replace(/\\/g, "/"));
|
|
538
|
+
function normalizePath$1(path$1) {
|
|
539
|
+
return posix.normalize(path$1.replace(/\\/g, "/"));
|
|
171
540
|
}
|
|
172
541
|
function libResolverPlugin(options) {
|
|
173
542
|
const virtualModuleId = "virtual:pubinfo-resolver";
|
|
@@ -265,6 +634,34 @@ function createLibResolver(options) {
|
|
|
265
634
|
|
|
266
635
|
//#endregion
|
|
267
636
|
//#region src/plugins/built-in/system-info.ts
|
|
637
|
+
function getGitInfo() {
|
|
638
|
+
try {
|
|
639
|
+
const commit = execSync("git rev-parse HEAD", { encoding: "utf-8" }).trim();
|
|
640
|
+
const commitShort = execSync("git rev-parse --short HEAD", { encoding: "utf-8" }).trim();
|
|
641
|
+
let branch = process.env.GITHUB_REF_NAME || process.env.CI_COMMIT_REF_NAME || process.env.GIT_BRANCH || process.env.BRANCH_NAME;
|
|
642
|
+
if (!branch) {
|
|
643
|
+
branch = execSync("git rev-parse --abbrev-ref HEAD", { encoding: "utf-8" }).trim();
|
|
644
|
+
if (branch === "HEAD") try {
|
|
645
|
+
const branches = execSync(`git show-ref --heads | grep ${commit}`, { encoding: "utf-8" }).trim();
|
|
646
|
+
if (branches) {
|
|
647
|
+
const match = branches.split("\n")[0].match(/refs\/heads\/(.+)$/);
|
|
648
|
+
if (match) branch = match[1];
|
|
649
|
+
}
|
|
650
|
+
} catch {}
|
|
651
|
+
}
|
|
652
|
+
return {
|
|
653
|
+
commit,
|
|
654
|
+
commitShort,
|
|
655
|
+
branch
|
|
656
|
+
};
|
|
657
|
+
} catch {
|
|
658
|
+
return {
|
|
659
|
+
commit: "",
|
|
660
|
+
commitShort: "",
|
|
661
|
+
branch: ""
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
}
|
|
268
665
|
function createSystemInfo(options = {}) {
|
|
269
666
|
const defineKey = options.defineKey || "__SYSTEM_INFO__";
|
|
270
667
|
return {
|
|
@@ -273,7 +670,8 @@ function createSystemInfo(options = {}) {
|
|
|
273
670
|
const pkg = await readPackageJSON();
|
|
274
671
|
const systemInfo = {
|
|
275
672
|
buildTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
|
|
276
|
-
pkg
|
|
673
|
+
pkg,
|
|
674
|
+
git: getGitInfo()
|
|
277
675
|
};
|
|
278
676
|
return { define: { [defineKey]: JSON.stringify(systemInfo) } };
|
|
279
677
|
}
|
|
@@ -483,9 +881,8 @@ function createInjectCSS() {
|
|
|
483
881
|
|
|
484
882
|
//#endregion
|
|
485
883
|
//#region src/plugins/intergrations/inspector.ts
|
|
486
|
-
function createInspector(
|
|
487
|
-
|
|
488
|
-
if (VITE_APP_INSPECTOR && VITE_APP_INSPECTOR === "true") return VueDevTools();
|
|
884
|
+
function createInspector(devtoolsConfig) {
|
|
885
|
+
if (devtoolsConfig?.enabled) return VueDevTools({ ...devtoolsConfig });
|
|
489
886
|
else return null;
|
|
490
887
|
}
|
|
491
888
|
|
|
@@ -536,7 +933,8 @@ function createVitePlugins(viteEnv, isBuild = false, config, type) {
|
|
|
536
933
|
createUnocss(),
|
|
537
934
|
createIcons(),
|
|
538
935
|
createMock(viteEnv, isBuild),
|
|
539
|
-
createInspector(
|
|
936
|
+
createInspector(config.devtools),
|
|
937
|
+
createChromeDevtools(config.chromeDevtools),
|
|
540
938
|
createOpenAPI(config.openapi),
|
|
541
939
|
createLibResolver(config.resolver),
|
|
542
940
|
createAppInfo(),
|
|
@@ -546,7 +944,7 @@ function createVitePlugins(viteEnv, isBuild = false, config, type) {
|
|
|
546
944
|
createDTS(),
|
|
547
945
|
createInjectCSS(),
|
|
548
946
|
createInjectAuto({ id: config.moduleId })
|
|
549
|
-
] :
|
|
947
|
+
] : [createCleanBuild()],
|
|
550
948
|
isBuild ? createCompression(viteEnv) : null
|
|
551
949
|
].filter(Boolean);
|
|
552
950
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pubinfo/vite",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.
|
|
4
|
+
"version": "2.0.14",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -21,13 +21,14 @@
|
|
|
21
21
|
"vue": "^3.5.17"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@pubinfo/unplugin-openapi": "^0.9.
|
|
24
|
+
"@pubinfo/unplugin-openapi": "^0.9.1",
|
|
25
25
|
"@vitejs/plugin-legacy": "^7.2.1",
|
|
26
26
|
"@vitejs/plugin-vue": "^6.0.0",
|
|
27
27
|
"@vitejs/plugin-vue-jsx": "^5.0.1",
|
|
28
28
|
"abort-controller": "^3.0.0",
|
|
29
29
|
"boxen": "^8.0.1",
|
|
30
30
|
"chalk": "^5.4.1",
|
|
31
|
+
"chii": "^1.15.5",
|
|
31
32
|
"consola": "^3.4.2",
|
|
32
33
|
"dayjs": "^1.11.13",
|
|
33
34
|
"fast-glob": "^3.3.3",
|
|
@@ -35,6 +36,7 @@
|
|
|
35
36
|
"jszip": "^3.10.1",
|
|
36
37
|
"lodash-es": "^4.17.21",
|
|
37
38
|
"magic-string": "^0.30.19",
|
|
39
|
+
"micromatch": "^4.0.8",
|
|
38
40
|
"picomatch": "^4.0.3",
|
|
39
41
|
"pkg-types": "^2.3.0",
|
|
40
42
|
"rolldown-vite": "^7.1.2",
|
|
@@ -53,9 +55,11 @@
|
|
|
53
55
|
"devDependencies": {
|
|
54
56
|
"@types/fs-extra": "^11.0.4",
|
|
55
57
|
"@types/lodash-es": "^4.17.12",
|
|
58
|
+
"@types/micromatch": "^4.0.9",
|
|
56
59
|
"@types/node": "^24.0.10",
|
|
57
60
|
"@types/picomatch": "^4.0.2",
|
|
58
|
-
"vue": "^3.5.17"
|
|
61
|
+
"vue": "^3.5.17",
|
|
62
|
+
"@pubinfo/shared": "2.0.14"
|
|
59
63
|
},
|
|
60
64
|
"scripts": {
|
|
61
65
|
"dev": "tsdown --watch",
|