UNPKG

6.17 kBJavaScriptView Raw
1import assert from 'assert';
2import {
3 matchSourceMappingURL,
4 loadSourceMapUrl,
5 loadSourceMap,
6} from '../src/sourcemap';
7import {NodeFS} from '@parcel/fs';
8import path from 'path';
9
10const fs = new NodeFS();
11
12describe('loadSourceMap', () => {
13 it('should not match sourceMappingURL when not at the end of the bundle', () => {
14 // Code example taken from livescript.js (issue #2408 in parcel-bundler)
15 // This snippet lead to JSAsset.js being mislead and incorrectly trying to
16 // load (due to false-positive match) sourcemap before fix was introduced
17 let code = fs.readFileSync(
18 path.join(__dirname, './input/sourcemap/no-sourcemap.js'),
19 'utf-8',
20 );
21
22 assert(!matchSourceMappingURL(code));
23 });
24
25 it('should match referenced-min sourceMappingURL when correctly inserted at end of the bundle', () => {
26 let code = fs.readFileSync(
27 path.join(__dirname, './input/sourcemap/referenced-min.js'),
28 'utf-8',
29 );
30
31 assert(!!matchSourceMappingURL(code));
32 });
33
34 it('should match inline sourceMappingURL when correctly inserted at end of the bundle', () => {
35 // inline source map taken from https://github.com/thlorenz/inline-source-map
36 let code = fs.readFileSync(
37 path.join(__dirname, './input/sourcemap/inline.js'),
38 'utf-8',
39 );
40
41 assert(!!matchSourceMappingURL(code));
42 });
43
44 it('Should be able to load sourcemap data from a url reference', async () => {
45 let filename = path.join(__dirname, './input/sourcemap/referenced-min.js');
46 let contents = fs.readFileSync(filename, 'utf-8');
47
48 let foundMap = await loadSourceMapUrl(fs, filename, contents);
49 assert.equal(foundMap.url, 'referenced-min.js.map');
50 assert.equal(
51 foundMap.filename,
52 path.join(path.dirname(filename), foundMap.url),
53 );
54 assert.deepEqual(foundMap.map, {
55 version: 3,
56 sources: ['./referenced.js'],
57 names: ['hello', 'l', 'o', 'console', 'log'],
58 mappings:
59 'AAAA,SAASA,QACP,IAAIC,EAAI,QACNC,EAAI,QACNC,QAAQC,IAAIH,EAAI,IAAMC,EAAI,KAE5BF',
60 });
61 });
62
63 it('Should be able to load sourcemap data from an inline url reference', async () => {
64 let filename = path.join(__dirname, './input/sourcemap/inline.js');
65 let contents = fs.readFileSync(filename, 'utf-8');
66
67 let foundMap = await loadSourceMapUrl(fs, filename, contents);
68 assert.equal(
69 foundMap.url,
70 'data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmb28uanMiLCJiYXIuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7O1VBQ0c7Ozs7Ozs7Ozs7Ozs7O3NCQ0RIO3NCQUNBIn0=',
71 );
72 assert.equal(foundMap.filename, filename);
73 assert.deepEqual(foundMap.map, {
74 version: 3,
75 file: '',
76 sources: ['foo.js', 'bar.js'],
77 names: [],
78 mappings: ';;;;;;;;;UACG;;;;;;;;;;;;;;sBCDH;sBACA',
79 });
80 });
81
82 it('Should be able to load a SourceMap instance from a file', async () => {
83 let filename = path.join(__dirname, './input/sourcemap/referenced-min.js');
84 let contents = fs.readFileSync(filename, 'utf-8');
85
86 let map = await loadSourceMap(filename, contents, {
87 fs,
88 projectRoot: __dirname,
89 });
90
91 assert(!!map);
92
93 let parsedMap = map.getMap();
94 assert.deepEqual(parsedMap.sources, [
95 path.normalize('input/sourcemap/referenced.js'),
96 ]);
97 assert.deepEqual(parsedMap.names, ['hello', 'l', 'o', 'console', 'log']);
98 assert.deepEqual(parsedMap.mappings, [
99 {
100 generated: {line: 1, column: 0},
101 original: {line: 1, column: 0},
102 source: 0,
103 },
104 {
105 generated: {line: 1, column: 9},
106 original: {line: 1, column: 9},
107 source: 0,
108 name: 0,
109 },
110 {
111 generated: {line: 1, column: 17},
112 original: {line: 2, column: 2},
113 source: 0,
114 },
115 {
116 generated: {line: 1, column: 21},
117 original: {line: 2, column: 6},
118 source: 0,
119 name: 1,
120 },
121 {
122 generated: {line: 1, column: 23},
123 original: {line: 2, column: 10},
124 source: 0,
125 },
126 {
127 generated: {line: 1, column: 31},
128 original: {line: 3, column: 4},
129 source: 0,
130 name: 2,
131 },
132 {
133 generated: {line: 1, column: 33},
134 original: {line: 3, column: 8},
135 source: 0,
136 },
137 {
138 generated: {line: 1, column: 41},
139 original: {line: 4, column: 2},
140 source: 0,
141 name: 3,
142 },
143 {
144 generated: {line: 1, column: 49},
145 original: {line: 4, column: 10},
146 source: 0,
147 name: 4,
148 },
149 {
150 generated: {line: 1, column: 53},
151 original: {line: 4, column: 14},
152 source: 0,
153 name: 1,
154 },
155 {
156 generated: {line: 1, column: 55},
157 original: {line: 4, column: 18},
158 source: 0,
159 },
160 {
161 generated: {line: 1, column: 59},
162 original: {line: 4, column: 24},
163 source: 0,
164 name: 2,
165 },
166 {
167 generated: {line: 1, column: 61},
168 original: {line: 4, column: 28},
169 source: 0,
170 },
171 {
172 generated: {line: 1, column: 66},
173 original: {line: 6, column: 0},
174 source: 0,
175 name: 0,
176 },
177 ]);
178 });
179
180 it('Should remap sources when using sourceRoot', async () => {
181 let filename = path.join(__dirname, './input/sourcemap/referenced-min.js');
182 let contents = fs.readFileSync(filename, 'utf-8');
183
184 let map = await loadSourceMap(filename, contents, {
185 fs,
186 projectRoot: __dirname,
187 });
188
189 assert(!!map);
190
191 let parsedMap = map.getMap();
192 assert.deepEqual(parsedMap.sources, [
193 path.normalize('input/sourcemap/referenced.js'),
194 ]);
195 });
196
197 it('Should remap sources when using sourceRoot', async () => {
198 let filename = path.join(__dirname, './input/sourcemap/source-root.js');
199 let contents = fs.readFileSync(filename, 'utf-8');
200
201 let map = await loadSourceMap(filename, contents, {
202 fs,
203 projectRoot: __dirname,
204 });
205
206 assert(!!map);
207
208 let parsedMap = map.getMap();
209 assert.deepEqual(parsedMap.sources, [path.normalize('input/source.js')]);
210 });
211});