返回 slidev
global-layers.ts
根目录 / packages / slidev / node / virtual / global-layers.ts
1 import type { VirtualModuleTemplate } from './types'
2 import { join } from 'pathe'
3
4 const id = `/@slidev/global-layers`
5
6 export const templateGlobalLayers: VirtualModuleTemplate = {
7 id,
8 getContent({ roots }) {
9 const { makeAbsoluteImportGlob } = this
10 const imports = [`import { h } from 'vue'`]
11 let importIndex = 0
12
13 function* getComponent(name: string, names: string[]) {
14 yield `const ${name}Components = [\n`
15 for (const root of roots) {
16 const globs = names.map(name => join(root, `${name}.{ts,js,vue}`))
17 const importName = `__slidev_global_layer_${importIndex++}`
18 imports.push(`import ${importName} from ${JSON.stringify(makeAbsoluteImportGlob(globs, { import: 'default' }))}`)
19 yield ` Object.values(${importName})[0],\n`
20 }
21 yield `].filter(Boolean)\n`
22 yield `export const ${name} = { render: () => ${name}Components.map(comp => h(comp)) }\n\n`
23 }
24
25 const body = [
26 ...getComponent('GlobalTop', ['global', 'global-top', 'GlobalTop']),
27 ...getComponent('GlobalBottom', ['global-bottom', 'GlobalBottom']),
28 ...getComponent('SlideTop', ['slide-top', 'SlideTop']),
29 ...getComponent('SlideBottom', ['slide-bottom', 'SlideBottom']),
30 ].join('')
31
32 return `${imports.join('\n')}\n\n${body}`
33 },
34 }
35
35 lines TYPESCRIPT