forked from vitejs/vite
/
sourcemap-inline.spec.ts
65 lines (58 loc) · 2.25 KB
/
sourcemap-inline.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
import fs from 'fs'
import path from 'path'
import { untilUpdated, isBuild, testDir } from '../../testUtils'
import { Page } from 'playwright-chromium'
if (isBuild) {
// assert correct files
test('inlined sourcemap generation for web workers', async () => {
const assetsDir = path.resolve(testDir, 'dist/assets')
const files = fs.readdirSync(assetsDir)
// should have 2 worker chunk
expect(files.length).toBe(3)
const index = files.find((f) => f.includes('index'))
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8')
const indexSourcemap = getSourceMapUrl(content)
const worker = files.find((f) => f.includes('my-worker'))
const workerContent = fs.readFileSync(
path.resolve(assetsDir, worker),
'utf-8'
)
const workerSourcemap = getSourceMapUrl(workerContent)
const sharedWorker = files.find((f) => f.includes('my-shared-worker'))
const sharedWorkerContent = fs.readFileSync(
path.resolve(assetsDir, sharedWorker),
'utf-8'
)
const sharedWorkerSourcemap = getSourceMapUrl(sharedWorkerContent)
// sourcemap should exist and have a data URL
expect(indexSourcemap).toMatch(/^data:/)
expect(workerSourcemap).toMatch(/^data:/)
expect(sharedWorkerSourcemap).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("/assets`)
expect(content).toMatch(`new SharedWorker("/assets`)
// inlined
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
expect(content).toMatch(`window.Blob`)
})
} 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
}