/
previous-map.test.ts
329 lines (284 loc) · 8.11 KB
/
previous-map.test.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import { removeSync, outputFileSync } from 'fs-extra'
import { SourceMapConsumer } from 'source-map-js'
import { pathToFileURL } from 'url'
import { existsSync } from 'fs'
import { join } from 'path'
import { parse } from '../lib/postcss.js'
let dir = join(__dirname, 'prevmap-fixtures')
let mapObj = {
version: 3,
file: null,
sources: [],
names: [],
mappings: ''
}
let map = JSON.stringify(mapObj)
afterEach(() => {
if (existsSync(dir)) removeSync(dir)
})
it('misses property if no map', () => {
expect(parse('a{}').source?.input.map).not.toBeDefined()
})
it('creates property if map present', () => {
let root = parse('a{}', { map: { prev: map } })
expect(root.source?.input.map.text).toEqual(map)
})
it('returns consumer', () => {
let obj = parse('a{}', { map: { prev: map } }).source?.input.map.consumer()
expect(obj instanceof SourceMapConsumer).toBe(true)
})
it('sets annotation property', () => {
let mapOpts = { map: { prev: map } }
let root1 = parse('a{}', mapOpts)
expect(root1.source?.input.map.annotation).not.toBeDefined()
let root2 = parse('a{}/*# sourceMappingURL=a.css.map */', mapOpts)
expect(root2.source?.input.map.annotation).toEqual('a.css.map')
})
it('checks previous sources content', () => {
let map2: any = {
version: 3,
file: 'b',
sources: ['a'],
names: [],
mappings: ''
}
let opts = { map: { prev: map2 } }
expect(parse('a{}', opts).source?.input.map.withContent()).toBe(false)
map2.sourcesContent = ['a{}']
expect(parse('a{}', opts).source?.input.map.withContent()).toBe(true)
})
it('decodes base64 maps', () => {
let b64 = Buffer.from(map).toString('base64')
let css =
'a{}\n' + `/*# sourceMappingURL=data:application/json;base64,${b64} */`
expect(parse(css).source?.input.map.text).toEqual(map)
})
it('decodes base64 UTF-8 maps', () => {
let b64 = Buffer.from(map).toString('base64')
let css =
'a{}\n/*# sourceMappingURL=data:application/json;' +
'charset=utf-8;base64,' +
b64 +
' */'
expect(parse(css).source?.input.map.text).toEqual(map)
})
it('accepts different name for base64 maps with UTF-8 encoding', () => {
let b64 = Buffer.from(map).toString('base64')
let css =
'a{}\n/*# sourceMappingURL=data:application/json;' +
'charset=utf8;base64,' +
b64 +
' */'
expect(parse(css).source?.input.map.text).toEqual(map)
})
it('decodes URI maps', () => {
let uri = 'data:application/json,' + decodeURI(map)
let css = `a{}\n/*# sourceMappingURL=${uri} */`
expect(parse(css).source?.input.map.text).toEqual(map)
})
it('decodes URI UTF-8 maps', () => {
let uri = decodeURI(map)
let css =
'a{}\n/*# sourceMappingURL=data:application/json;' +
'charset=utf-8,' +
uri +
' */'
expect(parse(css).source?.input.map.text).toEqual(map)
})
it('accepts different name for URI maps with UTF-8 encoding', () => {
let uri = decodeURI(map)
let css =
'a{}\n/*# sourceMappingURL=data:application/json;' +
'charset=utf8,' +
uri +
' */'
expect(parse(css).source?.input.map.text).toEqual(map)
})
it('removes map on request', () => {
let uri = 'data:application/json,' + decodeURI(map)
let css = `a{}\n/*# sourceMappingURL=${uri} */`
let input = parse(css, { map: { prev: false } }).source?.input
expect(input?.map).not.toBeDefined()
})
it('raises on unknown inline encoding', () => {
let css =
'a { }\n/*# sourceMappingURL=data:application/json;' +
'md5,68b329da9893e34099c7d8ad5cb9c940*/'
expect(() => {
parse(css)
}).toThrow('Unsupported source map encoding md5')
})
it('raises on unknown map format', () => {
expect(() => {
// @ts-expect-error
parse('a{}', { map: { prev: 1 } })
}).toThrow('Unsupported previous source map format: 1')
})
it('reads map from annotation', () => {
let file = join(dir, 'a.map')
outputFileSync(file, map)
let root = parse('a{}\n/*# sourceMappingURL=a.map */', { from: file })
expect(root.source?.input.map.text).toEqual(map)
expect(root.source?.input.map.root).toEqual(dir)
})
it('reads only the last map from annotation', () => {
let file = join(dir, 'c.map')
outputFileSync(file, map)
let root = parse(
'a{}' +
'\n/*# sourceMappingURL=a.map */' +
'\n/*# sourceMappingURL=b.map */' +
'\n/*# sourceMappingURL=c.map */',
{ from: file }
)
expect(root.source?.input.map.text).toEqual(map)
expect(root.source?.input.map.root).toEqual(dir)
})
it('sets unique name for inline map', () => {
let map2 = {
version: 3,
sources: ['a'],
names: [],
mappings: ''
}
let opts = { map: { prev: map2 } }
let file1 = parse('a{}', opts).source?.input.map.file
let file2 = parse('a{}', opts).source?.input.map.file
expect(file1).toMatch(/^<input css [\w-]+>$/)
expect(file1).not.toEqual(file2)
})
it('accepts an empty mappings string', () => {
expect(() => {
let emptyMap = {
version: 3,
sources: [],
names: [],
mappings: ''
}
parse('body{}', { map: { prev: emptyMap } })
}).not.toThrow()
})
it('accepts a function', () => {
let css = 'body{}\n/*# sourceMappingURL=a.map */'
let file = join(dir, 'previous-sourcemap-function.map')
outputFileSync(file, map)
let opts = {
map: {
prev: () => file
}
}
let root = parse(css, opts)
expect(root.source?.input.map.text).toEqual(map)
expect(root.source?.input.map.annotation).toEqual('a.map')
})
it('calls function with opts.from', () => {
expect.assertions(1)
let css = 'body{}\n/*# sourceMappingURL=a.map */'
let file = join(dir, 'previous-sourcemap-function.map')
outputFileSync(file, map)
parse(css, {
from: 'a.css',
map: {
prev: from => {
expect(from).toEqual('a.css')
return file
}
}
})
})
it('raises when function returns invalid path', () => {
let css = 'body{}\n/*# sourceMappingURL=a.map */'
let fakeMap = Number.MAX_SAFE_INTEGER.toString() + '.map'
let fakePath = join(dir, fakeMap)
let opts = {
map: {
prev: () => fakePath
}
}
expect(() => {
parse(css, opts)
}).toThrow('Unable to load previous source map: ' + fakePath)
})
it('uses source map path as a root', () => {
let from = join(dir, 'a.css')
outputFileSync(
join(dir, 'maps', 'a.map'),
JSON.stringify({
version: 3,
mappings: 'AAAA,CAAC;EAAC,CAAC,EAAC,CAAC',
sources: ['../../test.scss'],
names: [],
file: 'test.css'
})
)
let root = parse('a{}\n/*# sourceMappingURL=maps/a.map */', { from })
expect(root.source?.input.origin(1, 1)).toEqual({
url: pathToFileURL(join(dir, '..', 'test.scss')).href,
file: join(dir, '..', 'test.scss'),
line: 1,
column: 1
})
})
it('uses current file path for source map', () => {
let root = parse('a{b:1}', {
from: join(__dirname, 'dir', 'subdir', 'a.css'),
map: {
prev: {
version: 3,
mappings: 'AAAA,CAAC;EAAC,CAAC,EAAC,CAAC',
sources: ['../test.scss'],
names: [],
file: 'test.css'
}
}
})
expect(root.source?.input.origin(1, 1)).toEqual({
url: pathToFileURL(join(__dirname, 'dir', 'test.scss')).href,
file: join(__dirname, 'dir', 'test.scss'),
line: 1,
column: 1
})
})
it('works with non-file sources', () => {
let root = parse('a{b:1}', {
from: join(__dirname, 'dir', 'subdir', 'a.css'),
map: {
prev: {
version: 3,
mappings: 'AAAA,CAAC;EAAC,CAAC,EAAC,CAAC',
sources: ['http://example.com/test.scss'],
names: [],
file: 'test.css'
}
}
})
expect(root.source?.input.origin(1, 1)).toEqual({
url: 'http://example.com/test.scss',
line: 1,
column: 1
})
})
it('works with index map', () => {
let root = parse('body {\nwidth:100%;\n}', {
from: join(__dirname, 'a.css'),
map: {
prev: {
version: 3,
sections: [
{
offset: { line: 0, column: 0 },
map: {
version: 3,
mappings: 'AAAA;AACA;AACA;',
sources: ['b.css'],
sourcesContent: ['body {\nwidth:100%;\n}']
}
}
]
}
}
})
expect((root as any).source.input.origin(1, 1).file).toEqual(
join(__dirname, 'b.css')
)
})