From a481a477caa861d1ce76eaee04c79c7bbf8095d2 Mon Sep 17 00:00:00 2001
From: Danielku15
Date: Sat, 13 Apr 2024 15:17:21 +0200
Subject: [PATCH 1/7] fix: broken worker urls in 3rd party modules (fix #10838)
---
.../src/node/plugins/workerImportMetaUrl.ts | 23 ++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/packages/vite/src/node/plugins/workerImportMetaUrl.ts b/packages/vite/src/node/plugins/workerImportMetaUrl.ts
index 22a5de2ec67b06..d438de991caf73 100644
--- a/packages/vite/src/node/plugins/workerImportMetaUrl.ts
+++ b/packages/vite/src/node/plugins/workerImportMetaUrl.ts
@@ -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'
@@ -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',
@@ -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: [],
From 8543320af1f76af1a8c7564f033439d080a4dbe1 Mon Sep 17 00:00:00 2001
From: Danielku15
Date: Sat, 13 Apr 2024 15:18:03 +0200
Subject: [PATCH 2/7] test: add integration test for worker in 3rd party module
---
.../worker/__tests__/es/worker-es.spec.ts | 6 ++++++
.../worker/__tests__/iife/worker-iife.spec.ts | 6 ++++++
.../worker-relative-base.spec.ts | 6 ++++++
.../worker/dep-with-worker/dep-worker.js | 19 +++++++++++++++++++
playground/worker/dep-with-worker/index.js | 16 ++++++++++++++++
.../worker/dep-with-worker/package.json | 7 +++++++
playground/worker/dep-with-worker/shared.js | 9 +++++++++
playground/worker/index.html | 6 ++++++
playground/worker/package.json | 3 ++-
playground/worker/worker/main-module.js | 3 +++
pnpm-lock.yaml | 10 ++++++++++
11 files changed, 90 insertions(+), 1 deletion(-)
create mode 100644 playground/worker/dep-with-worker/dep-worker.js
create mode 100644 playground/worker/dep-with-worker/index.js
create mode 100644 playground/worker/dep-with-worker/package.json
create mode 100644 playground/worker/dep-with-worker/shared.js
diff --git a/playground/worker/__tests__/es/worker-es.spec.ts b/playground/worker/__tests__/es/worker-es.spec.ts
index 3656b07c18d576..7a0d388138d2e0 100644
--- a/playground/worker/__tests__/es/worker-es.spec.ts
+++ b/playground/worker/__tests__/es/worker-es.spec.ts
@@ -240,3 +240,9 @@ test('self reference url worker', async () => {
'pong: main\npong: nested\n',
)
})
+
+test('dep with worker', async () => {
+ expectWithRetry(() => page.textContent('.dep-with-worker')).toBe(
+ 'ping: main\npong: worker',
+ )
+})
diff --git a/playground/worker/__tests__/iife/worker-iife.spec.ts b/playground/worker/__tests__/iife/worker-iife.spec.ts
index 77547e8426fbd1..0f1ffdc22fcdd2 100644
--- a/playground/worker/__tests__/iife/worker-iife.spec.ts
+++ b/playground/worker/__tests__/iife/worker-iife.spec.ts
@@ -192,3 +192,9 @@ function decodeSourceMapUrl(content: string) {
).toString(),
)
}
+
+test('dep with worker', async () => {
+ expectWithRetry(() => page.textContent('.dep-with-worker')).toBe(
+ 'ping: main\npong: worker',
+ )
+})
diff --git a/playground/worker/__tests__/relative-base/worker-relative-base.spec.ts b/playground/worker/__tests__/relative-base/worker-relative-base.spec.ts
index 6c3180c7e2aa31..bff516e7428271 100644
--- a/playground/worker/__tests__/relative-base/worker-relative-base.spec.ts
+++ b/playground/worker/__tests__/relative-base/worker-relative-base.spec.ts
@@ -173,3 +173,9 @@ test('self reference url worker', async () => {
'pong: main\npong: nested\n',
)
})
+
+test('dep with worker', async () => {
+ expectWithRetry(() => page.textContent('.dep-with-worker')).toBe(
+ 'ping: main\npong: worker',
+ )
+})
diff --git a/playground/worker/dep-with-worker/dep-worker.js b/playground/worker/dep-with-worker/dep-worker.js
new file mode 100644
index 00000000000000..df0ece2e1852de
--- /dev/null
+++ b/playground/worker/dep-with-worker/dep-worker.js
@@ -0,0 +1,19 @@
+import { sharedBetweenWorkerAndMain } from './shared'
+
+self.onmessage = (e) => {
+ if (e.data === 'ping') {
+ self.postMessage({ msg })
+ }
+}
+self.postMessage({
+ msg,
+ mode,
+ bundleWithPlugin,
+ msgFromDep,
+ viteSvg,
+ metaUrl,
+ name,
+})
+
+// for sourcemap
+sharedBetweenWorkerAndMain('dep-worker')
diff --git a/playground/worker/dep-with-worker/index.js b/playground/worker/dep-with-worker/index.js
new file mode 100644
index 00000000000000..3cbf9f496d7a92
--- /dev/null
+++ b/playground/worker/dep-with-worker/index.js
@@ -0,0 +1,16 @@
+import { sharedBetweenWorkerAndMain } from './shared'
+
+export function startWorker(el) {
+ sharedBetweenWorkerAndMain(el, 'ping: main')
+
+ const worker = new Worker(
+ new URL('./dep-worker.mjs?worker', import.meta.url),
+ {
+ type: 'module ',
+ },
+ )
+ worker.postMessage('pong: worker')
+ worker.addEventListener('message', (e) => {
+ sharedBetweenWorkerAndMain(el, e.data.msg)
+ })
+}
diff --git a/playground/worker/dep-with-worker/package.json b/playground/worker/dep-with-worker/package.json
new file mode 100644
index 00000000000000..b5c609a362842a
--- /dev/null
+++ b/playground/worker/dep-with-worker/package.json
@@ -0,0 +1,7 @@
+{
+ "name": "@vitejs/test-dep-with-worker",
+ "private": true,
+ "version": "1.0.0",
+ "type": "module",
+ "main": "index.js"
+}
diff --git a/playground/worker/dep-with-worker/shared.js b/playground/worker/dep-with-worker/shared.js
new file mode 100644
index 00000000000000..891f0314f1f2f7
--- /dev/null
+++ b/playground/worker/dep-with-worker/shared.js
@@ -0,0 +1,9 @@
+export function sharedBetweenWorkerAndMain(el, text) {
+ if (self && self.postMessage) {
+ self.postMessage({ msg: text })
+ } else if (document.querySelector(el).textContent.length) {
+ document.querySelector(el).textContent += '\n' + text
+ } else {
+ document.querySelector(el).textContent += text
+ }
+}
diff --git a/playground/worker/index.html b/playground/worker/index.html
index c1944046af6998..436342fef4aae0 100644
--- a/playground/worker/index.html
+++ b/playground/worker/index.html
@@ -215,6 +215,12 @@
+
+ worker within dependency
+ .dep-with-worker
+
+
+