Skip to content

Commit

Permalink
fix: fix export assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Jun 11, 2024
1 parent fac6b8d commit d4c1df7
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions vike/shared/page-configs/assertPlusFileExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,32 @@ const TOLERATE_SIDE_EXPORTS = ['.md', '.mdx'] as const

function assertPlusFileExport(fileExports: Record<string, unknown>, filePathToShowToUser: string, configName: string) {
const exportNames = Object.keys(fileExports).filter((exportName) => !EXPORTS_IGNORE.includes(exportName))
const exportNamesInvalid = exportNames.filter((e) => e !== 'default' && e !== configName)
if (exportNamesInvalid.length === 0) {
if (exportNames.length === 1) {
return
}
const exportDefault = pc.cyan('export default')
const exportNamed = pc.cyan(`export { ${configName} }`)
if (exportNames.length === 0) {
assertUsage(
false,
`${filePathToShowToUser} doesn't export any value, but it should have a ${exportNamed} or ${exportDefault}`
)
} else {
assert(exportNames.length === 2) // because `exportsInvalid.length === 0`
assertWarning(
false,
`The exports of ${filePathToShowToUser} are ambiguous: remove ${exportDefault} or ${exportNamed}`,
{ onlyOnce: true }
)
}
} else {
if (TOLERATE_SIDE_EXPORTS.some((ext) => filePathToShowToUser.endsWith(ext))) return
const isValid = (exportName: string) => exportName === 'default' || exportName === configName
const exportNamesValid = exportNames.filter(isValid)
const exportNamesInvalid = exportNames.filter((e) => !isValid(e))

if (exportNamesValid.length === 1 && exportNamesInvalid.length === 0) {
return
}

const exportDefault = pc.code('export default')
const exportNamed = pc.code(`export { ${configName} }`)
if (exportNamesValid.length === 0) {
assertUsage(false, `${filePathToShowToUser} should have a ${exportNamed} or ${exportDefault}`)
}
if (exportNamesValid.length === 2) {
assertWarning(false, `${filePathToShowToUser} is ambiguous: remove ${exportDefault} or ${exportNamed}`, {
onlyOnce: true
})
}

assert(exportNamesValid.length === 1)
assert(exportNamesInvalid.length > 0)
if (!TOLERATE_SIDE_EXPORTS.some((ext) => filePathToShowToUser.endsWith(ext))) {
exportNamesInvalid.forEach((exportInvalid) => {
assertWarning(
false,
`${filePathToShowToUser} should have only one export: move ${pc.cyan(
`export { ${exportInvalid} }`
)} to its own +${exportNamesInvalid}.js file`,
{ onlyOnce: true }
)
assertWarning(false, `${filePathToShowToUser} unexpected ${pc.cyan(`export { ${exportInvalid} }`)}`, {
onlyOnce: true
})
})
}
}

0 comments on commit d4c1df7

Please sign in to comment.