From 391bb4917b55baf3fdb01c6fc3d782d6c51be6c8 Mon Sep 17 00:00:00 2001
From: Hiroshi Ogawa
Date: Thu, 15 Aug 2024 20:11:36 +0900
Subject: [PATCH] fix(worker): handle self reference url worker in dependency
for build (#17846)
---
packages/vite/src/node/plugins/worker.ts | 2 +-
.../vite/src/node/plugins/workerImportMetaUrl.ts | 2 +-
playground/worker/__tests__/es/worker-es.spec.ts | 2 +-
.../worker/__tests__/iife/worker-iife.spec.ts | 8 +++++++-
.../worker-sourcemap-hidden.spec.ts | 2 +-
.../worker-sourcemap-inline.spec.ts | 2 +-
.../__tests__/sourcemap/worker-sourcemap.spec.ts | 2 +-
.../worker/dep-self-reference-url-worker/index.js | 9 +++++++++
.../dep-self-reference-url-worker/package.json | 7 +++++++
.../worker/dep-self-reference-url-worker/worker.js | 14 ++++++++++++++
playground/worker/index.html | 5 +++++
playground/worker/package.json | 1 +
playground/worker/vite.config-iife.js | 3 +++
playground/worker/worker/main-module.js | 6 ++++++
pnpm-lock.yaml | 10 ++++++++++
15 files changed, 68 insertions(+), 7 deletions(-)
create mode 100644 playground/worker/dep-self-reference-url-worker/index.js
create mode 100644 playground/worker/dep-self-reference-url-worker/package.json
create mode 100644 playground/worker/dep-self-reference-url-worker/worker.js
diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts
index 2e614ec7e0fa85..ffd01de70a272a 100644
--- a/packages/vite/src/node/plugins/worker.ts
+++ b/packages/vite/src/node/plugins/worker.ts
@@ -292,7 +292,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
let urlCode: string
if (isBuild) {
- if (isWorker && this.getModuleInfo(cleanUrl(id))?.isEntry) {
+ if (isWorker && config.bundleChain.at(-1) === cleanUrl(id)) {
urlCode = 'self.location.href'
} else if (inlineRE.test(id)) {
const chunk = await bundleWorkerEntry(config, id)
diff --git a/packages/vite/src/node/plugins/workerImportMetaUrl.ts b/packages/vite/src/node/plugins/workerImportMetaUrl.ts
index 4065c6470aca98..acf1c87a480478 100644
--- a/packages/vite/src/node/plugins/workerImportMetaUrl.ts
+++ b/packages/vite/src/node/plugins/workerImportMetaUrl.ts
@@ -171,7 +171,7 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
if (
isBuild &&
config.isWorker &&
- this.getModuleInfo(cleanUrl(file))?.isEntry
+ config.bundleChain.at(-1) === cleanUrl(file)
) {
s.update(expStart, expEnd, 'self.location.href')
} else {
diff --git a/playground/worker/__tests__/es/worker-es.spec.ts b/playground/worker/__tests__/es/worker-es.spec.ts
index 27e615dfab56af..0af2cf7dda15ef 100644
--- a/playground/worker/__tests__/es/worker-es.spec.ts
+++ b/playground/worker/__tests__/es/worker-es.spec.ts
@@ -111,7 +111,7 @@ describe.runIf(isBuild)('build', () => {
test('inlined code generation', async () => {
const assetsDir = path.resolve(testDir, 'dist/es/assets')
const files = fs.readdirSync(assetsDir)
- expect(files.length).toBe(34)
+ expect(files.length).toBe(35)
const index = files.find((f) => f.includes('main-module'))
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8')
const worker = files.find((f) => f.includes('my-worker'))
diff --git a/playground/worker/__tests__/iife/worker-iife.spec.ts b/playground/worker/__tests__/iife/worker-iife.spec.ts
index 5d5fa297f2161e..a2f981e694dc88 100644
--- a/playground/worker/__tests__/iife/worker-iife.spec.ts
+++ b/playground/worker/__tests__/iife/worker-iife.spec.ts
@@ -75,7 +75,7 @@ describe.runIf(isBuild)('build', () => {
test('inlined code generation', async () => {
const assetsDir = path.resolve(testDir, 'dist/iife/assets')
const files = fs.readdirSync(assetsDir)
- expect(files.length).toBe(22)
+ expect(files.length).toBe(23)
const index = files.find((f) => f.includes('main-module'))
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8')
const worker = files.find((f) => f.includes('worker_entry-my-worker'))
@@ -173,6 +173,12 @@ test('self reference url worker', async () => {
).toBe('pong: main\npong: nested\n')
})
+test('self reference url worker in dependency', async () => {
+ await expectWithRetry(() =>
+ page.textContent('.self-reference-url-worker-dep'),
+ ).toBe('pong: main\npong: nested\n')
+})
+
test.runIf(isServe)('sourcemap boundary', async () => {
const response = page.waitForResponse(/my-worker.ts\?worker_file&type=module/)
await page.goto(viteTestUrl)
diff --git a/playground/worker/__tests__/sourcemap-hidden/worker-sourcemap-hidden.spec.ts b/playground/worker/__tests__/sourcemap-hidden/worker-sourcemap-hidden.spec.ts
index 780ce34df6795a..ef50c15564ea70 100644
--- a/playground/worker/__tests__/sourcemap-hidden/worker-sourcemap-hidden.spec.ts
+++ b/playground/worker/__tests__/sourcemap-hidden/worker-sourcemap-hidden.spec.ts
@@ -10,7 +10,7 @@ describe.runIf(isBuild)('build', () => {
const files = fs.readdirSync(assetsDir)
// should have 2 worker chunk
- expect(files.length).toBe(44)
+ expect(files.length).toBe(46)
const index = files.find((f) => f.includes('main-module'))
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8')
const indexSourcemap = getSourceMapUrl(content)
diff --git a/playground/worker/__tests__/sourcemap-inline/worker-sourcemap-inline.spec.ts b/playground/worker/__tests__/sourcemap-inline/worker-sourcemap-inline.spec.ts
index 10933fee2ff7ce..7f806761ae66fc 100644
--- a/playground/worker/__tests__/sourcemap-inline/worker-sourcemap-inline.spec.ts
+++ b/playground/worker/__tests__/sourcemap-inline/worker-sourcemap-inline.spec.ts
@@ -10,7 +10,7 @@ describe.runIf(isBuild)('build', () => {
const files = fs.readdirSync(assetsDir)
// should have 2 worker chunk
- expect(files.length).toBe(22)
+ expect(files.length).toBe(23)
const index = files.find((f) => f.includes('main-module'))
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8')
const indexSourcemap = getSourceMapUrl(content)
diff --git a/playground/worker/__tests__/sourcemap/worker-sourcemap.spec.ts b/playground/worker/__tests__/sourcemap/worker-sourcemap.spec.ts
index 47905cd3d33673..4e3dce9d817a8f 100644
--- a/playground/worker/__tests__/sourcemap/worker-sourcemap.spec.ts
+++ b/playground/worker/__tests__/sourcemap/worker-sourcemap.spec.ts
@@ -9,7 +9,7 @@ describe.runIf(isBuild)('build', () => {
const assetsDir = path.resolve(testDir, 'dist/iife-sourcemap/assets')
const files = fs.readdirSync(assetsDir)
// should have 2 worker chunk
- expect(files.length).toBe(44)
+ expect(files.length).toBe(46)
const index = files.find((f) => f.includes('main-module'))
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8')
const indexSourcemap = getSourceMapUrl(content)
diff --git a/playground/worker/dep-self-reference-url-worker/index.js b/playground/worker/dep-self-reference-url-worker/index.js
new file mode 100644
index 00000000000000..cfb52d9fe55538
--- /dev/null
+++ b/playground/worker/dep-self-reference-url-worker/index.js
@@ -0,0 +1,9 @@
+export function startWorker(handler) {
+ const worker = new Worker(new URL('./worker.js', import.meta.url), {
+ type: 'module',
+ })
+ worker.postMessage('main')
+ worker.addEventListener('message', (e) => {
+ handler(e)
+ })
+}
diff --git a/playground/worker/dep-self-reference-url-worker/package.json b/playground/worker/dep-self-reference-url-worker/package.json
new file mode 100644
index 00000000000000..4f46ee5a6d81d0
--- /dev/null
+++ b/playground/worker/dep-self-reference-url-worker/package.json
@@ -0,0 +1,7 @@
+{
+ "name": "@vitejs/test-dep-self-reference-url-worker",
+ "private": true,
+ "version": "1.0.0",
+ "type": "module",
+ "main": "index.js"
+}
diff --git a/playground/worker/dep-self-reference-url-worker/worker.js b/playground/worker/dep-self-reference-url-worker/worker.js
new file mode 100644
index 00000000000000..908fe9071592f7
--- /dev/null
+++ b/playground/worker/dep-self-reference-url-worker/worker.js
@@ -0,0 +1,14 @@
+// copy of playground/worker/self-reference-url-worker.js
+self.addEventListener('message', (e) => {
+ if (e.data === 'main') {
+ const selfWorker = new Worker(new URL('./worker.js', import.meta.url), {
+ type: 'module',
+ })
+ selfWorker.postMessage('nested')
+ selfWorker.addEventListener('message', (e) => {
+ self.postMessage(e.data)
+ })
+ }
+
+ self.postMessage(`pong: ${e.data}`)
+})
diff --git a/playground/worker/index.html b/playground/worker/index.html
index c1944046af6998..d6b3806b3293ae 100644
--- a/playground/worker/index.html
+++ b/playground/worker/index.html
@@ -164,6 +164,11 @@
+
+ .self-reference-url-worker-dep
+
+
+
new Worker(new URL('../deeply-nested-worker.js', import.meta.url), { type:
'module' })
diff --git a/playground/worker/package.json b/playground/worker/package.json
index 90d4068a580f4a..e9b2dd7876e027 100644
--- a/playground/worker/package.json
+++ b/playground/worker/package.json
@@ -28,6 +28,7 @@
"debug": "node --inspect-brk ../../packages/vite/bin/vite"
},
"dependencies": {
+ "@vitejs/test-dep-self-reference-url-worker": "file:./dep-self-reference-url-worker",
"@vitejs/test-dep-to-optimize": "file:./dep-to-optimize"
}
}
diff --git a/playground/worker/vite.config-iife.js b/playground/worker/vite.config-iife.js
index 3d6d0de8a170e5..7e3cb6f68aa7f7 100644
--- a/playground/worker/vite.config-iife.js
+++ b/playground/worker/vite.config-iife.js
@@ -51,4 +51,7 @@ export default defineConfig({
},
],
cacheDir: 'node_modules/.vite-iife',
+ optimizeDeps: {
+ exclude: ['@vitejs/test-dep-self-reference-url-worker'],
+ },
})
diff --git a/playground/worker/worker/main-module.js b/playground/worker/worker/main-module.js
index c405adfc04815e..a59bc903800b12 100644
--- a/playground/worker/worker/main-module.js
+++ b/playground/worker/worker/main-module.js
@@ -1,3 +1,4 @@
+import * as depSelfReferenceUrlWorker from '@vitejs/test-dep-self-reference-url-worker'
import myWorker from '../my-worker.ts?worker'
import InlineWorker from '../my-worker.ts?worker&inline'
import InlineSharedWorker from '../my-inline-shared-worker?sharedworker&inline'
@@ -177,3 +178,8 @@ selfReferenceUrlWorker.addEventListener('message', (e) => {
document.querySelector('.self-reference-url-worker').textContent +=
`${e.data}\n`
})
+
+depSelfReferenceUrlWorker.startWorker((e) => {
+ document.querySelector('.self-reference-url-worker-dep').textContent +=
+ `${e.data}\n`
+})
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 733a3fb91d12b2..c3cb474b90fa00 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1555,10 +1555,15 @@ importers:
playground/worker:
dependencies:
+ '@vitejs/test-dep-self-reference-url-worker':
+ specifier: file:./dep-self-reference-url-worker
+ version: file:playground/worker/dep-self-reference-url-worker
'@vitejs/test-dep-to-optimize':
specifier: file:./dep-to-optimize
version: file:playground/worker/dep-to-optimize
+ playground/worker/dep-self-reference-url-worker: {}
+
playground/worker/dep-to-optimize: {}
packages:
@@ -3471,6 +3476,9 @@ packages:
'@vitejs/test-dep-relative-to-main@file:playground/optimize-deps/dep-relative-to-main':
resolution: {directory: playground/optimize-deps/dep-relative-to-main, type: directory}
+ '@vitejs/test-dep-self-reference-url-worker@file:playground/worker/dep-self-reference-url-worker':
+ resolution: {directory: playground/worker/dep-self-reference-url-worker, type: directory}
+
'@vitejs/test-dep-that-imports@file:playground/external/dep-that-imports':
resolution: {directory: playground/external/dep-that-imports, type: directory}
@@ -8862,6 +8870,8 @@ snapshots:
'@vitejs/test-dep-relative-to-main@file:playground/optimize-deps/dep-relative-to-main': {}
+ '@vitejs/test-dep-self-reference-url-worker@file:playground/worker/dep-self-reference-url-worker': {}
+
'@vitejs/test-dep-that-imports@file:playground/external/dep-that-imports(typescript@5.5.3)':
dependencies:
slash3: slash@3.0.0