Home / Function/ compileScript() — vue Function Reference

compileScript() — vue Function Reference

Architecture documentation for the compileScript() function in compileScript.ts from the vue codebase.

Entity Profile

Dependency Diagram

graph TD
  4dff3da5_c3f4_4303_7989_1d16ed3f091d["compileScript()"]
  c9346cac_54e3_f6ca_68a7_03c6e82c9609["compileScript.ts"]
  4dff3da5_c3f4_4303_7989_1d16ed3f091d -->|defined in| c9346cac_54e3_f6ca_68a7_03c6e82c9609
  14d6c8d1_8b2c_a3db_d77c_d93821233e45["analyzeScriptBindings()"]
  4dff3da5_c3f4_4303_7989_1d16ed3f091d -->|calls| 14d6c8d1_8b2c_a3db_d77c_d93821233e45
  3f2874de_dacf_5309_7716_ce1aa1b354a6["rewriteDefault()"]
  4dff3da5_c3f4_4303_7989_1d16ed3f091d -->|calls| 3f2874de_dacf_5309_7716_ce1aa1b354a6
  3b99ed03_b8e9_f8e3_3a3a_885f324f52b7["genNormalScriptCssVarsCode()"]
  4dff3da5_c3f4_4303_7989_1d16ed3f091d -->|calls| 3b99ed03_b8e9_f8e3_3a3a_885f324f52b7
  4fe50d93_1397_443f_6dd0_df2c8c87a955["isImportUsed()"]
  4dff3da5_c3f4_4303_7989_1d16ed3f091d -->|calls| 4fe50d93_1397_443f_6dd0_df2c8c87a955
  001d4700_1435_f6f3_5057_66425a67ca75["isCallOf()"]
  4dff3da5_c3f4_4303_7989_1d16ed3f091d -->|calls| 001d4700_1435_f6f3_5057_66425a67ca75
  484ec320_49bc_b969_d86d_edc246af3234["walkIdentifiers()"]
  4dff3da5_c3f4_4303_7989_1d16ed3f091d -->|calls| 484ec320_49bc_b969_d86d_edc246af3234
  393d75e6_b12b_47a2_6562_3e79f38e2ac5["toRuntimeTypeString()"]
  4dff3da5_c3f4_4303_7989_1d16ed3f091d -->|calls| 393d75e6_b12b_47a2_6562_3e79f38e2ac5
  3de0d66d_b49c_710a_a5dd_4aad647e8642["walkDeclaration()"]
  4dff3da5_c3f4_4303_7989_1d16ed3f091d -->|calls| 3de0d66d_b49c_710a_a5dd_4aad647e8642
  e49b037f_d27b_861a_74e2_10d6a15d0ca4["warnOnce()"]
  4dff3da5_c3f4_4303_7989_1d16ed3f091d -->|calls| e49b037f_d27b_861a_74e2_10d6a15d0ca4
  203503b8_22db_80c3_4c32_90580ed9ee87["isFunctionType()"]
  4dff3da5_c3f4_4303_7989_1d16ed3f091d -->|calls| 203503b8_22db_80c3_4c32_90580ed9ee87
  15980fd6_fbc3_7a96_daca_3f054741f0e8["registerBinding()"]
  4dff3da5_c3f4_4303_7989_1d16ed3f091d -->|calls| 15980fd6_fbc3_7a96_daca_3f054741f0e8
  d0f5ebe6_ca7a_17bd_e29b_5326c7ef914b["recordType()"]
  4dff3da5_c3f4_4303_7989_1d16ed3f091d -->|calls| d0f5ebe6_ca7a_17bd_e29b_5326c7ef914b
  2c50a6c2_cede_55c3_a14b_5f395f412ebe["extractRuntimeProps()"]
  4dff3da5_c3f4_4303_7989_1d16ed3f091d -->|calls| 2c50a6c2_cede_55c3_a14b_5f395f412ebe
  style 4dff3da5_c3f4_4303_7989_1d16ed3f091d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/compiler-sfc/src/compileScript.ts lines 98–1273

export function compileScript(
  sfc: SFCDescriptor,
  options: SFCScriptCompileOptions = { id: '' }
): SFCScriptBlock {
  let { filename, script, scriptSetup, source } = sfc
  const isProd = !!options.isProd
  const genSourceMap = options.sourceMap !== false
  let refBindings: string[] | undefined

  const cssVars = sfc.cssVars
  const scopeId = options.id ? options.id.replace(/^data-v-/, '') : ''
  const scriptLang = script && script.lang
  const scriptSetupLang = scriptSetup && scriptSetup.lang
  const isTS =
    scriptLang === 'ts' ||
    scriptLang === 'tsx' ||
    scriptSetupLang === 'ts' ||
    scriptSetupLang === 'tsx'

  // resolve parser plugins
  const plugins: ParserPlugin[] = []
  if (!isTS || scriptLang === 'tsx' || scriptSetupLang === 'tsx') {
    plugins.push('jsx')
  } else {
    // If don't match the case of adding jsx, should remove the jsx from the babelParserPlugins
    if (options.babelParserPlugins)
      options.babelParserPlugins = options.babelParserPlugins.filter(
        n => n !== 'jsx'
      )
  }
  if (options.babelParserPlugins) plugins.push(...options.babelParserPlugins)
  if (isTS) {
    plugins.push('typescript')
    if (!plugins.includes('decorators')) {
      plugins.push('decorators-legacy')
    }
  }

  if (!scriptSetup) {
    if (!script) {
      throw new Error(`[@vue/compiler-sfc] SFC contains no <script> tags.`)
    }
    if (scriptLang && !isTS && scriptLang !== 'jsx') {
      // do not process non js/ts script blocks
      return script
    }
    try {
      let content = script.content
      let map = script.map
      const scriptAst = _parse(content, {
        plugins,
        sourceType: 'module'
      }).program
      const bindings = analyzeScriptBindings(scriptAst.body)
      if (cssVars.length) {
        content = rewriteDefault(content, DEFAULT_VAR, plugins)
        content += genNormalScriptCssVarsCode(
          cssVars,
          bindings,
          scopeId,
          isProd
        )
        content += `\nexport default ${DEFAULT_VAR}`
      }
      return {
        ...script,
        content,
        map,
        bindings,
        scriptAst: scriptAst.body
      }
    } catch (e: any) {
      // silently fallback if parse fails since user may be using custom
      // babel syntax
      return script
    }
  }

  if (script && scriptLang !== scriptSetupLang) {
    throw new Error(
      `[@vue/compiler-sfc] <script> and <script setup> must have the same ` +

Domain

Subdomains

Frequently Asked Questions

What does compileScript() do?
compileScript() is a function in the vue codebase, defined in packages/compiler-sfc/src/compileScript.ts.
Where is compileScript() defined?
compileScript() is defined in packages/compiler-sfc/src/compileScript.ts at line 98.
What does compileScript() call?
compileScript() calls 17 function(s): analyzeScriptBindings, extractRuntimeEmits, extractRuntimeProps, genCssVarsCode, genNormalScriptCssVarsCode, genRuntimeEmits, getObjectOrArrayExpressionKeys, isCallOf, and 9 more.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free