Skip to content

Commit

Permalink
wip: adjust inline cache
Browse files Browse the repository at this point in the history
  • Loading branch information
grudev32 committed Nov 20, 2020
1 parent a01ea43 commit 2737a65
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
query.type === 'template'
? descriptor.template!
: query.type === 'script'
? getResolvedScript(descriptor, !isServer)
? getResolvedScript(descriptor, isServer)
: query.type === 'style'
? descriptor.styles[query.index]
: typeof query.index === 'number'
Expand Down
27 changes: 13 additions & 14 deletions src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import { Options } from '.'
import { getTemplateCompilerOptions } from './template'
import { createRollupError } from './utils/error'

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

export function getResolvedScript(
descriptor: SFCDescriptor,
enableInline: boolean
isServer: boolean
): SFCScriptBlock | null | undefined {
const cacheToUse = enableInline ? inlinedCache : normalCache
return cacheToUse.get(descriptor)
return (isServer ? serverCache : clientCache).get(descriptor)
}

export function resolveScript(
Expand All @@ -29,8 +27,7 @@ export function resolveScript(
return null
}

const enableInline = !isServer
const cacheToUse = enableInline ? inlinedCache : normalCache
const cacheToUse = isServer ? serverCache : clientCache
const cached = cacheToUse.get(descriptor)
if (cached) {
return cached
Expand All @@ -41,12 +38,14 @@ export function resolveScript(
if (compileScript) {
try {
resolved = compileScript(descriptor, {
scopeId,
id: scopeId,
isProd,
inlineTemplate: enableInline,
templateOptions: enableInline
? getTemplateCompilerOptions(options, descriptor, scopeId)
: undefined,
inlineTemplate: true,
templateOptions: getTemplateCompilerOptions(
options,
descriptor,
scopeId
),
})
} catch (e) {
pluginContext.error(createRollupError(descriptor.filename, e))
Expand Down
5 changes: 3 additions & 2 deletions src/sfc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ export function transformSFCEntry(
// feature information
const hasScoped = descriptor.styles.some((s) => s.scoped)

const useInlineTemplate = descriptor.scriptSetup && !isServer
const hasTemplateImport = descriptor.template && !useInlineTemplate
const isTemplateInlined =
descriptor.scriptSetup && !(descriptor.template && descriptor.template.src)
const hasTemplateImport = descriptor.template && !isTemplateInlined

const templateImport = hasTemplateImport
? genTemplateCode(descriptor, scopeId, isServer)
Expand Down
15 changes: 8 additions & 7 deletions src/template.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
generateCssVars,
compileTemplate,
SFCDescriptor,
SFCTemplateCompileOptions,
Expand All @@ -22,6 +21,7 @@ export function transformTemplate(
const descriptor = getDescriptor(query.filename)
const result = compileTemplate({
...getTemplateCompilerOptions(options, descriptor, query.id),
id: query.id,
source: code,
filename: query.filename,
})
Expand Down Expand Up @@ -62,31 +62,32 @@ export function getTemplateCompilerOptions(
return
}

const isServer = options.target === 'node'
const isProduction =
const isProd =
process.env.NODE_ENV === 'production' || process.env.BUILD === 'production'
const isServer = options.target === 'node'
const hasScoped = descriptor.styles.some((s) => s.scoped)
const preprocessLang = block.lang
const preprocessOptions =
preprocessLang &&
options.templatePreprocessOptions &&
options.templatePreprocessOptions[preprocessLang]
const resolvedScript = getResolvedScript(descriptor, !isServer)
const resolvedScript = getResolvedScript(descriptor, isServer)
return {
id: scopeId,
scoped: hasScoped,
isProd,
filename: descriptor.filename,
inMap: block.src ? undefined : block.map,
preprocessLang,
preprocessOptions,
preprocessCustomRequire: options.preprocessCustomRequire,
compiler: options.compiler,
ssr: isServer,
ssrCssVars: descriptor.cssVars,
compilerOptions: {
...options.compilerOptions,
scopeId: hasScoped ? `data-v-${scopeId}` : undefined,
bindingMetadata: resolvedScript ? resolvedScript.bindings : undefined,
ssrCssVars: isServer
? generateCssVars(descriptor, scopeId, isProduction)
: undefined,
},
transformAssetUrls: options.transformAssetUrls,
}
Expand Down

0 comments on commit 2737a65

Please sign in to comment.