Skip to content

Commit 87c86e4

Browse files
committed
refactor: ensure ssr branches are included in esm-bundler build
1 parent 4886a63 commit 87c86e4

File tree

11 files changed

+52
-26
lines changed

11 files changed

+52
-26
lines changed

jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
__ESM_BUNDLER__: true,
1212
__ESM_BROWSER__: false,
1313
__NODE_JS__: true,
14+
__SSR__: true,
1415
__FEATURE_OPTIONS_API__: true,
1516
__FEATURE_SUSPENSE__: true,
1617
__FEATURE_PROD_DEVTOOLS__: false,

packages/global.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ declare var __GLOBAL__: boolean
88
declare var __ESM_BUNDLER__: boolean
99
declare var __ESM_BROWSER__: boolean
1010
declare var __NODE_JS__: boolean
11+
declare var __SSR__: boolean
1112
declare var __COMMIT__: string
1213
declare var __VERSION__: string
1314
declare var __COMPAT__: boolean

packages/runtime-core/src/apiAsyncComponent.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export function defineAsyncComponent<
141141
// suspense-controlled or SSR.
142142
if (
143143
(__FEATURE_SUSPENSE__ && suspensible && instance.suspense) ||
144-
(__NODE_JS__ && isInSSRComponentSetup)
144+
(__SSR__ && isInSSRComponentSetup)
145145
) {
146146
return load()
147147
.then(comp => {

packages/runtime-core/src/apiWatch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ function doWatch(
280280

281281
// in SSR there is no need to setup an actual effect, and it should be noop
282282
// unless it's eager
283-
if (__NODE_JS__ && isInSSRComponentSetup) {
283+
if (__SSR__ && isInSSRComponentSetup) {
284284
// we will also not call the invalidate callback (+ runner is not set up)
285285
onInvalidate = NOOP
286286
if (!cb) {

packages/runtime-core/src/component.ts

+6-13
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ export function handleSetupResult(
678678
) {
679679
if (isFunction(setupResult)) {
680680
// setup returned an inline render function
681-
if (__NODE_JS__ && (instance.type as ComponentOptions).__ssrInlineRender) {
681+
if (__SSR__ && (instance.type as ComponentOptions).__ssrInlineRender) {
682682
// when the function's name is `ssrRender` (compiled by SFC inline mode),
683683
// set it as ssrRender instead.
684684
instance.ssrRender = setupResult
@@ -751,18 +751,11 @@ export function finishComponentSetup(
751751
}
752752

753753
// template / render function normalization
754-
if (__NODE_JS__ && isSSR) {
755-
// 1. the render function may already exist, returned by `setup`
756-
// 2. otherwise try to use the `Component.render`
757-
// 3. if the component doesn't have a render function,
758-
// set `instance.render` to NOOP so that it can inherit the render
759-
// function from mixins/extend
760-
instance.render = (instance.render ||
761-
Component.render ||
762-
NOOP) as InternalRenderFunction
763-
} else if (!instance.render) {
764-
// could be set from setup()
765-
if (compile && !Component.render) {
754+
// could be already set when returned from setup()
755+
if (!instance.render) {
756+
// only do on-the-fly compile if not in SSR - SSR on-the-fly compliation
757+
// is done by server-renderer
758+
if (!isSSR && compile && !Component.render) {
766759
const template =
767760
(__COMPAT__ &&
768761
instance.vnode.props &&

packages/runtime-core/src/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,7 @@ const _ssrUtils = {
315315
* SSR utils for \@vue/server-renderer. Only exposed in cjs builds.
316316
* @internal
317317
*/
318-
export const ssrUtils = (
319-
__NODE_JS__ || __ESM_BUNDLER__ ? _ssrUtils : null
320-
) as typeof _ssrUtils
318+
export const ssrUtils = (__SSR__ ? _ssrUtils : null) as typeof _ssrUtils
321319

322320
// 2.x COMPAT ------------------------------------------------------------------
323321

packages/runtime-dom/src/directives/vModel.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,9 @@ function callModelHook(
303303
fn && fn(el, binding, vnode, prevVNode)
304304
}
305305

306-
// SSR vnode transforms
307-
if (__NODE_JS__) {
306+
// SSR vnode transforms, only used when user includes client-oriented render
307+
// function in SSR
308+
export function initVModelForSSR() {
308309
vModelText.getSSRProps = ({ value }) => ({ value })
309310

310311
vModelRadio.getSSRProps = ({ value }, vnode) => {

packages/runtime-dom/src/directives/vShow.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ export const vShow: ObjectDirective<VShowElement> = {
4040
}
4141
}
4242

43-
if (__NODE_JS__) {
43+
function setDisplay(el: VShowElement, value: unknown): void {
44+
el.style.display = value ? el._vod : 'none'
45+
}
46+
47+
// SSR vnode transforms, only used when user includes client-oriented render
48+
// function in SSR
49+
export function initVShowForSSR() {
4450
vShow.getSSRProps = ({ value }) => {
4551
if (!value) {
4652
return { style: { display: 'none' } }
4753
}
4854
}
4955
}
50-
51-
function setDisplay(el: VShowElement, value: unknown): void {
52-
el.style.display = value ? el._vod : 'none'
53-
}

packages/runtime-dom/src/index.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ import {
1515
import { nodeOps } from './nodeOps'
1616
import { patchProp } from './patchProp'
1717
// Importing from the compiler, will be tree-shaken in prod
18-
import { isFunction, isString, isHTMLTag, isSVGTag, extend } from '@vue/shared'
18+
import {
19+
isFunction,
20+
isString,
21+
isHTMLTag,
22+
isSVGTag,
23+
extend,
24+
NOOP
25+
} from '@vue/shared'
1926

2027
declare module '@vue/reactivity' {
2128
export interface RefUnwrapBailTypes {
@@ -225,6 +232,24 @@ export {
225232
export { withModifiers, withKeys } from './directives/vOn'
226233
export { vShow } from './directives/vShow'
227234

235+
import { initVModelForSSR } from './directives/vModel'
236+
import { initVShowForSSR } from './directives/vShow'
237+
238+
let ssrDirectiveInitialized = false
239+
240+
/**
241+
* @internal
242+
*/
243+
export const initDirectivesForSSR = __SSR__
244+
? () => {
245+
if (!ssrDirectiveInitialized) {
246+
ssrDirectiveInitialized = true
247+
initVModelForSSR()
248+
initVShowForSSR()
249+
}
250+
}
251+
: NOOP
252+
228253
// re-export everything from core
229254
// h, Component, reactivity API, nextTick, flags & types
230255
export * from '@vue/runtime-core'

packages/server-renderer/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { initDirectivesForSSR } from 'vue'
2+
initDirectivesForSSR()
3+
14
// public
25
export { SSRContext } from './render'
36
export { renderToString } from './renderToString'

rollup.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ function createReplacePlugin(
242242
__ESM_BROWSER__: isBrowserESMBuild,
243243
// is targeting Node (SSR)?
244244
__NODE_JS__: isNodeBuild,
245+
// need SSR-specific branches?
246+
__SSR__: isNodeBuild || isBundlerESMBuild,
245247

246248
// for compiler-sfc browser build inlined deps
247249
...(isBrowserESMBuild

0 commit comments

Comments
 (0)