Skip to content

Commit

Permalink
Hide private shared modules (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Nov 24, 2024
1 parent 43ab296 commit 47a6cc6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
31 changes: 26 additions & 5 deletions src/plugins/output-state-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
getSpecialExportTypeFromComposedExportPath,
normalizeExportPath,
} from '../entries'
import { isBinExportPath, isTypeFile } from '../utils'
import { isBinExportPath, isPrivateExportPath, isTypeFile } from '../utils'

// [filename, sourceFileName, size]
type FileState = [string, string, number]
Expand Down Expand Up @@ -156,22 +156,43 @@ function logOutputState(stats: SizeStats) {
const [filename, , size] = item
const normalizedExportName = normalizeExportName(exportName)

const prefix =
const exportNameWithPadding =
index === 0
? normalizedExportName
? // Each exports, only show the export path in the first item.
// For other formats, just show the padding spaces.
normalizedExportName
: ' '.repeat(normalizedExportName.length)
const filenamePadding = ' '.repeat(
Math.max(maxLengthOfExportName, 'Exports'.length) -
normalizedExportName.length,
)
const isType = isTypeFile(filename)

const prettiedSize = prettyBytes(size)
const sizePadding = ' '.repeat(
Math.max(maxFilenameLength, 'File'.length) - filename.length,
)
const prettiedSize = prettyBytes(size)

// Logging shared in debug mode
if (isPrivateExportPath(exportName)) {
if (index === 0 && process.env.DEBUG) {
const sizePadding = ' '.repeat(
Math.max(maxFilenameLength, 'File'.length) -
'private shared'.length,
)
console.log(
pc.dim(normalizeExportName(exportName)),
filenamePadding,
pc.dim('private shared'),
sizePadding,
pc.dim(prettiedSize),
)
}
return
}

console.log(
prefix,
exportNameWithPadding,
filenamePadding,
`${pc[isType ? 'dim' : 'bold'](filename)}`,
sizePadding,
Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,12 @@ export function sourceFilenameToExportFullPath(filename: string) {

return relativify(exportPath)
}

// If the file is matching the private module convention file export path.
// './lib/_foo' -> true
// './_util/index' -> true
// './lib/_foo/bar' -> true
// './foo' -> false
export function isPrivateExportPath(exportPath: string) {
return /\/_/.test(exportPath)
}
5 changes: 4 additions & 1 deletion test/integration/shared-module/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('integration shared-module', () => {
{
directory: __dirname,
},
async ({ distDir }) => {
async ({ distDir, stdout }) => {
const jsFiles = await getFileNamesFromDirectory(distDir)
expect(jsFiles).toEqual([
'_internal/index.cjs',
Expand All @@ -35,6 +35,9 @@ describe('integration shared-module', () => {
'index.react-server.js': `'./_internal/index.react-server.js'`,
'./_internal/index.react-server.js': 'internal:react-server',
})

// Hide private shared module
expect(stdout).not.toContain('./lib/_util')
},
)
})
Expand Down

0 comments on commit 47a6cc6

Please sign in to comment.