Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit df98a00

Browse files
committed
wip: adjust inline cache
1 parent 42aec88 commit df98a00

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

Diff for: src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
114114
query.type === 'template'
115115
? descriptor.template!
116116
: query.type === 'script'
117-
? getResolvedScript(descriptor, !isServer)
117+
? getResolvedScript(descriptor, isServer)
118118
: query.type === 'style'
119119
? descriptor.styles[query.index]
120120
: typeof query.index === 'number'

Diff for: src/script.ts

+13-14
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ import { Options } from '.'
44
import { getTemplateCompilerOptions } from './template'
55
import { createRollupError } from './utils/error'
66

7-
// since we generate different output based on whether the template is inlined
8-
// or not, we need to cache the results separately
9-
const inlinedCache = new WeakMap<SFCDescriptor, SFCScriptBlock | null>()
10-
const normalCache = new WeakMap<SFCDescriptor, SFCScriptBlock | null>()
7+
// ssr and non ssr builds would output different script content
8+
const clientCache = new WeakMap<SFCDescriptor, SFCScriptBlock | null>()
9+
const serverCache = new WeakMap<SFCDescriptor, SFCScriptBlock | null>()
1110

1211
export function getResolvedScript(
1312
descriptor: SFCDescriptor,
14-
enableInline: boolean
13+
isServer: boolean
1514
): SFCScriptBlock | null | undefined {
16-
const cacheToUse = enableInline ? inlinedCache : normalCache
17-
return cacheToUse.get(descriptor)
15+
return (isServer ? serverCache : clientCache).get(descriptor)
1816
}
1917

2018
export function resolveScript(
@@ -29,8 +27,7 @@ export function resolveScript(
2927
return null
3028
}
3129

32-
const enableInline = !isServer
33-
const cacheToUse = enableInline ? inlinedCache : normalCache
30+
const cacheToUse = isServer ? serverCache : clientCache
3431
const cached = cacheToUse.get(descriptor)
3532
if (cached) {
3633
return cached
@@ -41,12 +38,14 @@ export function resolveScript(
4138
if (compileScript) {
4239
try {
4340
resolved = compileScript(descriptor, {
44-
scopeId,
41+
id: scopeId,
4542
isProd,
46-
inlineTemplate: enableInline,
47-
templateOptions: enableInline
48-
? getTemplateCompilerOptions(options, descriptor, scopeId)
49-
: undefined,
43+
inlineTemplate: true,
44+
templateOptions: getTemplateCompilerOptions(
45+
options,
46+
descriptor,
47+
scopeId
48+
),
5049
})
5150
} catch (e) {
5251
pluginContext.error(createRollupError(descriptor.filename, e))

Diff for: src/sfc.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ export function transformSFCEntry(
4242
// feature information
4343
const hasScoped = descriptor.styles.some((s) => s.scoped)
4444

45-
const useInlineTemplate = descriptor.scriptSetup && !isServer
46-
const hasTemplateImport = descriptor.template && !useInlineTemplate
45+
const isTemplateInlined =
46+
descriptor.scriptSetup && !(descriptor.template && descriptor.template.src)
47+
const hasTemplateImport = descriptor.template && !isTemplateInlined
4748

4849
const templateImport = hasTemplateImport
4950
? genTemplateCode(descriptor, scopeId, isServer)

Diff for: src/template.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
generateCssVars,
32
compileTemplate,
43
SFCDescriptor,
54
SFCTemplateCompileOptions,
@@ -22,6 +21,7 @@ export function transformTemplate(
2221
const descriptor = getDescriptor(query.filename)
2322
const result = compileTemplate({
2423
...getTemplateCompilerOptions(options, descriptor, query.id),
24+
id: query.id,
2525
source: code,
2626
filename: query.filename,
2727
})
@@ -62,31 +62,32 @@ export function getTemplateCompilerOptions(
6262
return
6363
}
6464

65-
const isServer = options.target === 'node'
66-
const isProduction =
65+
const isProd =
6766
process.env.NODE_ENV === 'production' || process.env.BUILD === 'production'
67+
const isServer = options.target === 'node'
6868
const hasScoped = descriptor.styles.some((s) => s.scoped)
6969
const preprocessLang = block.lang
7070
const preprocessOptions =
7171
preprocessLang &&
7272
options.templatePreprocessOptions &&
7373
options.templatePreprocessOptions[preprocessLang]
74-
const resolvedScript = getResolvedScript(descriptor, !isServer)
74+
const resolvedScript = getResolvedScript(descriptor, isServer)
7575
return {
76+
id: scopeId,
77+
scoped: hasScoped,
78+
isProd,
7679
filename: descriptor.filename,
7780
inMap: block.src ? undefined : block.map,
7881
preprocessLang,
7982
preprocessOptions,
8083
preprocessCustomRequire: options.preprocessCustomRequire,
8184
compiler: options.compiler,
8285
ssr: isServer,
86+
ssrCssVars: descriptor.cssVars,
8387
compilerOptions: {
8488
...options.compilerOptions,
8589
scopeId: hasScoped ? `data-v-${scopeId}` : undefined,
8690
bindingMetadata: resolvedScript ? resolvedScript.bindings : undefined,
87-
ssrCssVars: isServer
88-
? generateCssVars(descriptor, scopeId, isProduction)
89-
: undefined,
9091
},
9192
transformAssetUrls: options.transformAssetUrls,
9293
}

0 commit comments

Comments
 (0)