Skip to content

Commit 972cab4

Browse files
committed
dx(compiler-dom): move ssr specific template warning to compiler-ssr (fix #12088)
1 parent f399dd3 commit 972cab4

File tree

6 files changed

+23
-10
lines changed

6 files changed

+23
-10
lines changed

packages/compiler-dom/src/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ import { transformShow } from './transforms/vShow'
1919
import { transformTransition } from './transforms/Transition'
2020
import { stringifyStatic } from './transforms/stringifyStatic'
2121
import { ignoreSideEffectTags } from './transforms/ignoreSideEffectTags'
22-
import { validateHtmlNesting } from './transforms/validateHtmlNesting'
2322
import { extend } from '@vue/shared'
2423

2524
export { parserOptions }
2625

2726
export const DOMNodeTransforms: NodeTransform[] = [
2827
transformStyle,
29-
...(__DEV__ ? [transformTransition, validateHtmlNesting] : []),
28+
...(__DEV__ ? [transformTransition] : []),
3029
]
3130

3231
export const DOMDirectiveTransforms: Record<string, DirectiveTransform> = {

packages/compiler-dom/__tests__/transforms/validateHtmlNesting.spec.ts packages/compiler-ssr/__tests__/validateHtmlNesting.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { type CompilerError, compile } from '../../src'
1+
import type { CompilerError } from '@vue/compiler-core'
2+
import { compile } from '@vue/compiler-ssr'
23

34
describe('validate html nesting', () => {
45
it('should warn with p > div', () => {
@@ -7,7 +8,7 @@ describe('validate html nesting', () => {
78
onWarn: e => (err = e),
89
})
910
expect(err).toBeDefined()
10-
expect(err!.message).toMatch(`<div> cannot be child of <p>`)
11+
expect(err!.message).toMatch(`<div> as a child of <p>`)
1112
})
1213

1314
it('should not warn with select > hr', () => {

packages/compiler-dom/src/htmlNesting.ts packages/compiler-ssr/src/htmlNesting.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
* To avoid runtime dependency on validate-html-nesting
66
* This file should not change very often in the original repo
77
* but we may need to keep it up-to-date from time to time.
8+
*
9+
* The parent-child nesting is considered valid if the Browser
10+
* does not modify it, regardless of whether or not the HTML spec
11+
* considers it valid or invalid. So, the library is purely for
12+
* detecting the kind of element nesting which result in altered DOM.
13+
*
814
*/
915

1016
/**
11-
* returns true if given parent-child nesting is valid HTML
17+
* returns true if given parent-child nesting is not known to result
18+
* in an altered DOM
1219
*/
1320
export function isValidHTMLNesting(parent: string, child: string): boolean {
1421
// if we know the list of children that are the only valid children for the given parent

packages/compiler-ssr/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { ssrTransformModel } from './transforms/ssrVModel'
2727
import { ssrTransformShow } from './transforms/ssrVShow'
2828
import { ssrInjectFallthroughAttrs } from './transforms/ssrInjectFallthroughAttrs'
2929
import { ssrInjectCssVars } from './transforms/ssrInjectCssVars'
30+
import { validateHtmlNesting } from './transforms/validateHtmlNesting'
3031

3132
export function compile(
3233
source: string | RootNode,
@@ -66,6 +67,7 @@ export function compile(
6667
ssrTransformComponent,
6768
trackSlotScopes,
6869
transformStyle,
70+
...(__DEV__ ? [validateHtmlNesting] : []),
6971
...(options.nodeTransforms || []), // user transforms
7072
],
7173
directiveTransforms: {

packages/compiler-dom/src/transforms/validateHtmlNesting.ts packages/compiler-ssr/src/transforms/validateHtmlNesting.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export const validateHtmlNesting: NodeTransform = (node, context) => {
1616
!isValidHTMLNesting(context.parent.tag, node.tag)
1717
) {
1818
const error = new SyntaxError(
19-
`<${node.tag}> cannot be child of <${context.parent.tag}>, ` +
20-
'according to HTML specifications. ' +
19+
`<${node.tag}> as a child of <${context.parent.tag}> ` +
20+
'might result in the browser modifying the DOM. ' +
2121
'This can cause hydration errors or ' +
2222
'potentially disrupt future functionality.',
2323
) as CompilerError

packages/server-renderer/src/helpers/ssrCompile.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ export function ssrCompile(
6767
return cached
6868
}
6969

70-
finalCompilerOptions.onError = (err: CompilerError) => {
71-
if (__DEV__) {
70+
if (__DEV__) {
71+
const compilationErrorHandler = (err: CompilerError) => {
7272
const message = `[@vue/server-renderer] Template compilation error: ${err.message}`
7373
const codeFrame =
7474
err.loc &&
@@ -78,7 +78,11 @@ export function ssrCompile(
7878
err.loc.end.offset,
7979
)
8080
warn(codeFrame ? `${message}\n${codeFrame}` : message)
81-
} else {
81+
}
82+
finalCompilerOptions.onWarn = compilationErrorHandler
83+
finalCompilerOptions.onError = compilationErrorHandler
84+
} else {
85+
finalCompilerOptions.onError = (err: CompilerError) => {
8286
throw err
8387
}
8488
}

0 commit comments

Comments
 (0)