]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: fix compileScript script/script-setup co-usage
authorEvan You <yyx990803@gmail.com>
Thu, 19 Nov 2020 21:17:54 +0000 (16:17 -0500)
committerEvan You <yyx990803@gmail.com>
Thu, 19 Nov 2020 21:17:54 +0000 (16:17 -0500)
packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index 6ff7608ccfe1f6255a93c2c2b5548ec4479bf993..c2407f8aca15f1aae5a4d81c65e805cceff8ca1f 100644 (file)
@@ -1,5 +1,38 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
+exports[`SFC compile <script setup> <script> and <script setup> co-usage script first 1`] = `
+"import { x } from './x'
+      
+      export const n = 1
+      
+export default {
+  expose: [],
+  setup(__props) {
+
+      x()
+      
+return { x }
+}
+
+}"
+`;
+
+exports[`SFC compile <script setup> <script> and <script setup> co-usage script setup first 1`] = `
+"import { x } from './x'
+      
+export default {
+  expose: [],
+  setup(__props) {
+
+      x()
+      
+return { x }
+}
+
+}
+      export const n = 1"
+`;
+
 exports[`SFC compile <script setup> defineOptions() 1`] = `
 "export default {
   expose: [],
index 0123a9da37fc5bab67aaa0cb10e15444524f5d92..064a026d122b63df963b38d94daa43f743e35184 100644 (file)
@@ -53,6 +53,34 @@ const bar = 1
   emit: ['a', 'b'],`)
   })
 
+  describe('<script> and <script setup> co-usage', () => {
+    test('script first', () => {
+      const { content } = compile(`
+      <script>
+      export const n = 1
+      </script>
+      <script setup>
+      import { x } from './x'
+      x()
+      </script>
+      `)
+      assertCode(content)
+    })
+
+    test('script setup first', () => {
+      const { content } = compile(`
+      <script setup>
+      import { x } from './x'
+      x()
+      </script>
+      <script>
+      export const n = 1
+      </script>
+      `)
+      assertCode(content)
+    })
+  })
+
   describe('imports', () => {
     test('should hoist and expose imports', () => {
       assertCode(
index c1f0ae6ca9683bb6925d36df02e8defa9d64d20e..31e92fb02ebc2447c0cd5ef99b637c618a813b6a 100644 (file)
@@ -921,7 +921,7 @@ export function compileScript(
         hasAwait ? `async ` : ``
       }setup(${args}) {\n`
     )
-    s.append(`})`)
+    s.appendRight(endOffset, `})`)
   } else {
     if (defaultExport) {
       // can't rely on spread operator in non ts mode
@@ -939,7 +939,7 @@ export function compileScript(
         `\nexport default {${runtimeOptions}\n  ` +
           `${hasAwait ? `async ` : ``}setup(${args}) {\n`
       )
-      s.append(`}`)
+      s.appendRight(endOffset, `}`)
     }
   }