-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ssr): compiler-ssr support for Suspense
- Loading branch information
Showing
10 changed files
with
396 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { compile } from '../src' | ||
|
||
describe('ssr compile: portal', () => { | ||
test('should work', () => { | ||
expect(compile(`<portal :target="target"><div/></portal>`).code) | ||
.toMatchInlineSnapshot(` | ||
"const { ssrRenderPortal: _ssrRenderPortal } = require(\\"@vue/server-renderer\\") | ||
return function ssrRender(_ctx, _push, _parent) { | ||
_ssrRenderPortal((_push) => { | ||
_push(\`<div></div>\`) | ||
}, _ctx.target, _parent) | ||
}" | ||
`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { compile } from '../src' | ||
|
||
describe('ssr compile: suspense', () => { | ||
test('implicit default', () => { | ||
expect(compile(`<suspense><foo/></suspense>`).code).toMatchInlineSnapshot(` | ||
"const { resolveComponent: _resolveComponent } = require(\\"vue\\") | ||
const { ssrRenderComponent: _ssrRenderComponent, ssrRenderSuspense: _ssrRenderSuspense } = require(\\"@vue/server-renderer\\") | ||
return function ssrRender(_ctx, _push, _parent) { | ||
const _component_foo = _resolveComponent(\\"foo\\") | ||
_push(_ssrRenderSuspense({ | ||
default: (_push) => { | ||
_push(_ssrRenderComponent(_component_foo, null, null, _parent)) | ||
}, | ||
_: 1 | ||
})) | ||
}" | ||
`) | ||
}) | ||
|
||
test('explicit slots', () => { | ||
expect( | ||
compile(`<suspense> | ||
<template #default> | ||
<foo/> | ||
</template> | ||
<template #fallback> | ||
loading... | ||
</template> | ||
</suspense>`).code | ||
).toMatchInlineSnapshot(` | ||
"const { resolveComponent: _resolveComponent } = require(\\"vue\\") | ||
const { ssrRenderComponent: _ssrRenderComponent, ssrRenderSuspense: _ssrRenderSuspense } = require(\\"@vue/server-renderer\\") | ||
return function ssrRender(_ctx, _push, _parent) { | ||
const _component_foo = _resolveComponent(\\"foo\\") | ||
_push(_ssrRenderSuspense({ | ||
default: (_push) => { | ||
_push(_ssrRenderComponent(_component_foo, null, null, _parent)) | ||
}, | ||
fallback: (_push) => { | ||
_push(\` loading... \`) | ||
}, | ||
_: 1 | ||
})) | ||
}" | ||
`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/compiler-ssr/src/transforms/ssrTransformPortal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
ComponentNode, | ||
findProp, | ||
JSChildNode, | ||
NodeTypes, | ||
createSimpleExpression, | ||
createFunctionExpression, | ||
createCallExpression | ||
} from '@vue/compiler-dom' | ||
import { | ||
SSRTransformContext, | ||
processChildrenAsStatement | ||
} from '../ssrCodegenTransform' | ||
import { createSSRCompilerError, SSRErrorCodes } from '../errors' | ||
import { SSR_RENDER_PORTAL } from '../runtimeHelpers' | ||
|
||
// Note: this is a 2nd-pass codegen transform. | ||
export function ssrProcessPortal( | ||
node: ComponentNode, | ||
context: SSRTransformContext | ||
) { | ||
const targetProp = findProp(node, 'target') | ||
if (!targetProp) { | ||
context.onError( | ||
createSSRCompilerError(SSRErrorCodes.X_SSR_NO_PORTAL_TARGET, node.loc) | ||
) | ||
return | ||
} | ||
|
||
let target: JSChildNode | ||
if (targetProp.type === NodeTypes.ATTRIBUTE && targetProp.value) { | ||
target = createSimpleExpression(targetProp.value.content, true) | ||
} else if (targetProp.type === NodeTypes.DIRECTIVE && targetProp.exp) { | ||
target = targetProp.exp | ||
} else { | ||
context.onError( | ||
createSSRCompilerError( | ||
SSRErrorCodes.X_SSR_NO_PORTAL_TARGET, | ||
targetProp.loc | ||
) | ||
) | ||
return | ||
} | ||
|
||
const contentRenderFn = createFunctionExpression( | ||
[`_push`], | ||
undefined, // Body is added later | ||
true, // newline | ||
false, // isSlot | ||
node.loc | ||
) | ||
contentRenderFn.body = processChildrenAsStatement(node.children, context) | ||
context.pushStatement( | ||
createCallExpression(context.helper(SSR_RENDER_PORTAL), [ | ||
contentRenderFn, | ||
target, | ||
`_parent` | ||
]) | ||
) | ||
} |
Oops, something went wrong.