Files
wehub-resource-sync cddb07a176
build container image / cpu (push) Waiting to run
build container image / cuda (push) Waiting to run
build container image / rocm (push) Waiting to run
frontend tests / frontend-tests (push) Waiting to run
openapi checks / openapi-checks (push) Waiting to run
python tests / py3.12: macos-default (push) Waiting to run
python tests / py3.11: windows-cpu (push) Waiting to run
python tests / py3.12: windows-cpu (push) Waiting to run
python tests / py3.11: linux-cpu (push) Waiting to run
python tests / py3.12: linux-cpu (push) Waiting to run
typegen checks / typegen-checks (push) Waiting to run
uv lock checks / uv-lock-checks (push) Waiting to run
frontend checks / frontend-checks (push) Waiting to run
lfs checks / lfs-check (push) Waiting to run
python checks / python-checks (push) Waiting to run
python tests / py3.11: macos-default (push) Waiting to run
docs / deploy (push) Has been cancelled
docs / changes (push) Has been cancelled
docs / check-and-build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:22:06 +08:00

52 lines
1.6 KiB
TypeScript

import MagicString from 'magic-string';
import type { Plugin } from 'vite';
/**
* A Vite plugin that automatically adds file path context to logger calls.
*/
export function loggerContextPlugin(): Plugin {
return {
name: 'logger-context',
transform(code: string, id: string) {
// Only process TypeScript/JavaScript files in src directory
if (!id.includes('/src/') || !id.match(/\.(ts|tsx|js|jsx)$/)) {
return null;
}
// Check if the file imports logger
if (!code.includes("from 'app/logging/logger'") && !code.includes('from "app/logging/logger"')) {
return null;
}
const s = new MagicString(code);
// Extract relative path from src/
const srcIndex = id.indexOf('/src/');
const relativePath = srcIndex !== -1 ? id.substring(srcIndex + 5) : id.split('/').pop() || 'unknown';
// Match logger calls: logger('namespace')
const loggerRegex = /\blogger\s*\(\s*['"`](\w+)['"`]\s*\)/g;
let match;
while ((match = loggerRegex.exec(code)) !== null) {
const fullMatch = match[0];
const namespace = match[1];
const startIndex = match.index;
const endIndex = startIndex + fullMatch.length;
// Replace with logger('namespace').child({ filePath: 'path/to/file.ts' })
s.overwrite(startIndex, endIndex, `logger('${namespace}').child({ filePath: '${relativePath}' })`);
}
if (s.hasChanged()) {
return {
code: s.toString(),
map: s.generateMap({ hires: true }),
};
}
return null;
},
};
}