From fa6b4949e707f25fa8eb5203f831c891ecbc2ec6 Mon Sep 17 00:00:00 2001 From: yoho Date: Tue, 2 Aug 2022 22:03:41 +0800 Subject: [PATCH 1/7] test: legacy plugin no emit worker --- playground/legacy/__tests__/legacy.spec.ts | 6 ++++++ playground/legacy/index.html | 3 +++ playground/legacy/main.js | 6 ++++++ playground/legacy/module.js | 1 + playground/legacy/vite.config.js | 2 +- playground/legacy/worker.js | 3 +++ 6 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 playground/legacy/module.js create mode 100644 playground/legacy/worker.js diff --git a/playground/legacy/__tests__/legacy.spec.ts b/playground/legacy/__tests__/legacy.spec.ts index e9375e051efa6d..a9d25423f4407d 100644 --- a/playground/legacy/__tests__/legacy.spec.ts +++ b/playground/legacy/__tests__/legacy.spec.ts @@ -16,6 +16,12 @@ test('import.meta.env.LEGACY', async () => { expect(await page.textContent('#env')).toMatch(isBuild ? 'true' : 'false') }) +test('should load the worker', async () => { + expect(await page.textContent('.worker-message')).toMatchInlineSnapshot( + '"\\"module\\""' + ) +}) + // https://github.com/vitejs/vite/issues/3400 test('transpiles down iterators correctly', async () => { expect(await page.textContent('#iterators')).toMatch('hello') diff --git a/playground/legacy/index.html b/playground/legacy/index.html index cbf6242fad756b..1a271cc5ba5260 100644 --- a/playground/legacy/index.html +++ b/playground/legacy/index.html @@ -6,4 +6,7 @@

+ +

## worker message:

