forked from vitejs/vite
/
sourcemap-inline-worker.spec.ts
112 lines (103 loc) · 3.85 KB
/
sourcemap-inline-worker.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import fs from 'fs'
import path from 'path'
import { untilUpdated, isBuild, testDir } from '../../../testUtils'
import { Page } from 'playwright-chromium'
if (isBuild) {
const assetsDir = path.resolve(testDir, 'dist/iife-sourcemap-inline/assets')
// assert correct files
test('sourcemap generation for web workers', async () => {
const files = fs.readdirSync(assetsDir)
// should have 2 worker chunk
expect(files.length).toBe(12)
const index = files.find((f) => f.includes('main-module'))
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8')
const indexSourcemap = getSourceMapUrl(content)
const worker = files.find((f) => /^my-worker\.\w+\.js$/.test(f))
const workerContent = fs.readFileSync(
path.resolve(assetsDir, worker),
'utf-8'
)
const workerSourcemap = getSourceMapUrl(workerContent)
const sharedWorker = files.find((f) =>
/^my-shared-worker\.\w+\.js$/.test(f)
)
const sharedWorkerContent = fs.readFileSync(
path.resolve(assetsDir, sharedWorker),
'utf-8'
)
const sharedWorkerSourcemap = getSourceMapUrl(sharedWorkerContent)
const possibleTsOutputWorker = files.find((f) =>
/^possible-ts-output-worker\.\w+\.js$/.test(f)
)
const possibleTsOutputWorkerContent = fs.readFileSync(
path.resolve(assetsDir, possibleTsOutputWorker),
'utf-8'
)
const possibleTsOutputWorkerSourcemap = getSourceMapUrl(
possibleTsOutputWorkerContent
)
const workerNestedWorker = files.find((f) =>
/^worker-nested-worker\.\w+\.js$/.test(f)
)
const workerNestedWorkerContent = fs.readFileSync(
path.resolve(assetsDir, workerNestedWorker),
'utf-8'
)
const workerNestedWorkerSourcemap = getSourceMapUrl(
workerNestedWorkerContent
)
const subWorker = files.find((f) => /^sub-worker\.\w+\.js$/.test(f))
const subWorkerContent = fs.readFileSync(
path.resolve(assetsDir, subWorker),
'utf-8'
)
const subWorkerSourcemap = getSourceMapUrl(subWorkerContent)
// sourcemap should exist and have a data URL
expect(indexSourcemap).toMatch(/^data:/)
expect(workerSourcemap).toMatch(/^data:/)
expect(sharedWorkerSourcemap).toMatch(/^data:/)
expect(possibleTsOutputWorkerSourcemap).toMatch(/^data:/)
expect(workerNestedWorkerSourcemap).toMatch(/^data:/)
expect(subWorkerSourcemap).toMatch(/^data:/)
// worker should have all imports resolved and no exports
expect(workerContent).not.toMatch(`import`)
expect(workerContent).not.toMatch(`export`)
// shared worker should have all imports resolved and no exports
expect(sharedWorkerContent).not.toMatch(`import`)
expect(sharedWorkerContent).not.toMatch(`export`)
// chunk
expect(content).toMatch(
`new Worker("/iife-sourcemap-inline/assets/my-worker`
)
expect(content).toMatch(`new Worker("data:application/javascript;base64`)
expect(content).toMatch(
`new Worker("/iife-sourcemap-inline/assets/possible-ts-output-worker`
)
expect(content).toMatch(
`new Worker("/iife-sourcemap-inline/assets/worker-nested-worker`
)
expect(content).toMatch(
`new SharedWorker("/iife-sourcemap-inline/assets/my-shared-worker`
)
// inlined
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
expect(content).toMatch(`window.Blob`)
expect(workerNestedWorkerContent).toMatch(
`new Worker("/iife-sourcemap-inline/assets/sub-worker`
)
})
} else {
// Workaround so that testing serve does not emit
// "Your test suite must contain at least one test"
test('true', () => {
expect(true).toBe(true)
})
}
function getSourceMapUrl(code: string): string {
const regex = /\/\/[#@]\s(?:source(?:Mapping)?URL)=\s*(\S+)/g
const results = regex.exec(code)
if (results && results.length >= 2) {
return results[1]
}
return null
}