Skip to content

Dep optimizer rewrites bare-specifier new URL('pkg/worker.js', import.meta.url) in pre-bundled deps to a non-existent path (regression in 8.0.0, #21434) #23785

Closed as duplicate

Description

@sihwang79

Describe the bug

Since Vite 8.0.0 (#21434, "fix(optimizer): map relative new URL paths to correct relative file location"), the dependency optimizer rewrites every new URL('<string>', import.meta.url) inside a pre-bundled dependency as if the string were relative to the original module (path.resolve(path.dirname(id), url) in packages/vite/src/node/optimizer/rolldownDepPlugin.ts).

Libraries commonly ship worker expressions with a bare specifier so that the consumer's bundler resolves the worker from the app (e.g. new Worker(new URL('monaco-editor/editor/editor.worker.js', import.meta.url), { type: 'module' })). After pre-bundling, the bare specifier becomes <library dir>/<specifier> — a file that does not exist — and the dev server answers with the SPA fallback HTML, so the worker fails ("Could not create web worker(s)").

The worker plugin (workerImportMetaUrl.ts) itself resolves a non-relative string relative first and then as a package, so the optimizer and the worker plugin now disagree. Vite 7 left the expression to the worker plugin, which resolved the bare specifier from the app root. Production builds are fine in both versions.

/* @vite-ignore */ is not a workaround: the optimizer skips it, but the worker plugin's hook filter (a strict regex on the original code) then skips the module, so the production build no longer bundles the worker.

Reproduction

Self-contained script (no repo needed): npm i vite@8.3.4 && node repro.mjs, then again with vite@7.3.1.

// Self-contained repro: a pre-bundled dependency whose worker expression uses a bare specifier.
// Run: npm i vite@<version> && node repro.mjs
import { createServer, version } from 'vite'
import fs from 'node:fs'
import path from 'node:path'

const root = path.resolve('app')
fs.rmSync(root, { recursive: true, force: true })
const files = {
  // the worker lives in another package that the app installs (so the bare specifier resolves from the app root)
  'node_modules/fake-dep/package.json': JSON.stringify({ name: 'fake-dep', version: '1.0.0', type: 'module', exports: { './worker.js': './worker.js' } }),
  'node_modules/fake-dep/worker.js': "self.postMessage('worker-ok')\n",
  // the library that ships `new Worker(new URL('<bare specifier>', import.meta.url))`
  'node_modules/fake-lib/package.json': JSON.stringify({ name: 'fake-lib', version: '1.0.0', type: 'module', exports: { '.': './index.js' } }),
  'node_modules/fake-lib/index.js': "export const start = () => new Worker(new URL('fake-dep/worker.js', import.meta.url), { type: 'module' })\n",
  'package.json': JSON.stringify({ name: 'app', private: true, type: 'module' }),
  'index.html': '<script type="module" src="/main.js"></script>\n',
  'main.js': "import { start } from 'fake-lib'\nstart()\n",
}
for (const [f, t] of Object.entries(files)) { fs.mkdirSync(path.dirname(path.join(root, f)), { recursive: true }); fs.writeFileSync(path.join(root, f), t) }

const server = await createServer({ root, logLevel: 'silent', optimizeDeps: { include: ['fake-lib'] }, server: { port: 5299 } })
await server.listen()
const base = 'http://localhost:5299'
// follow main.js to the pre-bundled fake-lib module and read the worker URL it now contains
const main = await (await fetch(base + '/main.js')).text()
const depPath = main.match(/from\s+["']([^"']*fake-lib[^"']*)["']/)[1]
const depUrl = new URL(depPath, base)
const code = await (await fetch(depUrl)).text()
const url = code.match(/new URL\(\s*(?:\/\*.*?\*\/\s*)?["']([^"']+)["']/)[1]
const resolved = new URL(url, depUrl)
const res = await fetch(resolved)
console.log(`vite ${version}`)
console.log(`  pre-bundled module: ${depUrl.pathname}`)
console.log(`  worker URL in it:   ${url}`)
console.log(`  GET ${resolved.pathname} -> ${res.status} ${res.headers.get('content-type')}`)
await server.close()

Output:

vite 8.3.4
  pre-bundled module: /node_modules/.vite/deps/fake-lib.js
  worker URL in it:   /node_modules/fake-lib/fake-dep/worker.js?worker_file&type=module
  GET /node_modules/fake-lib/fake-dep/worker.js -> 200 text/html
vite 7.3.1
  pre-bundled module: /node_modules/.vite/deps/fake-lib.js
  worker URL in it:   /node_modules/fake-dep/worker.js?worker_file&type=module
  GET /node_modules/fake-dep/worker.js -> 200 text/javascript

Suggested fix

Only rebase strings that start with ./ or ../ (what #21434 was about), or — matching the worker plugin's behaviour — only rebase when the file relative to the original module exists; otherwise leave the expression untouched so the worker plugin resolves the bare specifier as before.

Workaround we ship

Our library build emits a one-line shim at the rewritten location (dirname(chunk)/<specifier> containing import '<specifier>'), which Vite 8 dev follows; Vite 7 and production builds ignore it. We would like to drop it once this is fixed.

System Info

vite 8.3.4 (latest; main has the same code), Node 24.14.1, Linux (WSL2), pnpm 10 (isolated node_modules)

Used Package Manager

pnpm

Activity

  1. added
    duplicate
    on Oct 16, 2026
  2. locked and limited conversation to collaborators on Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions