Home / Function/ webWorkerPostPlugin() — vite Function Reference

webWorkerPostPlugin() — vite Function Reference

Architecture documentation for the webWorkerPostPlugin() function in worker.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  89228559_98e1_be9f_e246_02c622573e81["webWorkerPostPlugin()"]
  971bf58d_477c_c4d8_0c3b_735e572044c4["worker.ts"]
  89228559_98e1_be9f_e246_02c622573e81 -->|defined in| 971bf58d_477c_c4d8_0c3b_735e572044c4
  8c4db194_5dfd_4391_cc9a_833655009196["resolveBuildPlugins()"]
  8c4db194_5dfd_4391_cc9a_833655009196 -->|calls| 89228559_98e1_be9f_e246_02c622573e81
  dfa3f5a8_b519_cb65_4b7e_9d4824406fd4["perEnvironmentPlugin()"]
  89228559_98e1_be9f_e246_02c622573e81 -->|calls| dfa3f5a8_b519_cb65_4b7e_9d4824406fd4
  style 89228559_98e1_be9f_e246_02c622573e81 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/worker.ts lines 314–378

export function webWorkerPostPlugin(config: ResolvedConfig): Plugin {
  if (config.isBundled && config.nativePluginEnabledLevel >= 1) {
    return perEnvironmentPlugin(
      'native:web-worker-post-plugin',
      (environment) => {
        if (environment.config.worker.format === 'iife') {
          return nativeWebWorkerPostPlugin()
        }
      },
    )
  }

  return {
    name: 'vite:worker-post',
    transform: {
      filter: {
        code: 'import.meta',
      },
      order: 'post',
      async handler(code, id) {
        // import.meta is unavailable in the IIFE worker, so we need to replace it
        if (this.environment.config.worker.format === 'iife') {
          await init

          let imports: readonly ImportSpecifier[]
          try {
            imports = parse(code)[0]
          } catch {
            // ignore if parse fails
            return
          }

          let injectedImportMeta = false
          let s: MagicString | undefined
          for (const { s: start, e: end, d: dynamicIndex } of imports) {
            // is import.meta
            if (dynamicIndex === -2) {
              const prop = code.slice(end, end + 4)
              if (prop === '.url') {
                s ||= new MagicString(code)
                s.overwrite(start, end + 4, 'self.location.href')
              } else {
                s ||= new MagicString(code)
                if (!injectedImportMeta) {
                  s.prepend(
                    'const _vite_importMeta = { url: self.location.href };\n',
                  )
                  injectedImportMeta = true
                }
                s.overwrite(start, end, '_vite_importMeta')
              }
            }
          }

          if (!s) return

          return {
            code: s.toString(),
            map: s.generateMap({ hires: 'boundary', source: id }),
          }
        }
      },
    },
  }
}

Domain

Subdomains

Frequently Asked Questions

What does webWorkerPostPlugin() do?
webWorkerPostPlugin() is a function in the vite codebase, defined in packages/vite/src/node/plugins/worker.ts.
Where is webWorkerPostPlugin() defined?
webWorkerPostPlugin() is defined in packages/vite/src/node/plugins/worker.ts at line 314.
What does webWorkerPostPlugin() call?
webWorkerPostPlugin() calls 1 function(s): perEnvironmentPlugin.
What calls webWorkerPostPlugin()?
webWorkerPostPlugin() is called by 1 function(s): resolveBuildPlugins.

Analyze Your Own Codebase

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

Try Supermodel Free