Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Jun 11, 2024
1 parent 30a0969 commit fac6b8d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
16 changes: 8 additions & 8 deletions vike/shared/page-configs/assertPlusFileExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ const EXPORTS_IGNORE = [
const TOLERATE_SIDE_EXPORTS = ['.md', '.mdx'] as const

function assertPlusFileExport(fileExports: Record<string, unknown>, filePathToShowToUser: string, configName: string) {
const exportsAll = Object.keys(fileExports).filter((exportName) => !EXPORTS_IGNORE.includes(exportName))
const exportsInvalid = exportsAll.filter((e) => e !== 'default' && e !== configName)
if (exportsInvalid.length === 0) {
if (exportsAll.length === 1) {
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 (exportsAll.length === 0) {
if (exportNames.length === 0) {
assertUsage(
false,
`${filePathToShowToUser} doesn't export any value, but it should have a ${exportNamed} or ${exportDefault}`
)
} else {
assert(exportsAll.length === 2) // because `exportsInvalid.length === 0`
assert(exportNames.length === 2) // because `exportsInvalid.length === 0`
assertWarning(
false,
`The exports of ${filePathToShowToUser} are ambiguous: remove ${exportDefault} or ${exportNamed}`,
Expand All @@ -37,12 +37,12 @@ function assertPlusFileExport(fileExports: Record<string, unknown>, filePathToSh
}
} else {
if (TOLERATE_SIDE_EXPORTS.some((ext) => filePathToShowToUser.endsWith(ext))) return
exportsInvalid.forEach((exportInvalid) => {
exportNamesInvalid.forEach((exportInvalid) => {
assertWarning(
false,
`${filePathToShowToUser} should have only one export: move ${pc.cyan(
`export { ${exportInvalid} }`
)} to its own +${exportsInvalid}.js file`,
)} to its own +${exportNamesInvalid}.js file`,
{ onlyOnce: true }
)
})
Expand Down
1 change: 1 addition & 0 deletions vike/shared/page-configs/serialize/parsePageConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ function parseValueSerialized(
})
}
})
// Already validated by assertPlusFileExport() call above.
assert(valueWasFound)
return { value, sideExports }
}
Expand Down

0 comments on commit fac6b8d

Please sign in to comment.