]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(ssr): avoid hard-coded ssr checks in cjs builds
authorEvan You <yyx990803@gmail.com>
Wed, 29 Jan 2020 14:49:17 +0000 (09:49 -0500)
committerEvan You <yyx990803@gmail.com>
Wed, 29 Jan 2020 14:49:17 +0000 (09:49 -0500)
jest.config.js
packages/global.d.ts
packages/runtime-core/src/apiLifecycle.ts
packages/runtime-core/src/apiWatch.ts
packages/runtime-core/src/component.ts
packages/runtime-core/src/index.ts
packages/server-renderer/src/renderToString.ts
rollup.config.js

index 48ded2ca42d2432a7934b14ed728f92dc4e83f01..46f235dc41d85f6f748441cc48d30f44f3dba16c 100644 (file)
@@ -7,7 +7,7 @@ module.exports = {
     __BROWSER__: false,
     __BUNDLER__: true,
     __RUNTIME_COMPILE__: true,
-    __SSR__: false,
+    __NODE_JS__: true,
     __FEATURE_OPTIONS__: true,
     __FEATURE_SUSPENSE__: true
   },
index 5af13958e441c91efeb197d85a789da0c3cc1060..7d1805539c86ba5598dc9bdb7afa9c26a670ac3b 100644 (file)
@@ -4,7 +4,7 @@ declare var __TEST__: boolean
 declare var __BROWSER__: boolean
 declare var __BUNDLER__: boolean
 declare var __RUNTIME_COMPILE__: boolean
-declare var __SSR__: boolean
+declare var __NODE_JS__: boolean
 declare var __COMMIT__: string
 declare var __VERSION__: string
 
index 0b0c25e2f9b19f6c5fc8c151dd827f4db995cd0e..8733ecd2bde039e9a8db2a32326247c04c7aa593 100644 (file)
@@ -2,7 +2,8 @@ import {
   ComponentInternalInstance,
   LifecycleHooks,
   currentInstance,
-  setCurrentInstance
+  setCurrentInstance,
+  isInSSRComponentSetup
 } from './component'
 import { ComponentPublicInstance } from './componentProxy'
 import { callWithAsyncErrorHandling, ErrorTypeStrings } from './errorHandling'
@@ -66,7 +67,7 @@ export const createHook = <T extends Function = () => any>(
   lifecycle: LifecycleHooks
 ) => (hook: T, target: ComponentInternalInstance | null = currentInstance) =>
   // post-create lifecycle registrations are noops during SSR
-  !__SSR__ && injectHook(lifecycle, hook, target)
+  !isInSSRComponentSetup && injectHook(lifecycle, hook, target)
 
 export const onBeforeMount = createHook(LifecycleHooks.BEFORE_MOUNT)
 export const onMounted = createHook(LifecycleHooks.MOUNTED)
