Przeglądaj źródła

feat(compiler-dom): v-cloak transform (#141)

Adam Dorling 6 lat temu
rodzic
commit
21441830dd

+ 32 - 0
packages/compiler-dom/__tests__/transforms/vCloak.spec.ts

@@ -0,0 +1,32 @@
+import {
+  parse,
+  transform,
+  CompilerOptions,
+  ElementNode
+} from '@vue/compiler-core'
+import { transformCloak } from '../../src/transforms/vCloak'
+import { transformElement } from '../../../compiler-core/src/transforms/transformElement'
+import { CallExpression } from '../../src'
+
+function transformWithCloak(template: string, options: CompilerOptions = {}) {
+  const ast = parse(template)
+  transform(ast, {
+    nodeTransforms: [transformElement],
+    directiveTransforms: {
+      cloak: transformCloak
+    },
+    ...options
+  })
+  return ast.children[0] as ElementNode
+}
+
+describe('compiler: `v-cloak` transform', () => {
+  test('should add no props to DOM', () => {
+    const node = transformWithCloak(`<div v-cloak/>`)
+    const codegenArgs = (node.codegenNode as CallExpression).arguments
+
+    // As v-cloak adds no properties the codegen should be identical to
+    // rendering a div with no props or reactive data (so just the tag as the arg)
+    expect(codegenArgs.length).toBe(1)
+  })
+})

+ 2 - 0
packages/compiler-dom/src/index.ts

@@ -2,6 +2,7 @@ import { baseCompile, CompilerOptions, CodegenResult } from '@vue/compiler-core'
 import { parserOptionsMinimal } from './parserOptionsMinimal'
 import { parserOptionsMinimal } from './parserOptionsMinimal'
 import { parserOptionsStandard } from './parserOptionsStandard'
 import { parserOptionsStandard } from './parserOptionsStandard'
 import { transformStyle } from './transforms/transformStyle'
 import { transformStyle } from './transforms/transformStyle'
+import { transformCloak } from './transforms/vCloak'
 import { transformVHtml } from './transforms/vHtml'
 import { transformVHtml } from './transforms/vHtml'
 
 
 export function compile(
 export function compile(
@@ -13,6 +14,7 @@ export function compile(
     ...(__BROWSER__ ? parserOptionsMinimal : parserOptionsStandard),
     ...(__BROWSER__ ? parserOptionsMinimal : parserOptionsStandard),
     nodeTransforms: [transformStyle, ...(options.nodeTransforms || [])],
     nodeTransforms: [transformStyle, ...(options.nodeTransforms || [])],
     directiveTransforms: {
     directiveTransforms: {
+      cloak: transformCloak,
       html: transformVHtml,
       html: transformVHtml,
       ...(options.directiveTransforms || {})
       ...(options.directiveTransforms || {})
     }
     }

+ 5 - 1
packages/compiler-dom/src/transforms/vCloak.ts

@@ -1 +1,5 @@
-// TODO
+import { DirectiveTransform } from 'packages/compiler-core/src/transform'
+
+export const transformCloak: DirectiveTransform = (node, context) => {
+  return { props: [], needRuntime: false }
+}