Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dx(compiler-dom): move ssr specific template warning to compiler-ssr (fix #12088) #12704

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/compiler-dom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ import { transformShow } from './transforms/vShow'
import { transformTransition } from './transforms/Transition'
import { stringifyStatic } from './transforms/stringifyStatic'
import { ignoreSideEffectTags } from './transforms/ignoreSideEffectTags'
import { validateHtmlNesting } from './transforms/validateHtmlNesting'
import { extend } from '@vue/shared'

export { parserOptions }

export const DOMNodeTransforms: NodeTransform[] = [
transformStyle,
...(__DEV__ ? [transformTransition, validateHtmlNesting] : []),
...(__DEV__ ? [transformTransition] : []),
]

export const DOMDirectiveTransforms: Record<string, DirectiveTransform> = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type CompilerError, compile } from '../../src'
import type { CompilerError } from '@vue/compiler-core'
import { compile } from '@vue/compiler-ssr'

describe('validate html nesting', () => {
it('should warn with p > div', () => {
Expand All @@ -7,7 +8,7 @@ describe('validate html nesting', () => {
onWarn: e => (err = e),
})
expect(err).toBeDefined()
expect(err!.message).toMatch(`<div> cannot be child of <p>`)
expect(err!.message).toMatch(`<div> as a child of <p>`)
})

it('should not warn with select > hr', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
* To avoid runtime dependency on validate-html-nesting
* This file should not change very often in the original repo
* but we may need to keep it up-to-date from time to time.
*
* The parent-child nesting is considered valid if the Browser
* does not modify it, regardless of whether or not the HTML spec
* considers it valid or invalid. So, the library is purely for
* detecting the kind of element nesting which result in altered DOM.
*
*/

/**
* returns true if given parent-child nesting is valid HTML
* returns true if given parent-child nesting is not known to result
* in an altered DOM
*/
export function isValidHTMLNesting(parent: string, child: string): boolean {
// if we know the list of children that are the only valid children for the given parent
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler-ssr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { ssrTransformModel } from './transforms/ssrVModel'
import { ssrTransformShow } from './transforms/ssrVShow'
import { ssrInjectFallthroughAttrs } from './transforms/ssrInjectFallthroughAttrs'
import { ssrInjectCssVars } from './transforms/ssrInjectCssVars'
import { validateHtmlNesting } from './transforms/validateHtmlNesting'

export function compile(
source: string | RootNode,
Expand Down Expand Up @@ -66,6 +67,7 @@ export function compile(
ssrTransformComponent,
trackSlotScopes,
transformStyle,
...(__DEV__ ? [validateHtmlNesting] : []),
...(options.nodeTransforms || []), // user transforms
],
directiveTransforms: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const validateHtmlNesting: NodeTransform = (node, context) => {
!isValidHTMLNesting(context.parent.tag, node.tag)
) {
const error = new SyntaxError(
`<${node.tag}> cannot be child of <${context.parent.tag}>, ` +
'according to HTML specifications. ' +
`<${node.tag}> as a child of <${context.parent.tag}> ` +
'might result in the browser modifying the DOM. ' +
'This can cause hydration errors or ' +
'potentially disrupt future functionality.',
) as CompilerError
Expand Down
10 changes: 7 additions & 3 deletions packages/server-renderer/src/helpers/ssrCompile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export function ssrCompile(
return cached
}

finalCompilerOptions.onError = (err: CompilerError) => {
if (__DEV__) {
if (__DEV__) {
const compilationErrorHandler = (err: CompilerError) => {
const message = `[@vue/server-renderer] Template compilation error: ${err.message}`
const codeFrame =
err.loc &&
Expand All @@ -78,7 +78,11 @@ export function ssrCompile(
err.loc.end.offset,
)
warn(codeFrame ? `${message}\n${codeFrame}` : message)
} else {
}
finalCompilerOptions.onWarn = compilationErrorHandler
finalCompilerOptions.onError = compilationErrorHandler
} else {
finalCompilerOptions.onError = (err: CompilerError) => {
throw err
}
}
Expand Down
Loading