]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip(ssr): element scopeId
authorEvan You <yyx990803@gmail.com>
Thu, 6 Feb 2020 21:51:26 +0000 (16:51 -0500)
committerEvan You <yyx990803@gmail.com>
Thu, 6 Feb 2020 22:45:46 +0000 (17:45 -0500)
packages/compiler-core/src/codegen.ts
packages/compiler-core/src/options.ts
packages/compiler-core/src/transform.ts
packages/compiler-ssr/src/index.ts
packages/compiler-ssr/src/transforms/ssrTransformElement.ts

index 41f6e4c77edc5763ab2a611201e3e09745f56f5d..cc0637fc21f2b19a72a0c8403e995ca5ec43bd40 100644 (file)
@@ -44,7 +44,8 @@ import {
   CREATE_TEXT,
   PUSH_SCOPE_ID,
   POP_SCOPE_ID,
-  WITH_SCOPE_ID
+  WITH_SCOPE_ID,
+  CREATE_BLOCK
 } from './runtimeHelpers'
 import { ImportItem } from './transform'
 
@@ -333,14 +334,28 @@ function genModulePreamble(
   context: CodegenContext,
   genScopeId: boolean
 ) {
-  const { push, helper, newline, scopeId, runtimeModuleName } = context
-  // generate import statements for helpers
-  if (genScopeId) {
-    ast.helpers.push(WITH_SCOPE_ID)
-    if (ast.hoists.length) {
-      ast.helpers.push(PUSH_SCOPE_ID, POP_SCOPE_ID)
+  const { push, helper, newline, scopeId, runtimeModuleName, ssr } = context
+
+  if (!__BROWSER__) {
+    // in ssr mode, `withId` helper is only needed if the template contains
+    // de-optimized component slots (which uses the createVNode helper)
+    if (
+      ssr &&
+      !(
+        ast.helpers.includes(CREATE_VNODE) || ast.helpers.includes(CREATE_BLOCK)
+      )
+    ) {
+      genScopeId = false
+    }
+    if (genScopeId) {
+      ast.helpers.push(WITH_SCOPE_ID)
+      if (ast.hoists.length) {
+        ast.helpers.push(PUSH_SCOPE_ID, POP_SCOPE_ID)
+      }
     }
   }
+
+  // generate import statements for helpers
   if (ast.helpers.length) {
     push(
       `import { ${ast.helpers.map(helper).join(', ')} } from ${JSON.stringify(
@@ -348,21 +363,25 @@ function genModulePreamble(
       )}\n`
     )
   }
-  if (!__BROWSER__ && ast.ssrHelpers && ast.ssrHelpers.length) {
-    push(
-      `import { ${ast.ssrHelpers
-        .map(helper)
-        .join(', ')} } from "@vue/server-renderer"\n`
-    )
-  }
-  if (ast.imports.length) {
-    genImports(ast.imports, context)
-    newline()
-  }
-  if (genScopeId) {
-    push(`const withId = ${helper(WITH_SCOPE_ID)}("${scopeId}")`)
-    newline()
+
+  if (!__BROWSER__) {
+    if (ast.ssrHelpers && ast.ssrHelpers.length) {
+      push(
+        `import { ${ast.ssrHelpers
+          .map(helper)
+          .join(', ')} } from "@vue/server-renderer"\n`
+      )
+    }
+    if (ast.imports.length) {
+      genImports(ast.imports, context)
+      newline()
+    }
+    if (genScopeId) {
+      push(`const withId = ${helper(WITH_SCOPE_ID)}("${scopeId}")`)
+      newline()
+    }
   }
+
   genHoists(ast.hoists, context)
   newline()
   push(`export `)
index d195cf393fec23c7c010bc169471abd676d1c9b3..f88b007b56352c4e59c9ab52d0f08497c3027b70 100644 (file)
@@ -49,6 +49,8 @@ export interface TransformOptions {
   //   analysis to determine if a handler is safe to cache.
   // - Default: false
   cacheHandlers?: boolean
+  // SFC scoped styles ID
+  scopeId?: string | null
   ssr?: boolean
   onError?: (error: CompilerError) => void
 }
index 81b386998613fcb27572440a4325125c834aecbc..578e08d8eb3884e776e9bd78913ae4800d8c145b 100644 (file)
@@ -118,6 +118,7 @@ function createTransformContext(
     nodeTransforms = [],
     directiveTransforms = {},
     isBuiltInComponent = NOOP,
+    scopeId = null,
     ssr = false,
     onError = defaultOnError
   }: TransformOptions
@@ -130,6 +131,7 @@ function createTransformContext(
     nodeTransforms,
     directiveTransforms,
     isBuiltInComponent,
+    scopeId,
     ssr,
     onError,
 
index 2a0ba788f37e46aa165c2a5eabe534893948a58a..e12acde64ae21fe60723033abfd453b8fa4355e4 100644 (file)
@@ -31,6 +31,7 @@ export function compile(
     // apply DOM-specific parsing options
     ...parserOptions,
     ssr: true,
+    scopeId: options.mode === 'function' ? null : options.scopeId,
     // always prefix since compiler-ssr doesn't have size concern
     prefixIdentifiers: true,
     // disalbe optimizations that are unnecessary for ssr
index df409ef2cf07e8a0614b2f3516a004037500acb1..cb35c331652401652118eaf2c4f1e857b118e5a0 100644 (file)
@@ -246,6 +246,10 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
         removeStaticBinding(openTag, 'class')
       }
 
+      if (context.scopeId) {
+        openTag.push(` ${context.scopeId}`)
+      }
+
       openTag.push(`>`)
       if (rawChildren) {
         openTag.push(rawChildren)