diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 162a7c2f0f58da..25aa49d38a966a 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -351,14 +351,19 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { ) } } - if (!isWorker) { - const workerMap = workerCache.get(config)! - workerMap.assets.forEach((asset) => { - this.emitFile(asset) - workerMap.assets.delete(asset.fileName!) - }) - } return result() + }, + + generateBundle(opts) { + // @ts-ignore asset emits are skipped in legacy bundle + if (opts.__vite_skip_asset_emit__ || isWorker) { + return + } + const workerMap = workerCache.get(config)! + workerMap.assets.forEach((asset) => { + this.emitFile(asset) + workerMap.assets.delete(asset.fileName!) + }) } } } diff --git a/playground/legacy/__tests__/legacy.spec.ts b/playground/legacy/__tests__/legacy.spec.ts index ac9b3d524cf9d5..2a2a0698d65b04 100644 --- a/playground/legacy/__tests__/legacy.spec.ts +++ b/playground/legacy/__tests__/legacy.spec.ts @@ -8,22 +8,32 @@ import { untilUpdated } from '~utils' +test('should load the worker', async () => { + await untilUpdated(() => page.textContent('.worker-message'), 'module', true) +}) + test('should work', async () => { - expect(await page.textContent('#app')).toMatch('Hello') + await untilUpdated(() => page.textContent('#app'), 'Hello', true) }) test('import.meta.env.LEGACY', async () => { - expect(await page.textContent('#env')).toMatch(isBuild ? 'true' : 'false') + await untilUpdated( + () => page.textContent('#env'), + isBuild ? 'true' : 'false', + true + ) }) // https://github.com/vitejs/vite/issues/3400 test('transpiles down iterators correctly', async () => { - expect(await page.textContent('#iterators')).toMatch('hello') + await untilUpdated(() => page.textContent('#iterators'), 'hello', true) }) test('wraps with iife', async () => { - expect(await page.textContent('#babel-helpers')).toMatch( - 'exposed babel helpers: false' + await untilUpdated( + () => page.textContent('#babel-helpers'), + 'exposed babel helpers: false', + true ) }) diff --git a/playground/legacy/index.html b/playground/legacy/index.html index e2bf74c7d4efaa..42b31af83bacdf 100644 --- a/playground/legacy/index.html +++ b/playground/legacy/index.html @@ -6,5 +6,7 @@

+

## worker message:

+
diff --git a/playground/legacy/main.js b/playground/legacy/main.js index 3a9714cadfea72..84b731227425eb 100644 --- a/playground/legacy/main.js +++ b/playground/legacy/main.js @@ -1,5 +1,6 @@ import './style.css' import viteSvgPath from './vite.svg' +import MyWorker from './worker?worker' async function run() { const { fn } = await import('./async.js') @@ -56,3 +57,9 @@ text('#asset-path', viteSvgPath) function text(el, text) { document.querySelector(el).textContent = text } + +const worker = new MyWorker() +worker.postMessage('ping') +worker.addEventListener('message', (ev) => { + text('.worker-message', JSON.stringify(ev.data)) +}) diff --git a/playground/legacy/module.js b/playground/legacy/module.js new file mode 100644 index 00000000000000..8080ab24c9a7f6 --- /dev/null +++ b/playground/legacy/module.js @@ -0,0 +1 @@ +export const module = 'module' diff --git a/playground/legacy/worker.js b/playground/legacy/worker.js new file mode 100644 index 00000000000000..d19cc6c52b9613 --- /dev/null +++ b/playground/legacy/worker.js @@ -0,0 +1,5 @@ +import { module } from './module' + +self.onmessage = () => { + self.postMessage(module) +}