/
sourcemap-worker.spec.ts
131 lines (121 loc) · 4.56 KB
/
sourcemap-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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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/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(25)
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)
expect(files).toContainEqual(expect.stringMatching(/^index\.\w+\.js\.map$/))
expect(files).toContainEqual(
expect.stringMatching(/^my-worker\.\w+\.js\.map$/)
)
expect(files).toContainEqual(
expect.stringMatching(/^my-shared-worker\.\w+\.js\.map$/)
)
expect(files).toContainEqual(
expect.stringMatching(/^possible-ts-output-worker\.\w+\.js\.map$/)
)
expect(files).toContainEqual(
expect.stringMatching(/^worker-nested-worker\.\w+\.js\.map$/)
)
expect(files).toContainEqual(
expect.stringMatching(/^sub-worker\.\w+\.js\.map$/)
)
// sourcemap should exist and have a data URL
expect(indexSourcemap).toMatch(/^main-module\.\w+\.js\.map$/)
expect(workerSourcemap).toMatch(/^my-worker\.\w+\.js\.map$/)
expect(sharedWorkerSourcemap).toMatch(/^my-shared-worker\.\w+\.js\.map$/)
expect(possibleTsOutputWorkerSourcemap).toMatch(
/^possible-ts-output-worker\.\w+\.js\.map$/
)
expect(workerNestedWorkerSourcemap).toMatch(
/^worker-nested-worker\.\w+\.js\.map$/
)
expect(subWorkerSourcemap).toMatch(/^sub-worker\.\w+\.js\.map$/)
// 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/assets/my-worker`)
expect(content).toMatch(`new Worker("data:application/javascript;base64`)
expect(content).toMatch(
`new Worker("/iife-sourcemap/assets/possible-ts-output-worker`
)
expect(content).toMatch(
`new Worker("/iife-sourcemap/assets/worker-nested-worker`
)
expect(content).toMatch(
`new SharedWorker("/iife-sourcemap/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/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
}