Home / File/ ssrTransform.ts — vite Source File

ssrTransform.ts — vite Source File

Architecture documentation for ssrTransform.ts, a typescript file in the vite codebase. 0 imports, 3 dependents.

File typescript HMRClient WebSocketTransport 3 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  e5afda66_d325_b39d_895d_9edd47b6bafd["ssrTransform.ts"]
  29e248d2_9983_1037_00e6_8bcd9ee87840["runner.ts"]
  29e248d2_9983_1037_00e6_8bcd9ee87840 --> e5afda66_d325_b39d_895d_9edd47b6bafd
  bf48f7e9_65f6_54c3_7dce_f44769180bd7["types.ts"]
  bf48f7e9_65f6_54c3_7dce_f44769180bd7 --> e5afda66_d325_b39d_895d_9edd47b6bafd
  20c20aaf_ad5b_4014_72b5_c8262a2b5be1["ssrTransform.ts"]
  20c20aaf_ad5b_4014_72b5_c8262a2b5be1 --> e5afda66_d325_b39d_895d_9edd47b6bafd
  style e5afda66_d325_b39d_895d_9edd47b6bafd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

export interface DefineImportMetadata {
  /**
   * Imported names before being transformed to `ssrImportKey`
   *
   * import foo, { bar as baz, qux } from 'hello'
   * => ['default', 'bar', 'qux']
   *
   * import * as namespace from 'world
   * => undefined
   */
  importedNames?: string[]
}

export interface SSRImportMetadata extends DefineImportMetadata {
  isDynamicImport?: boolean
}

/**
 * Vite converts `import { } from 'foo'` to `const _ = __vite_ssr_import__('foo')`.
 * Top-level imports and dynamic imports work slightly differently in Node.js.
 * This function normalizes the differences so it matches prod behaviour.
 */
export function analyzeImportedModDifference(
  mod: any,
  rawId: string,
  moduleType: string | undefined,
  metadata?: SSRImportMetadata,
): void {
  // No normalization needed if the user already dynamic imports this module
  if (metadata?.isDynamicImport) return

  // If the user named imports a specifier that can't be analyzed, error.
  // If the module doesn't import anything explicitly, e.g. `import 'foo'` or
  // `import * as foo from 'foo'`, we can skip.
  if (metadata?.importedNames?.length) {
    const missingBindings = metadata.importedNames.filter((s) => !(s in mod))
    if (missingBindings.length) {
      const lastBinding = missingBindings[missingBindings.length - 1]

      // For invalid named exports only, similar to how Node.js errors for top-level imports.
      // But since we transform as dynamic imports, we need to emulate the error manually.
      if (moduleType === 'module') {
        throw new SyntaxError(
          `[vite] The requested module '${rawId}' does not provide an export named '${lastBinding}'`,
        )
      } else {
        // For non-ESM, named imports is done via static analysis with cjs-module-lexer in Node.js.
        // Copied from Node.js
        throw new SyntaxError(`\
[vite] Named export '${lastBinding}' not found. The requested module '${rawId}' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '${rawId}';
const {${missingBindings.join(', ')}} = pkg;
`)
      }
    }
  }
}

Domain

Subdomains

Frequently Asked Questions

What does ssrTransform.ts do?
ssrTransform.ts is a source file in the vite codebase, written in typescript. It belongs to the HMRClient domain, WebSocketTransport subdomain.
What functions are defined in ssrTransform.ts?
ssrTransform.ts defines 1 function(s): analyzeImportedModDifference.
What files import ssrTransform.ts?
ssrTransform.ts is imported by 3 file(s): runner.ts, ssrTransform.ts, types.ts.
Where is ssrTransform.ts in the architecture?
ssrTransform.ts is located at packages/vite/src/shared/ssrTransform.ts (domain: HMRClient, subdomain: WebSocketTransport, directory: packages/vite/src/shared).

Analyze Your Own Codebase

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

Try Supermodel Free