-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
Copy pathscript.ts
55 lines (47 loc) · 1.58 KB
/
script.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { SFCDescriptor, SFCScriptBlock } from '@vue/compiler-sfc'
import { ResolvedOptions } from '.'
import { resolveTemplateCompilerOptions } from './template'
import { compiler } from './compiler'
// ssr and non ssr builds would output different script content
const clientCache = new WeakMap<SFCDescriptor, SFCScriptBlock | null>()
const ssrCache = new WeakMap<SFCDescriptor, SFCScriptBlock | null>()
export function getResolvedScript(
descriptor: SFCDescriptor,
ssr: boolean
): SFCScriptBlock | null | undefined {
return (ssr ? ssrCache : clientCache).get(descriptor)
}
export function setResolvedScript(
descriptor: SFCDescriptor,
script: SFCScriptBlock,
ssr: boolean
): void {
;(ssr ? ssrCache : clientCache).set(descriptor, script)
}
export function resolveScript(
descriptor: SFCDescriptor,
options: ResolvedOptions,
ssr: boolean
): SFCScriptBlock | null {
if (!descriptor.script && !descriptor.scriptSetup) {
return null
}
const cacheToUse = ssr ? ssrCache : clientCache
const cached = cacheToUse.get(descriptor)
if (cached) {
return cached
}
let resolved: SFCScriptBlock | null = null
resolved = compiler.compileScript(descriptor, {
...options.script,
id: descriptor.id,
isProd: options.isProduction,
inlineTemplate: !options.devServer,
refTransform: options.refTransform !== false,
templateOptions: resolveTemplateCompilerOptions(descriptor, options, ssr),
// @ts-ignore TODO remove ignore when we support this in @vue/compiler-sfc
sourceMap: options.sourceMap
})
cacheToUse.set(descriptor, resolved)
return resolved
}