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

Commit 298fc90

Browse files
committed
refactor: improve logic
1 parent facf4c6 commit 298fc90

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
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'

src/script.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { compileScript, SFCDescriptor, SFCScriptBlock } from '@vue/compiler-sfc'
2+
import { TransformPluginContext } from 'rollup'
23
import { Options } from '.'
34
import { 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

1012
export 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
}

src/sfc.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ export function transformSFCEntry(
4242
// feature information
4343
const hasScoped = descriptor.styles.some((s) => s.scoped)
4444

45-
const hasTemplateImport =
46-
descriptor.template &&
47-
// script setup compiles template inline, do not import again
48-
(isServer || !descriptor.scriptSetup)
45+
const useInlineTemplate = descriptor.scriptSetup && !isServer
46+
const hasTemplateImport = descriptor.template && !useInlineTemplate
4947

5048
const templateImport = hasTemplateImport
5149
? genTemplateCode(descriptor, scopeId, isServer)
@@ -62,7 +60,8 @@ export function transformSFCEntry(
6260
scopeId,
6361
isProduction,
6462
isServer,
65-
options
63+
options,
64+
pluginContext
6665
)
6766
const stylesCode = genStyleCode(descriptor, scopeId, options.preprocessStyles)
6867
const customBlocksCode = getCustomBlock(descriptor, filterCustomBlock)
@@ -118,10 +117,18 @@ function genScriptCode(
118117
scopeId: string,
119118
isProd: boolean,
120119
isServer: boolean,
121-
options: Options
120+
options: Options,
121+
pluginContext: TransformPluginContext
122122
) {
123123
let scriptImport = `const script = {}`
124-
const script = resolveScript(descriptor, scopeId, isProd, isServer, options)
124+
const script = resolveScript(
125+
descriptor,
126+
scopeId,
127+
isProd,
128+
isServer,
129+
options,
130+
pluginContext
131+
)
125132
if (script) {
126133
const src = script.src || descriptor.filename
127134
const attrsQuery = attrsToQuery(script.attrs, 'js')

src/template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export function getTemplateCompilerOptions(
7171
preprocessLang &&
7272
options.templatePreprocessOptions &&
7373
options.templatePreprocessOptions[preprocessLang]
74-
const resolvedScript = getResolvedScript(descriptor, isServer)
74+
const resolvedScript = getResolvedScript(descriptor, !isServer)
7575
return {
7676
filename: descriptor.filename,
7777
inMap: block.src ? undefined : block.map,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy