Skip to content

Commit

Permalink
fix: broken worker urls in 3rd party modules (fix vitejs#10838)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielku15 committed Apr 13, 2024
1 parent 58a2938 commit 89ce8d8
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Plugin } from '../plugin'
import { evalValue, injectQuery, transformStableResult } from '../utils'
import type { ResolveFn } from '..'
import { cleanUrl, slash } from '../../shared/utils'
import { getDepsOptimizer, optimizedDepInfoFromFile } from '../optimizer'
import type { WorkerType } from './worker'
import { WORKER_FILE_ID, workerFileToUrl } from './worker'
import { fileToUrl } from './asset'
Expand Down Expand Up @@ -114,6 +115,22 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
asSrc: true,
}

function tryOptimizedDepResolve(ssr: boolean, url: string, depId: string) {
const depsOptimizer = getDepsOptimizer(config, ssr)

if (depsOptimizer?.isOptimizedDepFile(depId)) {
const depFile = cleanUrl(depId)
const info = optimizedDepInfoFromFile(depsOptimizer.metadata, depFile)
const depSrc = info?.src
if (depSrc) {
const resolvedFile = path.resolve(path.dirname(depSrc), url)
return tryFsResolve(resolvedFile, fsResolveOptions)
}
}

return undefined
}

return {
name: 'vite:worker-import-meta-url',

Expand Down Expand Up @@ -148,10 +165,14 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
s ||= new MagicString(code)
const workerType = getWorkerType(code, cleanString, endIndex)
const url = rawUrl.slice(1, -1)

let file: string | undefined
if (url[0] === '.') {
file = path.resolve(path.dirname(id), url)
file = tryFsResolve(file, fsResolveOptions) ?? file
file =
tryFsResolve(file, fsResolveOptions) ??
tryOptimizedDepResolve(options?.ssr === true, url, id) ??
file
} else {
workerResolver ??= config.createResolver({
extensions: [],
Expand Down

0 comments on commit 89ce8d8

Please sign in to comment.