Home / Function/ extractWorkerTypeFromAst() — vite Function Reference

extractWorkerTypeFromAst() — vite Function Reference

Architecture documentation for the extractWorkerTypeFromAst() function in workerImportMetaUrl.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  1c99a215_ac45_d0f0_ba6d_8a18ab03dcc7["extractWorkerTypeFromAst()"]
  f241296c_aba8_08ac_c4f9_aed85a9eb247["workerImportMetaUrl.ts"]
  1c99a215_ac45_d0f0_ba6d_8a18ab03dcc7 -->|defined in| f241296c_aba8_08ac_c4f9_aed85a9eb247
  2bc39ad0_e74f_e4f8_2016_48276895bfca["parseWorkerOptions()"]
  2bc39ad0_e74f_e4f8_2016_48276895bfca -->|calls| 1c99a215_ac45_d0f0_ba6d_8a18ab03dcc7
  f23f9ad3_5ac1_6998_4142_106a985db0b0["err()"]
  1c99a215_ac45_d0f0_ba6d_8a18ab03dcc7 -->|calls| f23f9ad3_5ac1_6998_4142_106a985db0b0
  style 1c99a215_ac45_d0f0_ba6d_8a18ab03dcc7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/workerImportMetaUrl.ts lines 42–94

function extractWorkerTypeFromAst(
  expression: Expression,
  optsStartIndex: number,
): 'classic' | 'module' | undefined {
  if (expression.type !== 'ObjectExpression') {
    return
  }

  let lastSpreadElementIndex = -1
  let typeProperty = null
  let typePropertyIndex = -1

  for (let i = 0; i < expression.properties.length; i++) {
    const property = expression.properties[i]

    if (property.type === 'SpreadElement') {
      lastSpreadElementIndex = i
      continue
    }

    if (
      property.type === 'Property' &&
      ((property.key.type === 'Identifier' && property.key.name === 'type') ||
        (property.key.type === 'Literal' && property.key.value === 'type'))
    ) {
      typeProperty = property
      typePropertyIndex = i
    }
  }

  if (typePropertyIndex === -1 && lastSpreadElementIndex === -1) {
    // No type property or spread element in use. Assume safe usage and default to classic
    return 'classic'
  }

  if (typePropertyIndex < lastSpreadElementIndex) {
    throw err(
      'Expected object spread to be used before the definition of the type property. ' +
        'Vite needs a static value for the type property to correctly infer it.',
      optsStartIndex,
    )
  }

  if (typeProperty?.value.type !== 'Literal') {
    throw err(
      'Expected worker options type property to be a literal value.',
      optsStartIndex,
    )
  }

  // Silently default to classic type like the getWorkerType method
  return typeProperty?.value.value === 'module' ? 'module' : 'classic'
}

Domain

Subdomains

Calls

Frequently Asked Questions

What does extractWorkerTypeFromAst() do?
extractWorkerTypeFromAst() is a function in the vite codebase, defined in packages/vite/src/node/plugins/workerImportMetaUrl.ts.
Where is extractWorkerTypeFromAst() defined?
extractWorkerTypeFromAst() is defined in packages/vite/src/node/plugins/workerImportMetaUrl.ts at line 42.
What does extractWorkerTypeFromAst() call?
extractWorkerTypeFromAst() calls 1 function(s): err.
What calls extractWorkerTypeFromAst()?
extractWorkerTypeFromAst() is called by 1 function(s): parseWorkerOptions.

Analyze Your Own Codebase

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

Try Supermodel Free