11import { compileScript , SFCDescriptor , SFCScriptBlock } from '@vue/compiler-sfc'
2+ import { TransformPluginContext } from 'rollup'
23import { Options } from '.'
34import { getTemplateCompilerOptions } from './template'
5+ import { createRollupError } from './utils/error'
46
57// since we generate different output based on whether the template is inlined
68// or not, we need to cache the results separately
@@ -9,9 +11,9 @@ const normalCache = new WeakMap<SFCDescriptor, SFCScriptBlock | null>()
911
1012export function getResolvedScript (
1113 descriptor : SFCDescriptor ,
12- isServer : boolean
14+ enableInline : boolean
1315) : SFCScriptBlock | null | undefined {
14- const cacheToUse = isServer ? normalCache : inlinedCache
16+ const cacheToUse = enableInline ? inlinedCache : normalCache
1517 return cacheToUse . get ( descriptor )
1618}
1719
@@ -20,40 +22,44 @@ export function resolveScript(
2022 scopeId : string ,
2123 isProd : boolean ,
2224 isServer : boolean ,
23- options : Options
25+ options : Options ,
26+ pluginContext : TransformPluginContext
2427) {
2528 if ( ! descriptor . script && ! descriptor . scriptSetup ) {
2629 return null
2730 }
2831
29- const cached = getResolvedScript ( descriptor , isServer )
32+ const enableInline = ! isServer
33+ const cacheToUse = enableInline ? inlinedCache : normalCache
34+ const cached = cacheToUse . get ( descriptor )
3035 if ( cached ) {
3136 return cached
3237 }
3338
34- let resolved : SFCScriptBlock | null
39+ let resolved : SFCScriptBlock | null = null
3540
3641 if ( compileScript ) {
37- resolved = compileScript ( descriptor , {
38- id : scopeId ,
39- isProd,
40- inlineTemplate : ! isServer ,
41- templateOptions : getTemplateCompilerOptions ( options , descriptor , scopeId ) ,
42- } )
42+ try {
43+ resolved = compileScript ( descriptor , {
44+ id : scopeId ,
45+ isProd,
46+ inlineTemplate : enableInline ,
47+ templateOptions : enableInline
48+ ? getTemplateCompilerOptions ( options , descriptor , scopeId )
49+ : undefined ,
50+ } )
51+ } catch ( e ) {
52+ pluginContext . error ( createRollupError ( descriptor . filename , e ) )
53+ }
4354 } else if ( descriptor . scriptSetup ) {
44- throw new Error (
55+ pluginContext . error (
4556 `<script setup> is not supported by the installed version of ` +
4657 `@vue/compiler-sfc - please upgrade.`
4758 )
4859 } else {
4960 resolved = descriptor . script
5061 }
5162
52- if ( isServer ) {
53- normalCache . set ( descriptor , resolved )
54- } else {
55- inlinedCache . set ( descriptor , resolved )
56- }
57-
63+ cacheToUse . set ( descriptor , resolved )
5864 return resolved
5965}
0 commit comments