Skip to content

Commit

Permalink
refactor: unwrap if...else
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Oct 17, 2023
1 parent 9a8053c commit a0a05f9
Showing 1 changed file with 48 additions and 55 deletions.
103 changes: 48 additions & 55 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,65 +891,58 @@ export function compileScript(
}
}
returned = returned.replace(/, $/, '') + ` }`
} else {
} else if (sfc.template && !sfc.template.src) {
// inline mode
if (sfc.template && !sfc.template.src) {
if (options.templateOptions && options.templateOptions.ssr) {
hasInlinedSsrRenderFn = true
}
// inline render function mode - we are going to compile the template and
// inline it right here
const { code, ast, preamble, tips, errors } = compileTemplate({
filename,
source: sfc.template.content,
inMap: sfc.template.map,
...options.templateOptions,
id: scopeId,
scoped: sfc.styles.some(s => s.scoped),
isProd: options.isProd,
ssrCssVars: sfc.cssVars,
compilerOptions: {
...(options.templateOptions &&
options.templateOptions.compilerOptions),
inline: true,
isTS: ctx.isTS,
bindingMetadata: ctx.bindingMetadata
}
})
if (tips.length) {
tips.forEach(warnOnce)
}
const err = errors[0]
if (typeof err === 'string') {
throw new Error(err)
} else if (err) {
if (err.loc) {
err.message +=
`\n\n` +
sfc.filename +
'\n' +
generateCodeFrame(
source,
err.loc.start.offset,
err.loc.end.offset
) +
`\n`
}
throw err
}
if (preamble) {
ctx.s.prepend(preamble)
if (options.templateOptions && options.templateOptions.ssr) {
hasInlinedSsrRenderFn = true
}
// inline render function mode - we are going to compile the template and
// inline it right here
const { code, ast, preamble, tips, errors } = compileTemplate({
filename,
source: sfc.template.content,
inMap: sfc.template.map,
...options.templateOptions,
id: scopeId,
scoped: sfc.styles.some(s => s.scoped),
isProd: options.isProd,
ssrCssVars: sfc.cssVars,
compilerOptions: {
...(options.templateOptions && options.templateOptions.compilerOptions),
inline: true,
isTS: ctx.isTS,
bindingMetadata: ctx.bindingMetadata
}
// avoid duplicated unref import
// as this may get injected by the render function preamble OR the
// css vars codegen
if (ast && ast.helpers.has(UNREF)) {
ctx.helperImports.delete('unref')
})
if (tips.length) {
tips.forEach(warnOnce)
}
const err = errors[0]
if (typeof err === 'string') {
throw new Error(err)
} else if (err) {
if (err.loc) {
err.message +=
`\n\n` +
sfc.filename +
'\n' +
generateCodeFrame(source, err.loc.start.offset, err.loc.end.offset) +
`\n`
}
returned = code
} else {
returned = `() => {}`
throw err
}
if (preamble) {
ctx.s.prepend(preamble)
}
// avoid duplicated unref import
// as this may get injected by the render function preamble OR the
// css vars codegen
if (ast && ast.helpers.has(UNREF)) {
ctx.helperImports.delete('unref')
}
returned = code
} else {
returned = `() => {}`
}

if (!options.inlineTemplate && !__TEST__) {
Expand Down

0 comments on commit a0a05f9

Please sign in to comment.