]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(tests): simplify imports and reuse compile function
authordaiwei <daiwei521@126.com>
Thu, 23 Oct 2025 02:59:51 +0000 (10:59 +0800)
committeredison <daiwei521@126.com>
Thu, 23 Oct 2025 06:21:28 +0000 (14:21 +0800)
packages/runtime-vapor/__tests__/hydration.spec.ts

index fea78e9be16328611edb0bc0592e320ca76b6488..17174408eeea685ef7b2d640e2b8f9136762c2c4 100644 (file)
@@ -4,13 +4,10 @@ import {
   delegateEvents,
 } from '../src'
 import { defineAsyncComponent, nextTick, reactive, ref } from '@vue/runtime-dom'
-import { compileScript, parse } from '@vue/compiler-sfc'
-import * as runtimeVapor from '../src'
-import * as runtimeDom from '@vue/runtime-dom'
-import * as VueServerRenderer from '@vue/server-renderer'
 import { isString } from '@vue/shared'
 import type { VaporComponentInstance } from '../src/component'
 import type { TeleportFragment } from '../src/components/Teleport'
+import { VueServerRenderer, compile, runtimeDom, runtimeVapor } from './_utils'
 
 const formatHtml = (raw: string) => {
   return raw
@@ -19,48 +16,6 @@ const formatHtml = (raw: string) => {
     .replace(/\n{2,}/g, '\n')
 }
 
-const Vue = { ...runtimeDom, ...runtimeVapor }
-
-function compile(
-  sfc: string,
-  data: runtimeDom.Ref<any>,
-  components: Record<string, any> = {},
-  { vapor = true, ssr = false } = {},
-) {
-  if (!sfc.includes(`<script`)) {
-    sfc =
-      `<script vapor>const data = _data; const components = _components;</script>` +
-      sfc
-  }
-  const descriptor = parse(sfc).descriptor
-
-  const script = compileScript(descriptor, {
-    id: 'x',
-    isProd: true,
-    inlineTemplate: true,
-    genDefaultAs: '__sfc__',
-    vapor,
-    templateOptions: {
-      ssr,
-    },
-  })
-
-  const code =
-    script.content
-      .replace(/\bimport {/g, 'const {')
-      .replace(/ as _/g, ': _')
-      .replace(/} from ['"]vue['"]/g, `} = Vue`)
-      .replace(/} from "vue\/server-renderer"/g, '} = VueServerRenderer') +
-    '\nreturn __sfc__'
-
-  return new Function('Vue', 'VueServerRenderer', '_data', '_components', code)(
-    Vue,
-    VueServerRenderer,
-    data,
-    components,
-  )
-}
-
 async function testWithVaporApp(
   code: string,
   components?: Record<string, string | { code: string; vapor: boolean }>,