+
diff --git a/playground/legacy/main.js b/playground/legacy/main.js index 24959b129f28f2..06c27a68d8cc9b 100644 --- a/playground/legacy/main.js +++ b/playground/legacy/main.js @@ -1,5 +1,6 @@ import './style.css' import './vite.svg' +import MyWorker from './worker?worker' async function run() { const { fn } = await import('./async.js') @@ -54,3 +55,8 @@ document function text(el, text) { document.querySelector(el).textContent = text } + +const worker = new MyWorker() +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/vite.config.js b/playground/legacy/vite.config.js index f793980f365887..3cdf5a4e7e7d2e 100644 --- a/playground/legacy/vite.config.js +++ b/playground/legacy/vite.config.js @@ -6,7 +6,7 @@ module.exports = { base: './', plugins: [ legacy({ - targets: 'IE 11', + targets: 'IE 11, CHROME 52', modernPolyfills: true }) ], diff --git a/playground/legacy/worker.js b/playground/legacy/worker.js new file mode 100644 index 00000000000000..ef32ea1e894a2b --- /dev/null +++ b/playground/legacy/worker.js @@ -0,0 +1,3 @@ +import { module } from './module' + +self.postMessage(module) From 77f1aa12d97423e90c1cdfb47dbd14eb1194fd47 Mon Sep 17 00:00:00 2001 From: yoho Date: Tue, 2 Aug 2022 23:59:08 +0800 Subject: [PATCH 2/7] fix: emit worker asset in generateBundle hook and avoid emit worker asset when `__vite_skip_asset_emit__` is true --- packages/vite/src/node/plugins/worker.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index fe2f209b915b60..97203dab3eb62f 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -308,7 +308,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { } }, - renderChunk(code, chunk) { + renderChunk(code, chunk, opts) { let s: MagicString const result = () => { return ( @@ -356,14 +356,19 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { return result() } } - 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!) + }) } } } From 12f3b20ed50b42fb373b46e4c08b70abe83e2e47 Mon Sep 17 00:00:00 2001 From: yoho Date: Wed, 3 Aug 2022 00:06:26 +0800 Subject: [PATCH 3/7] chore: update --- packages/vite/src/node/plugins/worker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 97203dab3eb62f..0733b980740b5e 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -308,7 +308,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { } }, - renderChunk(code, chunk, opts) { + renderChunk(code, chunk) { let s: MagicString const result = () => { return ( From aa1fdda6e2a74ccdc33b1514e9f18f7b73c174de Mon Sep 17 00:00:00 2001 From: yoho Date: Wed, 3 Aug 2022 00:36:27 +0800 Subject: [PATCH 4/7] fix: targats --- playground/legacy/vite.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playground/legacy/vite.config.js b/playground/legacy/vite.config.js index 3cdf5a4e7e7d2e..f793980f365887 100644 --- a/playground/legacy/vite.config.js +++ b/playground/legacy/vite.config.js @@ -6,7 +6,7 @@ module.exports = { base: './', plugins: [ legacy({ - targets: 'IE 11, CHROME 52', + targets: 'IE 11', modernPolyfills: true }) ], From d73ff07712f98729eb1582250df57b737a5569a2 Mon Sep 17 00:00:00 2001 From: yoho Date: Wed, 3 Aug 2022 01:09:24 +0800 Subject: [PATCH 5/7] chore: untilUpdated --- playground/legacy/__tests__/legacy.spec.ts | 22 ++++++++++------------ playground/legacy/main.js | 1 + playground/legacy/worker.js | 4 +++- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/playground/legacy/__tests__/legacy.spec.ts b/playground/legacy/__tests__/legacy.spec.ts index a9d25423f4407d..d5421ebb6cfa79 100644 --- a/playground/legacy/__tests__/legacy.spec.ts +++ b/playground/legacy/__tests__/legacy.spec.ts @@ -8,27 +8,26 @@ import { untilUpdated } from '~utils' -test('should work', async () => { - expect(await page.textContent('#app')).toMatch('Hello') +test('should load the worker', async () => { + await untilUpdated(() => page.textContent('.worker-message'), 'module') }) -test('import.meta.env.LEGACY', async () => { - expect(await page.textContent('#env')).toMatch(isBuild ? 'true' : 'false') +test('should work', async () => { + await untilUpdated(() => page.textContent('#app'), 'Hello') }) -test('should load the worker', async () => { - expect(await page.textContent('.worker-message')).toMatchInlineSnapshot( - '"\\"module\\""' - ) +test('import.meta.env.LEGACY', async () => { + await untilUpdated(() => page.textContent('#env'), isBuild ? 'true' : 'false') }) // 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') }) test('wraps with iife', async () => { - expect(await page.textContent('#babel-helpers')).toMatch( + await untilUpdated( + () => page.textContent('#babel-helpers'), 'exposed babel helpers: false' ) }) @@ -54,8 +53,7 @@ test('generates assets', async () => { 'immutable-chunk: 404', 'immutable-chunk-legacy: 404', 'polyfills-legacy: 404' - ].join('\n'), - true + ].join('\n') ) }) diff --git a/playground/legacy/main.js b/playground/legacy/main.js index 06c27a68d8cc9b..5d5c3914eac43c 100644 --- a/playground/legacy/main.js +++ b/playground/legacy/main.js @@ -57,6 +57,7 @@ function text(el, text) { } const worker = new MyWorker() +worker.postMessage('ping') worker.addEventListener('message', (ev) => { text('.worker-message', JSON.stringify(ev.data)) }) diff --git a/playground/legacy/worker.js b/playground/legacy/worker.js index ef32ea1e894a2b..d19cc6c52b9613 100644 --- a/playground/legacy/worker.js +++ b/playground/legacy/worker.js @@ -1,3 +1,5 @@ import { module } from './module' -self.postMessage(module) +self.onmessage = () => { + self.postMessage(module) +} From aa0b77fa4144ee0c59c731ae1726a504b2a24c6c Mon Sep 17 00:00:00 2001 From: yoho Date: Wed, 10 Aug 2022 13:51:19 +0800 Subject: [PATCH 6/7] test: feat run in build --- playground/legacy/__tests__/legacy.spec.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/playground/legacy/__tests__/legacy.spec.ts b/playground/legacy/__tests__/legacy.spec.ts index d5421ebb6cfa79..23b79bcc9c3067 100644 --- a/playground/legacy/__tests__/legacy.spec.ts +++ b/playground/legacy/__tests__/legacy.spec.ts @@ -53,7 +53,8 @@ test('generates assets', async () => { 'immutable-chunk: 404', 'immutable-chunk-legacy: 404', 'polyfills-legacy: 404' - ].join('\n') + ].join('\n'), + true ) }) From 1b2f8511009039576e43a4903b7e4d89b8096601 Mon Sep 17 00:00:00 2001 From: yoho Date: Wed, 10 Aug 2022 16:39:55 +0800 Subject: [PATCH 7/7] feat: true --- playground/legacy/__tests__/legacy.spec.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/playground/legacy/__tests__/legacy.spec.ts b/playground/legacy/__tests__/legacy.spec.ts index 9035ecade725e3..2a2a0698d65b04 100644 --- a/playground/legacy/__tests__/legacy.spec.ts +++ b/playground/legacy/__tests__/legacy.spec.ts @@ -9,26 +9,31 @@ import { } from '~utils' test('should load the worker', async () => { - await untilUpdated(() => page.textContent('.worker-message'), 'module') + await untilUpdated(() => page.textContent('.worker-message'), 'module', true) }) test('should work', async () => { - await untilUpdated(() => page.textContent('#app'), 'Hello') + await untilUpdated(() => page.textContent('#app'), 'Hello', true) }) test('import.meta.env.LEGACY', async () => { - await untilUpdated(() => page.textContent('#env'), 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 () => { - await untilUpdated(() => page.textContent('#iterators'), 'hello') + await untilUpdated(() => page.textContent('#iterators'), 'hello', true) }) test('wraps with iife', async () => { await untilUpdated( () => page.textContent('#babel-helpers'), - 'exposed babel helpers: false' + 'exposed babel helpers: false', + true ) })