cc-haha/desktop/src/lib/htmlPreviewPolicy.ts
程序员阿江(Relakkes) ebc78befbe fix: avoid static previews for frontend index HTML
Project entry HTML often only mounts a dev-server app, while built output can be served statically. The desktop open-with policy now only offers the in-app browser for static HTML candidates, and preview-fs rewrites built HTML root-relative assets under the workspace preview path.

Constraint: Frontend project index.html needs a dev server instead of preview-fs static serving
Rejected: Treat every .html file as browser-previewable | project entry HTML can render blank or with missing assets
Confidence: high
Scope-risk: moderate
Tested: cd desktop && bun run check:desktop
Tested: bun run check:server
Tested: git diff --check
Not-tested: bun run verify was intentionally stopped at user request
2026-06-01 03:16:52 +08:00

37 lines
873 B
TypeScript

const HTML_EXT = /\.(html?|xhtml)$/i
const INDEX_HTML_RE = /(^|\/)index\.html?$/i
const STATIC_OUTPUT_DIRS = new Set([
'build',
'coverage',
'dist',
'docs',
'lcov-report',
'out',
'public',
'site',
'storybook-static',
])
function normalizePathForPolicy(filePath: string): string {
return filePath
.split(/[?#]/, 1)[0]!
.replace(/\\/g, '/')
.replace(/^\.\//, '')
}
export function isHtmlFilePath(filePath: string): boolean {
return HTML_EXT.test(normalizePathForPolicy(filePath))
}
export function shouldOfferStaticHtmlPreview(filePath: string): boolean {
const normalized = normalizePathForPolicy(filePath)
if (!HTML_EXT.test(normalized)) return false
if (!INDEX_HTML_RE.test(normalized)) return true
return normalized
.split('/')
.filter(Boolean)
.some((segment) => STATIC_OUTPUT_DIRS.has(segment.toLowerCase()))
}