|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import type { SFCDescriptor } from 'vue/compiler-sfc' |
| 3 | +import type { ResolvedOptions } from '../src/index' |
| 4 | +import { resolveCompiler } from '../src/compiler' |
| 5 | +import { transformMain } from '../src/main' |
| 6 | +import { transformTemplateAsModule } from '../src/template' |
| 7 | +import { createDescriptor } from '../src/utils/descriptorCache' |
| 8 | + |
| 9 | +const compiler = resolveCompiler(process.cwd()) |
| 10 | + |
| 11 | +function createOptions(): ResolvedOptions { |
| 12 | + return { |
| 13 | + root: '/root', |
| 14 | + isProduction: false, |
| 15 | + sourceMap: false, |
| 16 | + cssDevSourcemap: false, |
| 17 | + compiler, |
| 18 | + } as ResolvedOptions |
| 19 | +} |
| 20 | + |
| 21 | +function parseDescriptor( |
| 22 | + filename: string, |
| 23 | + source: string, |
| 24 | + options: ResolvedOptions, |
| 25 | +): SFCDescriptor { |
| 26 | + const { descriptor, errors } = createDescriptor(filename, source, options) |
| 27 | + if (errors.length) { |
| 28 | + throw errors[0] |
| 29 | + } |
| 30 | + return descriptor |
| 31 | +} |
| 32 | + |
| 33 | +function createPluginContext() { |
| 34 | + return { |
| 35 | + warn: vi.fn(), |
| 36 | + error: vi.fn((error: unknown) => { |
| 37 | + throw error |
| 38 | + }), |
| 39 | + } as any |
| 40 | +} |
| 41 | + |
| 42 | +// TODO: remove todo in v3.6 |
| 43 | +describe.todo('template-only vapor __multiRoot', () => { |
| 44 | + it('attaches __multiRoot for inline multi-root templates', async () => { |
| 45 | + const filename = '/root/Inline.vue' |
| 46 | + const source = '<template vapor><div /><div /></template>' |
| 47 | + const options = createOptions() |
| 48 | + |
| 49 | + const result = await transformMain( |
| 50 | + source, |
| 51 | + filename, |
| 52 | + options, |
| 53 | + createPluginContext(), |
| 54 | + false, |
| 55 | + false, |
| 56 | + ) |
| 57 | + |
| 58 | + expect(result?.code).toContain('const _sfc_main = { __vapor: true }') |
| 59 | + expect(result?.code).toContain('_sfc_main.__multiRoot = true') |
| 60 | + }) |
| 61 | + |
| 62 | + it('preserves false multiRoot values for inline single-root templates', async () => { |
| 63 | + const filename = '/root/InlineSingle.vue' |
| 64 | + const source = '<template vapor><div /></template>' |
| 65 | + const options = createOptions() |
| 66 | + |
| 67 | + const result = await transformMain( |
| 68 | + source, |
| 69 | + filename, |
| 70 | + options, |
| 71 | + createPluginContext(), |
| 72 | + false, |
| 73 | + false, |
| 74 | + ) |
| 75 | + |
| 76 | + expect(result?.code).toContain('_sfc_main.__multiRoot = false') |
| 77 | + }) |
| 78 | + |
| 79 | + it('re-exports and imports multiRoot for external template modules', async () => { |
| 80 | + const filename = '/root/External.vue' |
| 81 | + const source = '<template vapor lang="pug">div\ndiv</template>' |
| 82 | + const options = createOptions() |
| 83 | + const descriptor = parseDescriptor(filename, source, options) |
| 84 | + |
| 85 | + const mainResult = await transformMain( |
| 86 | + source, |
| 87 | + filename, |
| 88 | + options, |
| 89 | + createPluginContext(), |
| 90 | + false, |
| 91 | + false, |
| 92 | + ) |
| 93 | + const templateResult = await transformTemplateAsModule( |
| 94 | + descriptor.template!.content, |
| 95 | + filename, |
| 96 | + descriptor, |
| 97 | + options, |
| 98 | + createPluginContext(), |
| 99 | + false, |
| 100 | + false, |
| 101 | + ) |
| 102 | + |
| 103 | + expect(mainResult?.code).toContain( |
| 104 | + 'import { render as _sfc_render, multiRoot as _sfc_multiRoot }', |
| 105 | + ) |
| 106 | + expect(mainResult?.code).toContain('_sfc_main.__multiRoot = _sfc_multiRoot') |
| 107 | + expect(templateResult.code).toContain('export const multiRoot = true') |
| 108 | + }) |
| 109 | + |
| 110 | + it('does not attach __multiRoot when the component has script', async () => { |
| 111 | + const filename = '/root/WithScript.vue' |
| 112 | + const source = |
| 113 | + '<script>export default {}</script><template vapor lang="pug">div</template>' |
| 114 | + const options = createOptions() |
| 115 | + |
| 116 | + const result = await transformMain( |
| 117 | + source, |
| 118 | + filename, |
| 119 | + options, |
| 120 | + createPluginContext(), |
| 121 | + false, |
| 122 | + false, |
| 123 | + ) |
| 124 | + |
| 125 | + expect(result?.code).not.toContain('__multiRoot') |
| 126 | + expect(result?.code).not.toContain('multiRoot as _sfc_multiRoot') |
| 127 | + }) |
| 128 | +}) |
0 commit comments