forked from vitejs/vite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sourcemap-hidden-worker.spec.ts
123 lines (112 loc) · 4.19 KB
/
sourcemap-hidden-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
import fs from 'fs'
import path from 'path'
import { isBuild, testDir } from '~utils'
describe.runIf(isBuild)('build', () => {
// assert correct files
test('sourcemap generation for web workers', async () => {
const assetsDir = path.resolve(testDir, 'dist/iife-sourcemap-hidden/assets')
const files = fs.readdirSync(assetsDir)
// should have 2 worker chunk
expect(files.length).toBe(30)
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).toBe(null)
expect(workerSourcemap).toBe(null)
expect(sharedWorkerSourcemap).toBe(null)
expect(possibleTsOutputWorkerSourcemap).toBe(null)
expect(workerNestedWorkerSourcemap).toBe(null)
expect(subWorkerSourcemap).toBe(null)
// 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-hidden/assets/my-worker`
)
expect(content).toMatch(`new Worker("data:application/javascript;base64`)
expect(content).toMatch(
`new Worker("/iife-sourcemap-hidden/assets/possible-ts-output-worker`
)
expect(content).toMatch(
`new Worker("/iife-sourcemap-hidden/assets/worker-nested-worker`
)
expect(content).toMatch(
`new SharedWorker("/iife-sourcemap-hidden/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-hidden/assets/sub-worker`
)
})
})
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
}