From 4f6e9ba1c79da18c378a0da3f8a6a65152dd1ae3 Mon Sep 17 00:00:00 2001 From: sun0day Date: Mon, 4 Sep 2023 14:43:12 +0800 Subject: [PATCH 1/2] test(worker): sourcemap boundary --- .../worker/__tests__/iife/iife-worker.spec.ts | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/playground/worker/__tests__/iife/iife-worker.spec.ts b/playground/worker/__tests__/iife/iife-worker.spec.ts index 737c4b1f22361c..98d2bd080eae59 100644 --- a/playground/worker/__tests__/iife/iife-worker.spec.ts +++ b/playground/worker/__tests__/iife/iife-worker.spec.ts @@ -1,7 +1,15 @@ import fs from 'node:fs' import path from 'node:path' import { describe, expect, test } from 'vitest' -import { isBuild, page, readManifest, testDir, untilUpdated } from '~utils' +import { + isBuild, + isServe, + page, + readManifest, + testDir, + untilUpdated, + viteTestUrl, +} from '~utils' test('normal', async () => { await untilUpdated(() => page.textContent('.pong'), 'pong') @@ -143,3 +151,22 @@ test('import.meta.glob eager in worker', async () => { '["', ) }) + +test.runIf(isServe)('sourcemap boundary', async () => { + const response = page.waitForResponse(/my-worker.ts\?type=module&worker_file/) + await page.goto(viteTestUrl) + const content = await (await response).text() + const { mappings } = decodeSourceMapUrl(content) + expect(mappings.startsWith(';')).toBeTruthy() + expect(mappings.endsWith(';')).toBeFalsy() +}) + +function decodeSourceMapUrl(content: string) { + return JSON.parse( + atob( + content.match( + /\/\/[#@]\ssourceMappingURL=\s*data:application\/json;base64,(\S+)/, + )?.[1], + ), + ) +} From fbe95a0dada706abd7fac61447c3e8962e3dcd6f Mon Sep 17 00:00:00 2001 From: sun0day Date: Mon, 4 Sep 2023 23:21:48 +0800 Subject: [PATCH 2/2] refactor: use Buffer.from --- playground/worker/__tests__/iife/iife-worker.spec.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/playground/worker/__tests__/iife/iife-worker.spec.ts b/playground/worker/__tests__/iife/iife-worker.spec.ts index 98d2bd080eae59..96aaae6ef22e66 100644 --- a/playground/worker/__tests__/iife/iife-worker.spec.ts +++ b/playground/worker/__tests__/iife/iife-worker.spec.ts @@ -163,10 +163,11 @@ test.runIf(isServe)('sourcemap boundary', async () => { function decodeSourceMapUrl(content: string) { return JSON.parse( - atob( + Buffer.from( content.match( /\/\/[#@]\ssourceMappingURL=\s*data:application\/json;base64,(\S+)/, )?.[1], - ), + 'base64', + ).toString(), ) }