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

Commit

Permalink
refactor: improve logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Nov 20, 2020
1 parent facf4c6 commit 298fc90
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 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
42 changes: 24 additions & 18 deletions src/script.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { compileScript, SFCDescriptor, SFCScriptBlock } from '@vue/compiler-sfc'
import { TransformPluginContext } from 'rollup'
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
Expand All @@ -9,9 +11,9 @@ const normalCache = new WeakMap<SFCDescriptor, SFCScriptBlock | null>()

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

Expand All @@ -20,40 +22,44 @@ export function resolveScript(
scopeId: string,
isProd: boolean,
isServer: boolean,
options: Options
options: Options,
pluginContext: TransformPluginContext
) {
if (!descriptor.script && !descriptor.scriptSetup) {
return null
}

const cached = getResolvedScript(descriptor, isServer)
const enableInline = !isServer
const cacheToUse = enableInline ? inlinedCache : normalCache
const cached = cacheToUse.get(descriptor)
if (cached) {
return cached
}

let resolved: SFCScriptBlock | null
let resolved: SFCScriptBlock | null = null

if (compileScript) {
resolved = compileScript(descriptor, {
id: scopeId,
isProd,
inlineTemplate: !isServer,
templateOptions: getTemplateCompilerOptions(options, descriptor, scopeId),
})
try {
resolved = compileScript(descriptor, {
id: scopeId,
isProd,
inlineTemplate: enableInline,
templateOptions: enableInline
? getTemplateCompilerOptions(options, descriptor, scopeId)
: undefined,
})
} catch (e) {
pluginContext.error(createRollupError(descriptor.filename, e))
}
} else if (descriptor.scriptSetup) {
throw new Error(
pluginContext.error(
`<script setup> is not supported by the installed version of ` +
`@vue/compiler-sfc - please upgrade.`
)
} else {
resolved = descriptor.script
}

if (isServer) {
normalCache.set(descriptor, resolved)
} else {
inlinedCache.set(descriptor, resolved)
}

cacheToUse.set(descriptor, resolved)
return resolved
}
21 changes: 14 additions & 7 deletions src/sfc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ export function transformSFCEntry(
// feature information
const hasScoped = descriptor.styles.some((s) => s.scoped)

const hasTemplateImport =
descriptor.template &&
// script setup compiles template inline, do not import again
(isServer || !descriptor.scriptSetup)
const useInlineTemplate = descriptor.scriptSetup && !isServer
const hasTemplateImport = descriptor.template && !useInlineTemplate

const templateImport = hasTemplateImport
? genTemplateCode(descriptor, scopeId, isServer)
Expand All @@ -62,7 +60,8 @@ export function transformSFCEntry(
scopeId,
isProduction,
isServer,
options
options,
pluginContext
)
const stylesCode = genStyleCode(descriptor, scopeId, options.preprocessStyles)
const customBlocksCode = getCustomBlock(descriptor, filterCustomBlock)
Expand Down Expand Up @@ -118,10 +117,18 @@ function genScriptCode(
scopeId: string,
isProd: boolean,
isServer: boolean,
options: Options
options: Options,
pluginContext: TransformPluginContext
) {
let scriptImport = `const script = {}`
const script = resolveScript(descriptor, scopeId, isProd, isServer, options)
const script = resolveScript(
descriptor,
scopeId,
isProd,
isServer,
options,
pluginContext
)
if (script) {
const src = script.src || descriptor.filename
const attrsQuery = attrsToQuery(script.attrs, 'js')
Expand Down
2 changes: 1 addition & 1 deletion src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function getTemplateCompilerOptions(
preprocessLang &&
options.templatePreprocessOptions &&
options.templatePreprocessOptions[preprocessLang]
const resolvedScript = getResolvedScript(descriptor, isServer)
const resolvedScript = getResolvedScript(descriptor, !isServer)
return {
filename: descriptor.filename,
inMap: block.src ? undefined : block.map,
Expand Down

0 comments on commit 298fc90

Please sign in to comment.