Skip to content

Commit 327670a

Browse files
committed
test(compiler-ssr): test for ssr element transform
1 parent 8fd9e9b commit 327670a

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { compile } from '../../src'
2+
3+
function getElementString(src: string): string {
4+
return compile(src).code.match(/_push\((.*)\)/)![1]
5+
}
6+
7+
describe('ssr transform element', () => {
8+
test('basic elements', () => {
9+
expect(getElementString(`<div></div>`)).toMatchInlineSnapshot(
10+
`"\`<div></div>\`"`
11+
)
12+
})
13+
14+
test('static attrs', () => {
15+
expect(
16+
getElementString(`<div id="foo" class="bar"></div>`)
17+
).toMatchInlineSnapshot(`"\`<div id=\\"foo\\" class=\\"bar\\"></div>\`"`)
18+
})
19+
20+
test('nested elements', () => {
21+
expect(
22+
getElementString(`<div><span></span><span></span></div>`)
23+
).toMatchInlineSnapshot(`"\`<div><span></span><span></span></div>\`"`)
24+
})
25+
})

packages/compiler-ssr/src/ssrCodegenTransform.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import {
55
TemplateLiteral,
66
createCallExpression,
77
createTemplateLiteral,
8-
locStub,
98
NodeTypes,
109
TemplateChildNode,
11-
ElementTypes
10+
ElementTypes,
11+
createBlockStatement
1212
} from '@vue/compiler-dom'
1313
import { isString } from '@vue/shared'
1414

@@ -30,11 +30,7 @@ export function ssrCodegenTransform(ast: RootNode) {
3030
context.pushStringPart(`<!---->`)
3131
}
3232

33-
ast.codegenNode = {
34-
type: NodeTypes.JS_BLOCK_STATEMENT,
35-
loc: locStub,
36-
body: context.body
37-
}
33+
ast.codegenNode = createBlockStatement(context.body)
3834
}
3935

4036
type SSRTransformContext = ReturnType<typeof createSSRTransformContext>

packages/compiler-ssr/src/transforms/ssrTransformElement.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
TemplateLiteral,
66
createTemplateLiteral
77
} from '@vue/compiler-dom'
8-
import { escapeHtml } from '@vue/server-renderer/src'
8+
import { escapeHtml } from '@vue/server-renderer'
99

1010
/*
1111
## Simple Element

0 commit comments

Comments
 (0)