فهرست منبع

wip: compiler-sfc should not attach ast on template with src import

Evan You 2 سال پیش
والد
کامیت
37f9d3da8f

+ 1 - 1
packages/compiler-sfc/__tests__/compileTemplate.spec.ts

@@ -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',

+ 6 - 1
packages/compiler-sfc/__tests__/parse.spec.ts

@@ -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

+ 5 - 2
packages/compiler-sfc/src/parse.ts

@@ -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) {