Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix output format #472

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions src/plugins/output-state-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import path from 'path'
import prettyBytes from 'pretty-bytes'
import pc from 'picocolors'
import { type Entries } from '../types'
import { relativify } from '../lib/format'
import { logger } from '../logger'
import { BINARY_TAG } from '../constants'
import {
getSpecialExportTypeFromExportPath,
normalizeExportPath,
} from '../entries'

type Pair = [string, string, number]
type SizeStats = Map<string, Pair[]>
Expand Down Expand Up @@ -79,7 +83,7 @@ function createOutputState({ entries }: { entries: Entries }): {
}

function isBin(filename: string) {
return filename === 'bin' || filename.startsWith('bin/')
return filename === BINARY_TAG || filename.startsWith(BINARY_TAG + '/')
}

function isTypeFile(filename: string) {
Expand All @@ -94,25 +98,17 @@ function normalizeExportName(exportName: string): string {
const isBinary = isBin(exportName)
let result = exportName

const isSubpathExport = exportName.includes('/')
const isSpecialExport = exportName.includes('.')
if (isBinary) {
result = (exportName.replace(/bin(\/|$)/, '') || '.') + ' (bin)'
} else if (isSubpathExport || isSpecialExport) {
const subExportName: string | undefined =
exportName.split('/')[1] || exportName
if (subExportName.includes('.') && subExportName !== '.') {
const [originExportName, specialCondition] = subExportName.split('.')
result =
(isSubpathExport ? relativify(originExportName) : '.') +
' (' +
specialCondition +
')'
} else {
result = isSubpathExport ? relativify(subExportName) : '.'
}
result =
(exportName.replace(new RegExp(`^\\${BINARY_TAG}\\/?`), '') || '.') +
' (bin)'
} else {
result = '.'
const normalizedExportPath = normalizeExportPath(exportName)
const specialConditionName = getSpecialExportTypeFromExportPath(exportName)

result =
normalizedExportPath +
(specialConditionName !== 'default' ? ` (${specialConditionName})` : '')
}
return result
}
Expand Down Expand Up @@ -171,6 +167,7 @@ function logOutputState(sizeCollector: ReturnType<typeof createOutputState>) {
.forEach((item: Pair, index) => {
const [filename, , size] = item
const normalizedExportName = normalizeExportName(exportName)

const prefix =
index === 0
? normalizedExportName
Expand Down
12 changes: 10 additions & 2 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,16 @@ const testCases: {
*/

const lines = stripANSIColor(stdout).split('\n')
const [tableHeads, cliLine, indexLine, indexReactServerLine, fooLine] =
lines
const [tableHeads, ...restLines] = lines
const cliLine = restLines.find((line) => line.includes('cli'))!
const indexLine = restLines.find(
(line) => line.includes('. ') && !line.includes('react-server'),
)!
const indexReactServerLine = restLines.find((line) =>
line.includes('. (react-server)'),
)!
const fooLine = restLines.find((line) => line.includes('./foo'))!

expect(tableHeads).toContain('Exports')
expect(tableHeads).toContain('File')
expect(tableHeads).toContain('Size')
Expand Down
Loading