Files
OneOS1.2/vite-plugins/utils/moduleSpecifierQuery.ts
王冕 af29b26fe8 Sync OneOS workspace with new prototypes, annotations, and Gitea remote fix.
Add vehicle-h2-fee-ledger, customer-management, lease and self-operated ledgers, annotation sources, agent skills, and vite annotation runtime support. Update vehicle management, contract templates, and lease contract flows.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-30 15:27:23 +08:00

50 lines
2.0 KiB
TypeScript

function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function shouldSkipModuleSpecifier(specifier: string, key: string, value: string): boolean {
const pathOnly = specifier.split(/[?#]/u)[0] || specifier;
return !value
|| !specifier.startsWith('/')
|| pathOnly === '/@react-refresh'
|| pathOnly === '/@vite/client'
|| new RegExp(`[?&]${escapeRegExp(key)}=`).test(specifier);
}
export function appendSearchParamToModuleSpecifier(specifier: string, key: string, value: string): string {
if (shouldSkipModuleSpecifier(specifier, key, value)) {
return specifier;
}
const hashIndex = specifier.indexOf('#');
const withoutHash = hashIndex >= 0 ? specifier.slice(0, hashIndex) : specifier;
const hash = hashIndex >= 0 ? specifier.slice(hashIndex) : '';
const separator = withoutHash.includes('?') ? '&' : '?';
return `${withoutHash}${separator}${encodeURIComponent(key)}=${encodeURIComponent(value)}${hash}`;
}
export function appendSearchParamToModuleSpecifiersInCode(code: string, key: string, value: string): string {
if (!value) {
return code;
}
return code
.replace(
/(\bfrom\s*["'])(\/[^"']+)(["'])/gu,
(_match, prefix: string, specifier: string, suffix: string) =>
`${prefix}${appendSearchParamToModuleSpecifier(specifier, key, value)}${suffix}`,
)
.replace(
/(\bimport\s*["'])(\/[^"']+)(["'])/gu,
(_match, prefix: string, specifier: string, suffix: string) =>
`${prefix}${appendSearchParamToModuleSpecifier(specifier, key, value)}${suffix}`,
)
.replace(
/(\bimport\s*\(\s*["'])(\/[^"']+)(["']\s*\))/gu,
(_match, prefix: string, specifier: string, suffix: string) =>
`${prefix}${appendSearchParamToModuleSpecifier(specifier, key, value)}${suffix}`,
);
}
export function appendProjectIdToModuleSpecifiersInCode(code: string, projectId: string): string {
return appendSearchParamToModuleSpecifiersInCode(code, 'projectId', projectId);
}