From a0a05f997d164f5805379a1170f7708166f9683b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Tue, 17 Oct 2023 20:29:32 +0800 Subject: [PATCH] refactor: unwrap if...else --- packages/compiler-sfc/src/compileScript.ts | 103 ++++++++++----------- 1 file changed, 48 insertions(+), 55 deletions(-) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 58988359068..86aeab4423e 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -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__) {