From 3c0a6091fe96044e9dd84fbe5db3343339a88986 Mon Sep 17 00:00:00 2001 From: yoho <907415276@qq.com> Date: Thu, 3 Mar 2022 21:46:25 +0800 Subject: [PATCH] fix: execute classic worker in dev mode (#7099) --- .prettierignore | 1 + .../worker/__tests__/worker.spec.ts | 7 +- packages/playground/worker/classic-worker.js | 29 ++++++ packages/playground/worker/index.html | 23 +++-- .../worker/newUrl/classic-shared-worker.js | 6 ++ .../worker/newUrl/classic-worker.js | 5 ++ packages/playground/worker/public/classic.js | 1 + packages/vite/src/node/importGlob.ts | 52 ++++------- .../src/node/plugins/workerImportMetaUrl.ts | 88 +++++++++++++++++-- packages/vite/src/node/utils.ts | 2 + 10 files changed, 164 insertions(+), 50 deletions(-) create mode 100644 packages/playground/worker/classic-worker.js create mode 100644 packages/playground/worker/newUrl/classic-shared-worker.js create mode 100644 packages/playground/worker/newUrl/classic-worker.js create mode 100644 packages/playground/worker/public/classic.js diff --git a/.prettierignore b/.prettierignore index 1692b9d26..c624a3a21 100644 --- a/.prettierignore +++ b/.prettierignore @@ -10,3 +10,4 @@ pnpm-lock.yaml pnpm-workspace.yaml packages/playground/tsconfig-json-load-error/has-error/tsconfig.json packages/playground/html/invalid.html +packages/playground/worker/classic-worker.js diff --git a/packages/playground/worker/__tests__/worker.spec.ts b/packages/playground/worker/__tests__/worker.spec.ts index 5992a37f9..1c33e6ec6 100644 --- a/packages/playground/worker/__tests__/worker.spec.ts +++ b/packages/playground/worker/__tests__/worker.spec.ts @@ -56,7 +56,7 @@ if (isBuild) { // assert correct files test('inlined code generation', async () => { const files = fs.readdirSync(assetsDir) - expect(files.length).toBe(6) + expect(files.length).toBe(8) const index = files.find((f) => f.includes('index')) const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8') const worker = files.find((f) => f.includes('my-worker')) @@ -88,3 +88,8 @@ if (isBuild) { }) }) } + +test('classic worker is run', async () => { + expect(await page.textContent('.classic-worker')).toMatch('A classic') + expect(await page.textContent('.classic-shared-worker')).toMatch('A classic') +}) diff --git a/packages/playground/worker/classic-worker.js b/packages/playground/worker/classic-worker.js new file mode 100644 index 000000000..bb6f9c3f4 --- /dev/null +++ b/packages/playground/worker/classic-worker.js @@ -0,0 +1,29 @@ +// prettier-ignore +function text(el, text) { + document.querySelector(el).textContent = text +} + +const classicWorker = new Worker( + new URL('./newUrl/classic-worker.js', import.meta.url) /* , */ , + // test comment + +) + +classicWorker.addEventListener('message', ({ data }) => { + text('.classic-worker', data) +}) +classicWorker.postMessage('ping') + +const classicSharedWorker = new SharedWorker( + new URL('./newUrl/classic-shared-worker.js', import.meta.url), + { + type: 'classic' + } +) +classicSharedWorker.port.addEventListener('message', (ev) => { + text( + '.classic-shared-worker', + ev.data + ) +}) +classicSharedWorker.port.start() diff --git a/packages/playground/worker/index.html b/packages/playground/worker/index.html index be0b3becb..102074537 100644 --- a/packages/playground/worker/index.html +++ b/packages/playground/worker/index.html @@ -20,18 +20,25 @@ 0 -
new Worker(new Url('path', import.meta.url))
+new Worker(new Url('path', import.meta.url), { type: 'module' })
-new SharedWorker(new Url('path', import.meta.url))
+new SharedWorker(new Url('path', import.meta.url), { type: 'module' })
+new Worker(new Url('path', import.meta.url))
+ + +new Worker(new Url('path', import.meta.url), { type: 'classic' })
+ +