u
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { defineConfig, searchForWorkspaceRoot } from 'vite';
|
||||
import { execSync } from 'node:child_process';
|
||||
import { defineConfig, searchForWorkspaceRoot, createLogger } from 'vite';
|
||||
import react from '@vitejs/plugin-react';
|
||||
import { VitePWA } from 'vite-plugin-pwa';
|
||||
import pc from 'picocolors';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const useHttps = process.env.VITE_HTTPS === '1';
|
||||
@@ -209,7 +211,76 @@ function nginxHeadersPreviewPlugin() {
|
||||
};
|
||||
}
|
||||
|
||||
function getListeningProcess(port) {
|
||||
try {
|
||||
const stdout = execSync(`lsof -nP -ti :${port} -sTCP:LISTEN`, {
|
||||
encoding: 'utf8',
|
||||
stdio: ['pipe', 'pipe', 'ignore'],
|
||||
}).trim();
|
||||
const pid = stdout.split('\n')[0];
|
||||
if (!pid) return null;
|
||||
let cmd = '';
|
||||
try {
|
||||
cmd = execSync(`ps -p ${pid} -o comm=`, {
|
||||
encoding: 'utf8',
|
||||
stdio: ['pipe', 'pipe', 'ignore'],
|
||||
}).trim();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
return { pid, cmd };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function createPidLogger() {
|
||||
const logger = createLogger();
|
||||
const originalInfo = logger.info.bind(logger);
|
||||
logger.info = (msg, options) => {
|
||||
if (typeof msg === 'string') {
|
||||
const match = msg.match(/Port (\d+) is in use/);
|
||||
if (match) {
|
||||
const port = match[1];
|
||||
const owner = getListeningProcess(port);
|
||||
if (owner) {
|
||||
const detail = owner.cmd ? ` (PID ${owner.pid}: ${owner.cmd})` : ` (PID ${owner.pid})`;
|
||||
msg = msg.replace(`Port ${port} is in use`, `Port ${port} is in use${pc.yellow(detail)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
originalInfo(msg, options);
|
||||
};
|
||||
return logger;
|
||||
}
|
||||
|
||||
function pidReporterPlugin() {
|
||||
const patchPrintUrls = (server) => {
|
||||
if (!server || server._pidReporterPatched) return;
|
||||
server._pidReporterPatched = true;
|
||||
const origPrintUrls = server.printUrls.bind(server);
|
||||
server.printUrls = () => {
|
||||
origPrintUrls();
|
||||
const pmId = process.env.pm_id ? pc.dim(` (PM2 #${process.env.pm_id})`) : '';
|
||||
server.config.logger.info(
|
||||
` ${pc.green('➜')} ${pc.bold('PID')}: ${pc.cyan(process.pid)}${pmId}`,
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
return {
|
||||
name: 'pid-reporter',
|
||||
configureServer(server) {
|
||||
patchPrintUrls(server);
|
||||
},
|
||||
configurePreviewServer(server) {
|
||||
patchPrintUrls(server);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export default defineConfig(({ command }) => ({
|
||||
customLogger: createPidLogger(),
|
||||
resolve: sharedResolve,
|
||||
build: {
|
||||
emptyOutDir: false,
|
||||
@@ -298,6 +369,7 @@ export default defineConfig(({ command }) => ({
|
||||
},
|
||||
},
|
||||
plugins: [
|
||||
pidReporterPlugin(),
|
||||
react(),
|
||||
nginxHeadersPreviewPlugin(),
|
||||
...(command === 'build'
|
||||
|
||||
Reference in New Issue
Block a user