index 000f48edb1c15c7ffb46bfbcd89b6b8b7bebbd2d..74acbfd59f24eb1d5fc640a5d1811d2ce0c3c8d4 100644 (file)
@@ -21,7 +21,8 @@ import {
   currentInstance,
   ComponentInternalInstance,
   currentSuspense,
-  Data
+  Data,
+  isInSSRComponentSetup
 } from './component'
 import {
   ErrorCodes,
@@ -86,8 +87,8 @@ export function watch<T = any>(
   cbOrOptions?: WatchCallback<T> | WatchOptions,
   options?: WatchOptions
 ): StopHandle {
-  if (__SSR__ && !(options && options.flush === 'sync')) {
-    // during SSR, non-sync watchers never fire.
+  if (isInSSRComponentSetup && !(options && options.flush === 'sync')) {
+    // component watchers during SSR are no-op
     return NOOP
   } else if (isFunction(cbOrOptions)) {
     // effect callback as 2nd argument - this is a source watcher
index 70971260f5257cc576a9ad10eba6a6d15ad172c4..889c45d39be61cd0d668aaf5659aeb6883010341 100644 (file)
@@ -269,19 +269,26 @@ export function validateComponentName(name: string, config: AppConfig) {
   }
 }
 
+export let isInSSRComponentSetup = false
+
 export function setupComponent(
   instance: ComponentInternalInstance,
-  parentSuspense: SuspenseBoundary | null
+  parentSuspense: SuspenseBoundary | null,
+  isSSR = false
 ) {
+  isInSSRComponentSetup = isSSR
   const propsOptions = instance.type.props
   const { props, children, shapeFlag } = instance.vnode
   resolveProps(instance, props, propsOptions)
   resolveSlots(instance, children)
 
   // setup stateful logic
+  let setupResult
   if (shapeFlag & ShapeFlags.STATEFUL_COMPONENT) {
-    return setupStatefulComponent(instance, parentSuspense)
+    setupResult = setupStatefulComponent(instance, parentSuspense)
   }
+  isInSSRComponentSetup = false
+  return setupResult
 }
 
 function setupStatefulComponent(
@@ -314,7 +321,7 @@ function setupStatefulComponent(
   // 2. create props proxy
   // the propsProxy is a reactive AND readonly proxy to the actual props.
   // it will be updated in resolveProps() on updates before render
-  const propsProxy = (instance.propsProxy = __SSR__
+  const propsProxy = (instance.propsProxy = isInSSRComponentSetup
     ? instance.props
     : shallowReadonly(instance.props))
   // 3. call setup()
@@ -335,7 +342,7 @@ function setupStatefulComponent(
     currentSuspense = null
 
     if (isPromise(setupResult)) {
-      if (__SSR__) {
+      if (isInSSRComponentSetup) {
         // return the promise so server-renderer can wait on it
         return setupResult.then(resolvedResult => {
           handleSetupResult(instance, resolvedResult, parentSuspense)
@@ -413,7 +420,7 @@ function finishComponentSetup(
       ;(Component.render as RenderFunction).isRuntimeCompiled = true
     }
 
-    if (__DEV__ && !Component.render) {
+    if (__DEV__ && !Component.render && !Component.ssrRender) {
       /* istanbul ignore if */
       if (!__RUNTIME_COMPILE__ && Component.template) {
         warn(
@@ -421,7 +428,7 @@ function finishComponentSetup(
             `does not support runtime template compilation. Either use the ` +
             `full build or pre-compile the template using Vue CLI.`
         )
-      } else if (!__SSR__ || !Component.ssrRender) {
+      } else {
         warn(
           `Component is missing${
             __RUNTIME_COMPILE__ ? ` template or` : ``
index 9d82189359d779a14f9398ce17d060ba8c0d9d6b..12267adb0a3c1f7474a3c3f400c6fb2ad65982ab 100644 (file)
@@ -106,7 +106,7 @@ import { createComponentInstance, setupComponent } from './component'
 import { renderComponentRoot } from './componentRenderUtils'
 import { normalizeVNode } from './vnode'
 
-// SSR utils are only exposed in SSR builds.
+// SSR utils are only exposed in cjs builds.
 const _ssrUtils = {
   createComponentInstance,
   setupComponent,
@@ -114,7 +114,7 @@ const _ssrUtils = {
   normalizeVNode
 }
 
-export const ssrUtils = (__SSR__ ? _ssrUtils : null) as typeof _ssrUtils
+export const ssrUtils = (__NODE_JS__ ? _ssrUtils : null) as typeof _ssrUtils
 
 // Types -----------------------------------------------------------------------
 
index ff3e2749cc1339fb73306e56639597f988769da1..291593aa50293c5780160f031e6ecaddf062cf6a 100644 (file)
@@ -19,8 +19,7 @@ import {
   isPromise,
   isArray,
   isFunction,
-  isVoidTag,
-  EMPTY_OBJ
+  isVoidTag
 } from '@vue/shared'
 import { renderProps } from './renderProps'
 import { escape } from './escape'
@@ -104,7 +103,11 @@ function renderComponentVNode(
   parentComponent: ComponentInternalInstance | null = null
 ): ResolvedSSRBuffer | Promise<ResolvedSSRBuffer> {
   const instance = createComponentInstance(vnode, parentComponent)
-  const res = setupComponent(instance, null)
+  const res = setupComponent(
+    instance,
+    null /* parentSuspense */,
+    true /* isSSR */
+  )
   if (isPromise(res)) {
     return res.then(() => renderComponentSubTree(instance))
   } else {
index bad7672a8232f0137edaf38160b7092fc5dfc94d..5d528bf904b08b1b4375eef9a6a97893fca8b45f 100644 (file)
@@ -173,7 +173,7 @@ function createReplacePlugin(
     // support compile in browser?
     __RUNTIME_COMPILE__: isRuntimeCompileBuild,
     // is targeting Node (SSR)?
-    __SSR__: isNodeBuild,
+    __NODE_JS__: isNodeBuild,
     // support options?
     // the lean build drops options related code with buildOptions.lean: true
     __FEATURE_OPTIONS__: !packageOptions.lean && !process.env.LEAN,