( ctx: ScriptCompileContext, scopeId: string, )
| 8 | export const normalScriptDefaultVar = `__default__` |
| 9 | |
| 10 | export function processNormalScript( |
| 11 | ctx: ScriptCompileContext, |
| 12 | scopeId: string, |
| 13 | ): SFCScriptBlock { |
| 14 | const script = ctx.descriptor.script! |
| 15 | try { |
| 16 | let content = script.content |
| 17 | let map = script.map |
| 18 | const scriptAst = ctx.scriptAst! |
| 19 | const bindings = analyzeScriptBindings(scriptAst.body) |
| 20 | const { cssVars } = ctx.descriptor |
| 21 | const { genDefaultAs, isProd } = ctx.options |
| 22 | |
| 23 | if (cssVars.length || genDefaultAs) { |
| 24 | const defaultVar = genDefaultAs || normalScriptDefaultVar |
| 25 | const s = new MagicString(content) |
| 26 | rewriteDefaultAST(scriptAst.body, s, defaultVar) |
| 27 | content = s.toString() |
| 28 | if (cssVars.length && !ctx.options.templateOptions?.ssr) { |
| 29 | content += genNormalScriptCssVarsCode( |
| 30 | cssVars, |
| 31 | bindings, |
| 32 | scopeId, |
| 33 | !!isProd, |
| 34 | defaultVar, |
| 35 | ) |
| 36 | } |
| 37 | if (!genDefaultAs) { |
| 38 | content += `\nexport default ${defaultVar}` |
| 39 | } |
| 40 | } |
| 41 | return { |
| 42 | ...script, |
| 43 | content, |
| 44 | map, |
| 45 | bindings, |
| 46 | scriptAst: scriptAst.body, |
| 47 | } |
| 48 | } catch (e: any) { |
| 49 | // silently fallback if parse fails since user may be using custom |
| 50 | // babel syntax |
| 51 | return script |
| 52 | } |
| 53 | } |
no test coverage detected