fix: address build config review findings

This commit is contained in:
Tianyi Cui
2026-06-20 00:21:57 +08:00
parent c7e55fc0b1
commit ed94daed9e
36 changed files with 88 additions and 21 deletions

View File

@@ -28,6 +28,15 @@ interface PackageManifest {
version?: string
private?: boolean
type?: string
main?: string
types?: string
exports?: {
'.'?: {
types?: string
default?: string
}
}
files?: string[]
peerDependencies?: Record<string, string>
devDependencies?: Record<string, string>
}
@@ -58,6 +67,17 @@ function workspaceManifests(): WorkspaceManifest[] {
return manifests
}
const dshPackageFiles = [
'lib/index.js',
'lib/typings/**/*.d.ts',
'lib/typings/**/*.d.ts.map',
'src',
] as const
function sameStringList(actual: readonly string[] | undefined, expected: readonly string[]): boolean {
return !!actual && actual.length === expected.length && actual.every((value, index) => value === expected[index])
}
function checkWorkspace({ dir, manifest }: WorkspaceManifest): string[] {
const errors: string[] = []
const label = manifest.name ?? dir
@@ -85,6 +105,21 @@ function checkWorkspace({ dir, manifest }: WorkspaceManifest): string[] {
if (manifest.type !== 'module') {
errors.push(`${label}: package.json must set "type": "module"`)
}
if (manifest.main !== 'lib/index.js') {
errors.push(`${label}: package.json must set "main": "lib/index.js"`)
}
if (manifest.types !== 'lib/typings/index.d.ts') {
errors.push(`${label}: package.json must set "types": "lib/typings/index.d.ts"`)
}
if (manifest.exports?.['.']?.types !== './lib/typings/index.d.ts') {
errors.push(`${label}: package.json exports["."].types must be "./lib/typings/index.d.ts"`)
}
if (manifest.exports?.['.']?.default !== './lib/index.js') {
errors.push(`${label}: package.json exports["."].default must be "./lib/index.js"`)
}
if (!sameStringList(manifest.files, dshPackageFiles)) {
errors.push(`${label}: package.json files must be ${JSON.stringify(dshPackageFiles)}`)
}
}
return errors.map(error => `${relative(root, join(root, dir, 'package.json'))}: ${error}`)

View File

@@ -30,6 +30,13 @@ interface Block {
code: string
}
/** Strip JSONC comments from checked-in tsconfig files before JSON.parse. */
function stripJsonComments(raw: string): string {
return raw
.replace(/\/\*[\s\S]*?\*\//g, '')
.replace(/(^|[^:])\/\/.*$/gm, '$1')
}
/** Extract every ```ts / ```ts ignore-check block from one Markdown file. */
function extractBlocks(absPath: string): Block[] {
const text = readFileSync(absPath, 'utf8')
@@ -62,7 +69,7 @@ function extractBlocks(absPath: string): Block[] {
/** Reuse the repo typecheck graph references from a temp project one directory below root. */
function workspaceReferences(): { path: string }[] {
const raw = readFileSync(join(root, 'tsconfig.json'), 'utf8')
const { references } = JSON.parse(raw) as { references: { path: string }[] }
const { references } = JSON.parse(stripJsonComments(raw)) as { references: { path: string }[] }
return references.map(({ path }) => {
const relativeToTemp = path.startsWith('./') ? `../${path.slice(2)}` : `../${path}`
return { path: relativeToTemp }