Skip to content

Commit

Permalink
fix(compiler-ssr): import helpers from correct packages
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 5, 2020
1 parent c441e88 commit 8f6b669
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/compiler-ssr/__tests__/ssrElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('ssr: element', () => {
test('<textarea> with dynamic v-bind', () => {
expect(compile(`<textarea v-bind="obj">fallback</textarea>`).code)
.toMatchInlineSnapshot(`
"const { _renderAttrs, _interpolate } = require(\\"vue\\")
"const { _renderAttrs, _interpolate } = require(\\"@vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent) {
let _temp0
Expand Down
11 changes: 6 additions & 5 deletions packages/compiler-ssr/__tests__/ssrVShow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { compile } from '../src'
describe('ssr: v-show', () => {
test('basic', () => {
expect(compile(`<div v-show="foo"/>`).code).toMatchInlineSnapshot(`
"const { _renderStyle } = require(\\"vue\\")
"const { _renderStyle } = require(\\"@vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent) {
_push(\`<div\${_renderStyle((_ctx.foo) ? null : { display: \\"none\\" })}></div>\`)
Expand All @@ -14,7 +14,7 @@ describe('ssr: v-show', () => {
test('with static style', () => {
expect(compile(`<div style="color:red" v-show="foo"/>`).code)
.toMatchInlineSnapshot(`
"const { _renderStyle } = require(\\"vue\\")
"const { _renderStyle } = require(\\"@vue/server-renderer\\")
const _hoisted_1 = {\\"color\\":\\"red\\"}
Expand All @@ -30,7 +30,7 @@ describe('ssr: v-show', () => {
test('with dynamic style', () => {
expect(compile(`<div :style="{ color: 'red' }" v-show="foo"/>`).code)
.toMatchInlineSnapshot(`
"const { _renderStyle } = require(\\"vue\\")
"const { _renderStyle } = require(\\"@vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent) {
_push(\`<div\${_renderStyle([
Expand All @@ -46,7 +46,7 @@ describe('ssr: v-show', () => {
compile(`<div style="color:red" :style="{ fontSize: 14 }" v-show="foo"/>`)
.code
).toMatchInlineSnapshot(`
"const { _renderStyle } = require(\\"vue\\")
"const { _renderStyle } = require(\\"@vue/server-renderer\\")
const _hoisted_1 = {\\"color\\":\\"red\\"}
Expand All @@ -66,7 +66,8 @@ describe('ssr: v-show', () => {
`<div v-bind="baz" style="color:red" :style="{ fontSize: 14 }" v-show="foo"/>`
).code
).toMatchInlineSnapshot(`
"const { mergeProps, _renderAttrs } = require(\\"vue\\")
"const { mergeProps } = require(\\"vue\\")
const { _renderAttrs } = require(\\"@vue/server-renderer\\")
const _hoisted_1 = {\\"color\\":\\"red\\"}
Expand Down
10 changes: 6 additions & 4 deletions packages/compiler-ssr/src/runtimeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export const SSR_RENDER_ATTR = Symbol(`renderAttr`)
export const SSR_RENDER_DYNAMIC_ATTR = Symbol(`renderDynamicAttr`)
export const SSR_RENDER_LIST = Symbol(`renderList`)

// Note: these are helpers imported from @vue/server-renderer
// make sure the names match!
registerRuntimeHelpers({
export const ssrHelpers = {
[SSR_INTERPOLATE]: `_interpolate`,
[SSR_RENDER_COMPONENT]: `_renderComponent`,
[SSR_RENDER_SLOT]: `_renderSlot`,
Expand All @@ -22,4 +20,8 @@ registerRuntimeHelpers({
[SSR_RENDER_ATTR]: `_renderAttr`,
[SSR_RENDER_DYNAMIC_ATTR]: `_renderDynamicAttr`,
[SSR_RENDER_LIST]: `_renderList`
})
}

// Note: these are helpers imported from @vue/server-renderer
// make sure the names match!
registerRuntimeHelpers(ssrHelpers)
11 changes: 9 additions & 2 deletions packages/compiler-ssr/src/ssrCodegenTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
CallExpression
} from '@vue/compiler-dom'
import { isString, escapeHtml, NO } from '@vue/shared'
import { SSR_INTERPOLATE } from './runtimeHelpers'
import { SSR_INTERPOLATE, ssrHelpers } from './runtimeHelpers'
import { processIf } from './transforms/ssrVIf'
import { processFor } from './transforms/ssrVFor'

Expand All @@ -38,7 +38,14 @@ export function ssrCodegenTransform(ast: RootNode, options: CompilerOptions) {
}

ast.codegenNode = createBlockStatement(context.body)
ast.ssrHelpers = [...context.helpers]

// Finalize helpers.
// We need to separate helpers imported from 'vue' vs. '@vue/server-renderer'
ast.ssrHelpers = [
...ast.helpers.filter(h => h in ssrHelpers),
...context.helpers
]
ast.helpers = ast.helpers.filter(h => !(h in ssrHelpers))
}

export type SSRTransformContext = ReturnType<typeof createSSRTransformContext>
Expand Down

0 comments on commit 8f6b669

Please sign in to comment.