返回 slidev
contextInjection.ts
根目录 / packages / slidev / node / vite / contextInjection.ts
1 import type { Plugin } from 'vite'
2 import { templateImportContextUtils, templateInitContext, templateInjectionMarker } from './common'
3
4 const RE_EXPORT_DEFAULT_OBJECT = /export\s+default\s+\{/
5 const RE_INJECT_OPTION = /.*inject\s*:\s*([[{])/
6 const RE_VUE_FILE = /\.vue$/
7 const RE_CLIENT_FILE = /\/(?:@slidev\/client|packages\/client)\//
8
9 /**
10 * Inject `$slidev` into the script block of a Vue component
11 */
12 export function createContextInjectionPlugin(): Plugin {
13 return {
14 name: 'slidev:context-injection',
15 transform: {
16 filter: {
17 id: { include: RE_VUE_FILE, exclude: RE_CLIENT_FILE },
18 },
19 async handler(code, id) {
20 if (!RE_VUE_FILE.test(id) || RE_CLIENT_FILE.test(id))
21 return
22 if (code.includes(templateInjectionMarker) || code.includes('useSlideContext()'))
23 return code // Assume that the context is already imported and used
24 const imports = [
25 templateImportContextUtils,
26 templateInitContext,
27 templateInjectionMarker,
28 ]
29
30 // Find all <script> blocks
31 const matchScripts = [
32 ...code.matchAll(/<script([^>]*)>/g),
33 ]
34 // Find the <script ... setup> block
35 const setupScriptMatch = [
36 ...code.matchAll(/<script([^>]*)setup([^>]*)>/g),
37 ].at(0)
38 if (setupScriptMatch) {
39 // Only inject into the <script setup> block
40 const setupTag = setupScriptMatch[0]
41 const setupTagIndex = setupScriptMatch.index || 0
42 const setupTagEnd = setupTagIndex + setupTag.length
43 // Insert imports right after the <script setup ...> tag
44 return `${code.slice(0, setupTagEnd)}\n${imports.join('\n')}\n${code.slice(setupTagEnd)}`
45 }
46 else if (!setupScriptMatch && matchScripts.length === 1) {
47 // not a setup script
48 const matchExport = code.match(RE_EXPORT_DEFAULT_OBJECT)
49 if (matchExport) {
50 // script exports a component
51 const exportIndex = (matchExport.index || 0) + matchExport[0].length
52 let component = code.slice(exportIndex)
53 component = component.slice(0, component.indexOf('</script>'))
54
55 const scriptIndex = (matchScripts[0].index || 0) + matchScripts[0][0].length
56 const provideImport = '\nimport { injectionSlidevContext } from "@slidev/client/constants.ts"\n'
57 code = `${code.slice(0, scriptIndex)}${provideImport}${code.slice(scriptIndex)}`
58
59 let injectIndex = exportIndex + provideImport.length
60 let injectObject = '$slidev: { from: injectionSlidevContext },'
61 const matchInject = component.match(RE_INJECT_OPTION)
62 if (matchInject) {
63 // component has a inject option
64 injectIndex += (matchInject.index || 0) + matchInject[0].length
65 if (matchInject[1] === '[') {
66 // inject option in array
67 let injects = component.slice((matchInject.index || 0) + matchInject[0].length)
68 const injectEndIndex = injects.indexOf(']')
69 injects = injects.slice(0, injectEndIndex)
70 injectObject += injects.split(',').map(inject => `${inject}: {from: ${inject}}`).join(',')
71 return `${code.slice(0, injectIndex - 1)}{\n${injectObject}\n}${code.slice(injectIndex + injectEndIndex + 1)}`
72 }
73 else {
74 // inject option in object
75 return `${code.slice(0, injectIndex)}\n${injectObject}\n${code.slice(injectIndex)}`
76 }
77 }
78 // add inject option
79 return `${code.slice(0, injectIndex)}\ninject: { ${injectObject} },\n${code.slice(injectIndex)}`
80 }
81 }
82 // no setup script and not a vue component
83 return `<script setup>\n${imports.join('\n')}\n</script>\n${code}`
84 },
85 },
86 }
87 }
88
88 lines TYPESCRIPT