Home / Function/ webWorkerPlugin() — vite Function Reference

webWorkerPlugin() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  1a156a4f_5015_f60f_3a54_6eb2168861ca["webWorkerPlugin()"]
  971bf58d_477c_c4d8_0c3b_735e572044c4["worker.ts"]
  1a156a4f_5015_f60f_3a54_6eb2168861ca -->|defined in| 971bf58d_477c_c4d8_0c3b_735e572044c4
  b1b40b5b_3e43_2197_dea0_d36389d312a1["resolvePlugins()"]
  b1b40b5b_3e43_2197_dea0_d36389d312a1 -->|calls| 1a156a4f_5015_f60f_3a54_6eb2168861ca
  10b9dea8_362c_1af2_93be_afa4dd9aed9e["cleanUrl()"]
  1a156a4f_5015_f60f_3a54_6eb2168861ca -->|calls| 10b9dea8_362c_1af2_93be_afa4dd9aed9e
  75aa4483_7f33_5bdd_19ac_866cac2679e4["bundleWorkerEntry()"]
  1a156a4f_5015_f60f_3a54_6eb2168861ca -->|calls| 75aa4483_7f33_5bdd_19ac_866cac2679e4
  2263fc73_3545_6a6d_f020_363141e760f3["workerFileToUrl()"]
  1a156a4f_5015_f60f_3a54_6eb2168861ca -->|calls| 2263fc73_3545_6a6d_f020_363141e760f3
  b8f7857f_eb38_9730_a8eb_658b77a590c9["toOutputFilePathInJSForBundledDev()"]
  1a156a4f_5015_f60f_3a54_6eb2168861ca -->|calls| b8f7857f_eb38_9730_a8eb_658b77a590c9
  13b5d2b4_0a9d_90ef_9bfa_129b74a9d64e["fileToUrl()"]
  1a156a4f_5015_f60f_3a54_6eb2168861ca -->|calls| 13b5d2b4_0a9d_90ef_9bfa_129b74a9d64e
  1948f092_e5a5_076b_2f59_79ef22dec191["injectQuery()"]
  1a156a4f_5015_f60f_3a54_6eb2168861ca -->|calls| 1948f092_e5a5_076b_2f59_79ef22dec191
  c25246ea_7a11_06af_dc93_7717f85216db["createToImportMetaURLBasedRelativeRuntime()"]
  1a156a4f_5015_f60f_3a54_6eb2168861ca -->|calls| c25246ea_7a11_06af_dc93_7717f85216db
  51afdf58_3045_64b1_cf5b_929b1091e877["get()"]
  1a156a4f_5015_f60f_3a54_6eb2168861ca -->|calls| 51afdf58_3045_64b1_cf5b_929b1091e877
  904cc18a_9dfa_0cfb_8ae9_26b377002833["getEntryFilenameFromHash()"]
  1a156a4f_5015_f60f_3a54_6eb2168861ca -->|calls| 904cc18a_9dfa_0cfb_8ae9_26b377002833
  0b9e0d55_d22c_da82_ca1d_a2e7d0af1d66["toOutputFilePathInJS()"]
  1a156a4f_5015_f60f_3a54_6eb2168861ca -->|calls| 0b9e0d55_d22c_da82_ca1d_a2e7d0af1d66
  41d0f7a0_ed36_9f0f_d0d6_f403e4f50763["encodeURIPath()"]
  1a156a4f_5015_f60f_3a54_6eb2168861ca -->|calls| 41d0f7a0_ed36_9f0f_d0d6_f403e4f50763
  cda585c1_e9a5_1b32_4692_68fb220c8378["getAssets()"]
  1a156a4f_5015_f60f_3a54_6eb2168861ca -->|calls| cda585c1_e9a5_1b32_4692_68fb220c8378
  style 1a156a4f_5015_f60f_3a54_6eb2168861ca fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/worker.ts lines 380–660

export function webWorkerPlugin(config: ResolvedConfig): Plugin {
  const isBuild = config.command === 'build'
  const isWorker = config.isWorker

  workerOutputCaches.set(config, new WorkerOutputCache())
  const emittedAssets = new Set<string>()

  return {
    name: 'vite:worker',

    buildStart() {
      if (isWorker) return
      emittedAssets.clear()
    },

    load: {
      filter: { id: workerOrSharedWorkerRE },
      async handler(id) {
        const workerMatch = workerOrSharedWorkerRE.exec(id)
        if (!workerMatch) return

        const { format } = config.worker
        const workerConstructor =
          workerMatch[1] === 'sharedworker' ? 'SharedWorker' : 'Worker'
        const workerType = config.isBundled
          ? format === 'es'
            ? 'module'
            : 'classic'
          : 'module'
        const workerTypeOption = `{
          ${workerType === 'module' ? `type: "module",` : ''}
          name: options?.name
        }`

        let urlCode: string
        if (config.isBundled) {
          if (isWorker && config.bundleChain.at(-1) === cleanUrl(id)) {
            urlCode = 'self.location.href'
          } else if (inlineRE.test(id)) {
            const result = await bundleWorkerEntry(config, id)
            for (const file of result.watchedFiles) {
              this.addWatchFile(file)
            }

            const jsContent = `const jsContent = ${JSON.stringify(result.entryCode)};`

            const code =
              // Using blob URL for SharedWorker results in multiple instances of a same worker
              workerConstructor === 'Worker'
                ? `${jsContent}
            const blob = typeof self !== "undefined" && self.Blob && new Blob([${
              // NOTE: Revoke the objURL after creating the worker, otherwise it breaks WebKit-based browsers
              workerType === 'classic'
                ? `'(self.URL || self.webkitURL).revokeObjectURL(self.location.href);',`
                : // `URL` is always available, in `Worker[type="module"]`
                  `'URL.revokeObjectURL(import.meta.url);',`
            }jsContent], { type: "text/javascript;charset=utf-8" });
            export default function WorkerWrapper(options) {
              let objURL;
              try {
                objURL = blob && (self.URL || self.webkitURL).createObjectURL(blob);
                if (!objURL) throw ''
                const worker = new ${workerConstructor}(objURL, ${workerTypeOption});
                worker.addEventListener("error", () => {
                  (self.URL || self.webkitURL).revokeObjectURL(objURL);
                });
                return worker;
              } catch(e) {
                return new ${workerConstructor}(
                  'data:text/javascript;charset=utf-8,' + encodeURIComponent(jsContent),
                  ${workerTypeOption}
                );
              }
            }`
                : `${jsContent}
            export default function WorkerWrapper(options) {
              return new ${workerConstructor}(
                'data:text/javascript;charset=utf-8,' + encodeURIComponent(jsContent),
                ${workerTypeOption}
              );
            }

Domain

Subdomains

Called By

Frequently Asked Questions

What does webWorkerPlugin() do?
webWorkerPlugin() is a function in the vite codebase, defined in packages/vite/src/node/plugins/worker.ts.
Where is webWorkerPlugin() defined?
webWorkerPlugin() is defined in packages/vite/src/node/plugins/worker.ts at line 380.
What does webWorkerPlugin() call?
webWorkerPlugin() calls 15 function(s): bundleWorkerEntry, cleanUrl, createToImportMetaURLBasedRelativeRuntime, encodeURIPath, fileToUrl, get, getAssets, getEntryFilenameFromHash, and 7 more.
What calls webWorkerPlugin()?
webWorkerPlugin() is called by 1 function(s): resolvePlugins.

Analyze Your Own Codebase

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

Try Supermodel Free