Skip to content

Commit

Permalink
fix: improve hint for ESM/CJS errors
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Feb 24, 2024
1 parent 16b07b3 commit 7353aa0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
18 changes: 5 additions & 13 deletions vike/node/runtime/renderPage/logErrorHint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,7 @@ function getHint(error: unknown): null | string {

const res = isCjsEsmError(error)
if (res) {
const packageNames = res === true ? null : res
const hint = [
'Error could be a CJS/ESM issue, consider ',
!packageNames || packageNames.length === 0
? 'using'
: `adding ${joinEnglish(
packageNames!.map((p) => pc.cyan(p)),
'or'
)} to`,
` ${pc.cyan('ssr.noExternal')}`,
', see https://vike.dev/broken-npm-package'
].join('')
const hint = 'The error seems to be a CJS/ESM issue, see https://vike.dev/broken-npm-package'
return hint
}

Expand All @@ -76,15 +65,18 @@ function isKnownError(error: unknown): false | string {
// `false` -> noop
// `true` -> generic message
// `'some-npm-package'` -> add some-npm-package to `ssr.noExternal`
function isCjsEsmError(error: unknown): boolean | string[] {
function isCjsEsmError(error: unknown): boolean {
const res = check(error)
if (res === true || res === false) return res
const packageNames = normalizeRes(res)
if (packageNames === false) return packageNames
packageNames.forEach((packageName) => {
assert(!['vite', 'vike'].includes(packageName))
})
/* Not needed anymore. Shall we remove returning a list of npm packages?
return packageNames
*/
return true
}
function normalizeRes(res: string | string[]): string[] | false {
let packageNames: string[] = Array.isArray(res) ? res : [res]
Expand Down
4 changes: 1 addition & 3 deletions vike/node/runtime/renderPage/logErrorHint/log.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ describe('getHint()', () => {
"Cannot find module 'node_modules/vike-react/dist/renderer/getPageElement' imported from node_modules/vike-react/dist/renderer/onRenderHtml.js",
code: 'ERR_MODULE_NOT_FOUND'
})
).toMatchInlineSnapshot(
'"Error could be a CJS/ESM issue, consider adding vike-react to ssr.noExternal, see https://vike.dev/broken-npm-package"'
)
).toMatchInlineSnapshot('"The error seems to be a CJS/ESM issue, see https://vike.dev/broken-npm-package"')

expect(
getLog({
Expand Down

0 comments on commit 7353aa0

Please sign in to comment.