Home / File/ wasm.ts — vite Source File

wasm.ts — vite Source File

Architecture documentation for wasm.ts, a typescript file in the vite codebase. 11 imports, 1 dependents.

File typescript PluginSystem AssetManagement 11 imports 1 dependents 4 functions

Entity Profile

Dependency Diagram

graph LR
  d496a62e_d9d8_41d4_a984_96f1dbd5f4bd["wasm.ts"]
  45981d85_cbdd_e969_8c88_c17072ea0eda["build.ts"]
  d496a62e_d9d8_41d4_a984_96f1dbd5f4bd --> 45981d85_cbdd_e969_8c88_c17072ea0eda
  c25246ea_7a11_06af_dc93_7717f85216db["createToImportMetaURLBasedRelativeRuntime"]
  d496a62e_d9d8_41d4_a984_96f1dbd5f4bd --> c25246ea_7a11_06af_dc93_7717f85216db
  5abb8c87_ffcb_f2d4_7421_e36705d9e5c7["plugin.ts"]
  d496a62e_d9d8_41d4_a984_96f1dbd5f4bd --> 5abb8c87_ffcb_f2d4_7421_e36705d9e5c7
  dfa3f5a8_b519_cb65_4b7e_9d4824406fd4["perEnvironmentPlugin"]
  d496a62e_d9d8_41d4_a984_96f1dbd5f4bd --> dfa3f5a8_b519_cb65_4b7e_9d4824406fd4
  abfc9e70_3c15_b3f0_a595_3cf27afb7e64["utils.ts"]
  d496a62e_d9d8_41d4_a984_96f1dbd5f4bd --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  10b9dea8_362c_1af2_93be_afa4dd9aed9e["cleanUrl"]
  d496a62e_d9d8_41d4_a984_96f1dbd5f4bd --> 10b9dea8_362c_1af2_93be_afa4dd9aed9e
  e71b94ef_3010_e358_13d8_f3b3acb0a268["asset.ts"]
  d496a62e_d9d8_41d4_a984_96f1dbd5f4bd --> e71b94ef_3010_e358_13d8_f3b3acb0a268
  13b5d2b4_0a9d_90ef_9bfa_129b74a9d64e["fileToUrl"]
  d496a62e_d9d8_41d4_a984_96f1dbd5f4bd --> 13b5d2b4_0a9d_90ef_9bfa_129b74a9d64e
  ff79973e_f09f_1c6b_f6b5_d1707df47116["magic-string"]
  d496a62e_d9d8_41d4_a984_96f1dbd5f4bd --> ff79973e_f09f_1c6b_f6b5_d1707df47116
  db4d54eb_be78_e7bb_fac3_7f8913198d1d["filter"]
  d496a62e_d9d8_41d4_a984_96f1dbd5f4bd --> db4d54eb_be78_e7bb_fac3_7f8913198d1d
  693ca867_249b_3e5a_0ce1_8930413b7fcd["rolldown"]
  d496a62e_d9d8_41d4_a984_96f1dbd5f4bd --> 693ca867_249b_3e5a_0ce1_8930413b7fcd
  3bf7f0de_2a9f_6f04_cead_0321b3b7af01["index.ts"]
  3bf7f0de_2a9f_6f04_cead_0321b3b7af01 --> d496a62e_d9d8_41d4_a984_96f1dbd5f4bd
  style d496a62e_d9d8_41d4_a984_96f1dbd5f4bd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import MagicString from 'magic-string'
import { exactRegex } from 'rolldown/filter'
import type { BindingMagicString } from 'rolldown'
import { createToImportMetaURLBasedRelativeRuntime } from '../build'
import { type Plugin, perEnvironmentPlugin } from '../plugin'
import { cleanUrl } from '../../shared/utils'
import { assetUrlRE, fileToUrl } from './asset'

const wasmHelperId = '\0vite/wasm-helper.js'

const wasmInitRE = /(?<![?#].*)\.wasm\?init/

const wasmInitUrlRE: RegExp = /__VITE_WASM_INIT__([\w$]+)__/g

const wasmHelper = async (opts = {}, url: string) => {
  let result
  if (url.startsWith('data:')) {
    const urlContent = url.replace(/^data:.*?base64,/, '')
    let bytes
    if (typeof Buffer === 'function' && typeof Buffer.from === 'function') {
      bytes = Buffer.from(urlContent, 'base64')
    } else if (typeof atob === 'function') {
      const binaryString = atob(urlContent)
      bytes = new Uint8Array(binaryString.length)
      for (let i = 0; i < binaryString.length; i++) {
        bytes[i] = binaryString.charCodeAt(i)
      }
    } else {
      throw new Error(
        'Failed to decode base64-encoded data URL, Buffer and atob are not supported',
      )
    }
    result = await WebAssembly.instantiate(bytes, opts)
  } else {
    result = await instantiateFromUrl(url, opts)
  }
  return result.instance
}

const wasmHelperCode = wasmHelper.toString()

const instantiateFromUrl = async (url: string, opts?: WebAssembly.Imports) => {
  // https://github.com/mdn/webassembly-examples/issues/5
  // WebAssembly.instantiateStreaming requires the server to provide the
  // correct MIME type for .wasm files, which unfortunately doesn't work for
  // a lot of static file servers, so we just work around it by getting the
  // raw buffer.
  const response = await fetch(url)
  const contentType = response.headers.get('Content-Type') || ''
  if (
    'instantiateStreaming' in WebAssembly &&
    contentType.startsWith('application/wasm')
  ) {
    return WebAssembly.instantiateStreaming(response, opts)
  } else {
    const buffer = await response.arrayBuffer()
    return WebAssembly.instantiate(buffer, opts)
  }
}

// ... (101 more lines)

Domain

Subdomains

Frequently Asked Questions

What does wasm.ts do?
wasm.ts is a source file in the vite codebase, written in typescript. It belongs to the PluginSystem domain, AssetManagement subdomain.
What functions are defined in wasm.ts?
wasm.ts defines 4 function(s): instantiateFromFile, instantiateFromUrl, wasmHelper, wasmHelperPlugin.
What does wasm.ts depend on?
wasm.ts imports 11 module(s): asset.ts, build.ts, cleanUrl, createToImportMetaURLBasedRelativeRuntime, fileToUrl, filter, magic-string, perEnvironmentPlugin, and 3 more.
What files import wasm.ts?
wasm.ts is imported by 1 file(s): index.ts.
Where is wasm.ts in the architecture?
wasm.ts is located at packages/vite/src/node/plugins/wasm.ts (domain: PluginSystem, subdomain: AssetManagement, directory: packages/vite/src/node/plugins).

Analyze Your Own Codebase

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

Try Supermodel Free