]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: compiler-sfc should not attach ast on template with src import
authorEvan You <yyx990803@gmail.com>
Tue, 21 Nov 2023 14:09:38 +0000 (22:09 +0800)
committerEvan You <yyx990803@gmail.com>
Sat, 25 Nov 2023 08:18:29 +0000 (16:18 +0800)
packages/compiler-sfc/__tests__/compileTemplate.spec.ts
packages/compiler-sfc/__tests__/parse.spec.ts
packages/compiler-sfc/src/parse.ts

index 9b57c1230627d9703e23800821ab004e0d65160a..ca298df475e4da562785b4d0e17f34b39030c817 100644 (file)
@@ -139,7 +139,7 @@ test('should work w/ AST from descriptor', () => {
     sourceMap: true
   }).descriptor.template!
 
-  expect(template.ast.source).toBe(source)
+  expect(template.ast!.source).toBe(source)
 
   const { code, map } = compile({
     filename: 'example.vue',
index 6ae3427c74a095127bc0be4d72da8e5650ed3a5e..7c8efdfc40f3ea1379570d0d33b0237d0fada477 100644 (file)
@@ -164,6 +164,11 @@ h1 { color: red }
     expect(descriptor.script!.attrs['src']).toBe('com')
   })
 
+  test('should not expose ast on template node if has src import', () => {
+    const { descriptor } = parse(`<template src="./foo.html"/>`)
+    expect(descriptor.template!.ast).toBeUndefined()
+  })
+
   test('ignoreEmpty: false', () => {
     const { descriptor } = parse(
       `<script></script>\n<script setup>\n</script>`,
@@ -211,7 +216,7 @@ h1 { color: red }
     expect(errors.length).toBe(0)
     expect(descriptor.template!.content).toBe(content)
     // should not attempt to parse the content
-    expect(descriptor.template!.ast.children.length).toBe(1)
+    expect(descriptor.template!.ast!.children.length).toBe(1)
   })
 
   //#2566
index 3330254d24192ac0dc4e3253ef6594fb7b5fbe9f..618ffca2e667760da9640d92344046441c5cd886 100644 (file)
@@ -38,7 +38,7 @@ export interface SFCBlock {
 
 export interface SFCTemplateBlock extends SFCBlock {
   type: 'template'
-  ast: RootNode
+  ast?: RootNode
 }
 
 export interface SFCScriptBlock extends SFCBlock {
@@ -156,7 +156,10 @@ export function parse(
             source,
             false
           ) as SFCTemplateBlock)
-          templateBlock.ast = createRoot(node.children, source)
+
+          if (!templateBlock.attrs.src) {
+            templateBlock.ast = createRoot(node.children, source)
+          }
 
           // warn against 2.x <template functional>
           if (templateBlock.attrs.functional) {