Skip to content

Commit 2296914

Browse files
committedSep 4, 2023
fix: further use colors instead of backtick in error messages
1 parent a052d52 commit 2296914

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed
 

‎vite-plugin-ssr/node/prerender/runPrerender.ts

+35-15
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ async function runPrerender(
180180
assert(manuallyTriggered)
181181
assertWarning(
182182
prerenderConfig,
183-
`You're executing \`${manuallyTriggered}\` but the config \`prerender\` isn't set to true`,
183+
`You're executing ${pc.cyan(manuallyTriggered)} but the config ${pc.cyan('prerender')} isn't set to true`,
184184
{
185185
onlyOnce: true
186186
}
@@ -625,7 +625,9 @@ async function callOnPrerenderStartHook(
625625
}
626626

627627
const errPrefix = `The ${hookName}() hook exported by ${hookFilePath}`
628-
const rightUsage = `${errPrefix} should return \`null\`, \`undefined\`, or \`{ prerenderContext: { pageContexts } }\`.`
628+
const rightUsage = `${errPrefix} should return ${pc.cyan('null')}, ${pc.cyan('undefined')}, or ${pc.cyan(
629+
'{ prerenderContext: { pageContexts } }'
630+
)}`
629631

630632
// TODO/v1-release: remove
631633
if (hasProp(result, 'globalContext')) {
@@ -637,7 +639,9 @@ async function callOnPrerenderStartHook(
637639
)
638640
assertWarning(
639641
false,
640-
`${errPrefix} returns \`{ globalContext: { prerenderPageContexts } }\` but the return value has been renamed to \`{ prerenderContext: { pageContexts } }\`, see ${docLink}`,
642+
`${errPrefix} returns ${pc.cyan(
643+
'{ globalContext: { prerenderPageContexts } }'
644+
)} but the return value has been renamed to ${pc.cyan('{ prerenderContext: { pageContexts } }')}, see ${docLink}`,
641645
{ onlyOnce: true }
642646
)
643647
result = {
@@ -809,7 +813,9 @@ function warnMissingPages(
809813
const pageAt = getPageAt(pageId)
810814
assertWarning(
811815
partial,
812-
`Cannot pre-render page ${pageAt} because it has a non-static route, and no ${hookName}() hook returned (an) URL(s) matching the page's route. Either use a ${hookName}() hook to pre-render the page, or use the option \`prerender.partial\` to suppress this warning, see https://vite-plugin-ssr.com/prerender-config`,
816+
`Cannot pre-render page ${pageAt} because it has a non-static route, and no ${hookName}() hook returned (an) URL(s) matching the page's route. Either use a ${hookName}() hook to pre-render the page, or use the option ${pc.cyan(
817+
'prerender.partial'
818+
)} to suppress this warning, see https://vite-plugin-ssr.com/prerender-config`,
813819
{ onlyOnce: true }
814820
)
815821
})
@@ -955,31 +961,39 @@ function normalizeOnPrerenderHookResult(
955961

956962
const errMsg1 = `The ${hookName}() hook defined by ${prerenderHookFile} returned` as const
957963
const errMsg2 = `${errMsg1} an invalid value` as const
958-
const errHint =
959-
`Make sure your ${hookName}() hook returns an object \`{ url, pageContext }\` or an array of such objects.` as const
964+
const errHint = `Make sure your ${hookName}() hook returns an object ${pc.cyan(
965+
'{ url, pageContext }'
966+
)} or an array of such objects.` as const
960967
assertUsage(isPlainObject(prerenderElement), `${errMsg2}. ${errHint}`)
961-
assertUsage(hasProp(prerenderElement, 'url'), `${errMsg2}: \`url\` is missing. ${errHint}`)
968+
assertUsage(hasProp(prerenderElement, 'url'), `${errMsg2}: ${pc.cyan('url')} is missing. ${errHint}`)
962969
assertUsage(
963970
hasProp(prerenderElement, 'url', 'string'),
964-
`${errMsg2}: \`url\` should be a string (but \`typeof url === "${typeof prerenderElement.url}"\`).`
971+
`${errMsg2}: ${pc.cyan('url')} should be a string (but ${pc.cyan(
972+
`typeof url === "${typeof prerenderElement.url}"`
973+
)}).`
965974
)
966975
assertUsage(
967976
prerenderElement.url.startsWith('/'),
968977
`${errMsg1} a URL with an invalid value '${prerenderElement.url}' which doesn't start with '/'. Make sure each URL starts with '/'.`
969978
)
970979
Object.keys(prerenderElement).forEach((key) => {
971-
assertUsage(key === 'url' || key === 'pageContext', `${errMsg2}: unexpected object key \`${key}\`. ${errHint}`)
980+
assertUsage(
981+
key === 'url' || key === 'pageContext',
982+
`${errMsg2}: unexpected object key ${pc.cyan(key)}. ${errHint}`
983+
)
972984
})
973985
if (!hasProp(prerenderElement, 'pageContext')) {
974986
prerenderElement.pageContext = null
975987
} else if (!hasProp(prerenderElement, 'pageContext', 'null')) {
976988
assertUsage(
977989
hasProp(prerenderElement, 'pageContext', 'object'),
978-
`${errMsg1} an invalid \`pageContext\` value: make sure \`pageContext\` is an object.`
990+
`${errMsg1} an invalid ${pc.cyan('pageContext')} value: make sure ${pc.cyan('pageContext')} is an object.`
979991
)
980992
assertUsage(
981993
isPlainObject(prerenderElement.pageContext),
982-
`${errMsg1} an invalid \`pageContext\` object: make sure \`pageContext\` is a plain JavaScript object.`
994+
`${errMsg1} an invalid ${pc.cyan('pageContext')} object: make sure ${pc.cyan(
995+
'pageContext'
996+
)} is a plain JavaScript object.`
983997
)
984998
}
985999
assert(hasProp(prerenderElement, 'pageContext', 'object') || hasProp(prerenderElement, 'pageContext', 'null'))
@@ -1009,14 +1023,18 @@ function checkOutdatedOptions(options: {
10091023
;(['noExtraDir', 'partial', 'parallel'] as const).forEach((prop) => {
10101024
assertUsage(
10111025
options[prop] === undefined,
1012-
`[prerender()] Option \`${prop}\` is deprecated. Define \`${prop}\` in \`vite.config.js\` instead. See https://vite-plugin-ssr.com/prerender-config`,
1026+
`[prerender()] Option ${pc.cyan(prop)} is deprecated. Define ${pc.cyan(
1027+
prop
1028+
)} in vite.config.js instead. See https://vite-plugin-ssr.com/prerender-config`,
10131029
{ showStackTrace: true }
10141030
)
10151031
})
10161032
;(['base', 'outDir'] as const).forEach((prop) => {
10171033
assertWarning(
10181034
options[prop] === undefined,
1019-
`[prerender()] Option \`${prop}\` is outdated and has no effect (vite-plugin-ssr now automatically determines \`${prop}\`)`,
1035+
`[prerender()] Option ${pc.cyan(
1036+
prop
1037+
)} is outdated and has no effect (vite-plugin-ssr now automatically determines ${pc.cyan(prop)})`,
10201038
{
10211039
showStackTrace: true,
10221040
onlyOnce: true
@@ -1045,12 +1063,14 @@ function assertLoadedConfig(
10451063
}
10461064
const { configFile } = viteConfig
10471065
if (configFile) {
1048-
assertUsage(false, `${configFile} is missing vite-plugin-ssr. Add vite-plugin-ssr to \`${configFile}\`.`)
1066+
assertUsage(false, `${configFile} doesn't install vite-plugin-ssr`)
10491067
} else {
10501068
if (!options.viteConfig) {
10511069
assertUsage(
10521070
false,
1053-
`[prerender()] No \`vite.config.js\` file found at \`${process.cwd()}\`. Use the option \`prerender({ viteConfig })\`.`,
1071+
`[prerender()] No vite.config.js file found at ${process.cwd()}. Use the option ${pc.cyan(
1072+
'prerender({ viteConfig })'
1073+
)}.`,
10541074
{ showStackTrace: true }
10551075
)
10561076
} else {

‎vite-plugin-ssr/node/runtime/renderPage/executeOnRenderHtmlHook.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import type { PageConfig } from '../../../shared/page-configs/PageConfig.js'
3232
import { assertHookReturnedObject } from '../../../shared/assertHookReturnedObject.js'
3333
import { logRuntimeError } from './loggerRuntime.js'
3434
import type { PageContextSerialization } from '../html/serializePageContextClientSide.js'
35+
import pc from '@brillout/picocolors'
3536

3637
type GetPageAssets = () => Promise<PageAsset[]>
3738

@@ -156,8 +157,11 @@ function processHookReturnValue(hookReturnValue: unknown, renderHook: RenderHook
156157
}
157158

158159
const errPrefix = `The ${renderHook.hookName as string}() hook defined at ${renderHook.hookFilePath}` as const
159-
const errSuffix =
160-
'a string generated with the escapeInject`<html>...</html>` template tag or a string returned by dangerouslySkipEscape(), see https://vite-plugin-ssr.com/escapeInject' as const
160+
const errSuffix = `a string generated with the ${pc.cyan(
161+
'escapeInject`<html>...</html>`'
162+
)} template tag or a string returned by ${pc.cyan(
163+
'dangerouslySkipEscape()'
164+
)}, see https://vite-plugin-ssr.com/escapeInject` as const
161165
if (typeof hookReturnValue === 'string') {
162166
assertWarning(
163167
false,
@@ -172,7 +176,11 @@ function processHookReturnValue(hookReturnValue: unknown, renderHook: RenderHook
172176
isObject(hookReturnValue),
173177
[
174178
errPrefix,
175-
'should return `null`, a value `documentHtml`, or an object `{ documentHtml, pageContext }` where `pageContext` is `undefined` or an object holding additional pageContext values, and where `documentHtml` is',
179+
`should return ${pc.cyan('null')}, the value ${pc.cyan('documentHtml')}, or an object ${pc.cyan(
180+
'{ documentHtml, pageContext }'
181+
)} where ${pc.cyan('pageContext')} is ${pc.cyan(
182+
'undefined'
183+
)} or an object holding additional pageContext values, and where ${pc.cyan('documentHtml')} is`,
176184
errSuffix
177185
].join(' ')
178186
)
@@ -185,11 +193,15 @@ function processHookReturnValue(hookReturnValue: unknown, renderHook: RenderHook
185193

186194
if (hookReturnValue.documentHtml) {
187195
let val = hookReturnValue.documentHtml
188-
const errBegin = `${errPrefix} returned \`{ documentHtml }\`, but documentHtml`
196+
const errBegin = `${errPrefix} returned ${pc.cyan('{ documentHtml }')}, but ${pc.cyan('documentHtml')}` as const
189197
if (typeof val === 'string') {
190198
assertWarning(
191199
false,
192-
[errBegin, 'is a plain JavaScript string which is dangerous: documentHtml should be', errSuffix].join(' '),
200+
[
201+
errBegin,
202+
`is a plain JavaScript string which is dangerous: ${pc.cyan('documentHtml')} should be`,
203+
errSuffix
204+
].join(' '),
193205
{ onlyOnce: true }
194206
)
195207
val = dangerouslySkipEscape(val)
@@ -200,7 +212,7 @@ function processHookReturnValue(hookReturnValue: unknown, renderHook: RenderHook
200212

201213
if (hookReturnValue.pageContext) {
202214
const val = hookReturnValue.pageContext
203-
const errBegin = `${errPrefix} returned \`{ pageContext }\`, but pageContext`
215+
const errBegin = `${errPrefix} returned ${pc.cyan('{ pageContext }')}, but ${pc.cyan('pageContext')}`
204216
if (isPromise(val) || isCallable(val)) {
205217
assertWarning(
206218
!isPromise(val),

0 commit comments

Comments
 (0)
Please sign in to comment.