diff --git a/src/index.ts b/src/index.ts index 414ca70..f89a059 100644 --- a/src/index.ts +++ b/src/index.ts @@ -114,7 +114,7 @@ export default function PluginVue(userOptions: Partial = {}): 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' diff --git a/src/script.ts b/src/script.ts index f74b00b..4e9c9b6 100644 --- a/src/script.ts +++ b/src/script.ts @@ -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() -const normalCache = new WeakMap() +// ssr and non ssr builds would output different script content +const clientCache = new WeakMap() +const serverCache = new WeakMap() 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( @@ -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 @@ -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)) diff --git a/src/sfc.ts b/src/sfc.ts index 129dd5e..b6ed291 100644 --- a/src/sfc.ts +++ b/src/sfc.ts @@ -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) diff --git a/src/template.ts b/src/template.ts index 75d0b4a..b23a8f9 100644 --- a/src/template.ts +++ b/src/template.ts @@ -1,5 +1,4 @@ import { - generateCssVars, compileTemplate, SFCDescriptor, SFCTemplateCompileOptions, @@ -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, }) @@ -62,17 +62,20 @@ 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, @@ -80,13 +83,11 @@ export function getTemplateCompilerOptions( 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, }