-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
js-sourcemap.spec.ts
159 lines (146 loc) · 4.41 KB
/
js-sourcemap.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { URL } from 'node:url'
import { describe, expect, test } from 'vitest'
import { mapFileCommentRegex } from 'convert-source-map'
import { commentSourceMap } from '../foo-with-sourcemap-plugin'
import {
extractSourcemap,
findAssetFile,
formatSourcemapForSnapshot,
isBuild,
page,
serverLogs,
} from '~utils'
if (!isBuild) {
test('js', async () => {
const res = await page.request.get(new URL('./foo.js', page.url()).href)
const js = await res.text()
const map = extractSourcemap(js)
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
{
"mappings": "AAAA,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;",
"sources": [
"foo.js",
],
"sourcesContent": [
"export const foo = 'foo'
",
],
"version": 3,
}
`)
})
test('plugin return sourcemap with `sources: [""]`', async () => {
const res = await page.request.get(new URL('./zoo.js', page.url()).href)
const js = await res.text()
expect(js).toContain('// add comment')
const map = extractSourcemap(js)
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
{
"mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;",
"sources": [
"zoo.js",
],
"sourcesContent": [
"export const zoo = 'zoo'
",
],
"version": 3,
}
`)
})
test('js with inline sourcemap injected by a plugin', async () => {
const res = await page.request.get(
new URL('./foo-with-sourcemap.js', page.url()).href,
)
const js = await res.text()
expect(js).toContain(commentSourceMap)
const sourcemapComments = js.match(mapFileCommentRegex).length
expect(sourcemapComments).toBe(1)
const map = extractSourcemap(js)
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
{
"mappings": "AAAA,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG",
"sources": [
"",
],
"version": 3,
}
`)
})
test('ts', async () => {
const res = await page.request.get(new URL('./bar.ts', page.url()).href)
const js = await res.text()
const map = extractSourcemap(js)
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
{
"mappings": "AAAO,aAAM,MAAM;",
"sources": [
"bar.ts",
],
"sourcesContent": [
"export const bar = 'bar'
",
],
"version": 3,
}
`)
})
test('multiline import', async () => {
const res = await page.request.get(
new URL('./with-multiline-import.ts', page.url()).href,
)
const multi = await res.text()
const map = extractSourcemap(multi)
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
{
"mappings": "AACA;AAAA,EACE;AAAA,OACK;AAEP,QAAQ,IAAI,yBAAyB,GAAG;",
"sources": [
"with-multiline-import.ts",
],
"sourcesContent": [
"// prettier-ignore
import {
foo
} from '@vitejs/test-importee-pkg'
console.log('with-multiline-import', foo)
",
],
"version": 3,
}
`)
})
test('should not output missing source file warning', () => {
serverLogs.forEach((log) => {
expect(log).not.toMatch(/Sourcemap for .+ points to missing source files/)
})
})
}
describe.runIf(isBuild)('build tests', () => {
test('should not output sourcemap warning (#4939)', () => {
serverLogs.forEach((log) => {
expect(log).not.toMatch('Sourcemap is likely to be incorrect')
})
})
test('sourcemap is correct when preload information is injected', async () => {
const map = findAssetFile(/after-preload-dynamic.*\.js\.map/)
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
{
"mappings": ";;;;;;i3BAAA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB",
"sources": [
"../../after-preload-dynamic.js",
],
"sourcesContent": [
"import('./dynamic/dynamic-foo')
console.log('after preload dynamic')
",
],
"version": 3,
}
`)
// verify sourcemap comment is preserved at the last line
const js = findAssetFile(/after-preload-dynamic.*\.js$/)
expect(js).toMatch(
/\n\/\/# sourceMappingURL=after-preload-dynamic.*\.js\.map\n$/,
)
})
})