-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathloader.test.js
380 lines (322 loc) · 13.6 KB
/
loader.test.js
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import path from 'path';
import fs from 'fs';
import fetch from 'node-fetch';
import {
compile,
getCodeFromBundle,
getCompiler,
getErrors,
normalizeMap,
getWarnings,
readAsset,
} from './helpers';
const isWin = process.platform === 'win32';
describe('source-map-loader', () => {
it('should leave normal files untouched', async () => {
const testId = 'normal-file.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
expect(codeFromBundle.map).toBeUndefined();
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should leave normal files with fake source-map untouched', async () => {
const testId = 'normal-file2.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
expect(codeFromBundle.map).toBeUndefined();
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should process inlined SourceMaps', async () => {
const testId = 'inline-source-map.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
expect(codeFromBundle.map).toBeDefined();
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should process external SourceMaps', async () => {
const testId = 'external-source-map.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const deps = stats.compilation.fileDependencies;
const dependencies = [
path.resolve(__dirname, 'fixtures', 'external-source-map.map'),
];
dependencies.forEach((fixture) => {
expect(deps.has(fixture)).toBe(true);
});
expect(codeFromBundle.map).toBeDefined();
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should process external SourceMaps (external sources)', async () => {
const testId = 'external-source-map2.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const deps = stats.compilation.fileDependencies;
const dependencies = [
path.resolve(__dirname, 'fixtures', 'data', 'external-source-map2.map'),
path.resolve(__dirname, 'fixtures', 'external-source-map2.txt'),
];
dependencies.forEach((fixture) => {
expect(deps.has(fixture)).toBe(true);
});
expect(codeFromBundle.map).toBeDefined();
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should resolve SourceMap.sources to http', async () => {
const currentDirPath = path.join(__dirname, 'fixtures', 'fetch');
const testId = path.join(currentDirPath, 'sources-http.js');
const compiler = getCompiler(testId, {
fetchReader(url) {
return fetch(url)
.then((res) => res.text())
.then(() => 'some kind content');
},
});
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should not resolve SourceMap.sources to http', async () => {
const currentDirPath = path.join(__dirname, 'fixtures', 'fetch');
const testId = path.join(currentDirPath, 'sources-http.js');
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should reject http SourceMaps', async () => {
const testId = 'http-source-map.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should reject not exist file: SourceMaps', async () => {
const testId = isWin ? 'file-source-map-windows.js' : 'file-source-map.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const errorString = getWarnings(stats).join('');
const warning = errorString.match(/TypeError \[ERR_INVALID_FILE/gi);
expect(...warning).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should use last SourceMap directive', async () => {
const testId = 'multi-source-map.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
expect(codeFromBundle.map).toBeDefined();
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should skip invalid base64 SourceMap', async () => {
const testId = 'invalid-inline-source-map.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
expect(codeFromBundle.map).toBeUndefined();
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should warn on invalid base64 SourceMap', async () => {
const testId = 'invalid-inline-source-map2.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
expect(codeFromBundle.map).toBeUndefined();
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should warn on invalid SourceMap', async () => {
const testId = 'invalid-source-map.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const deps = stats.compilation.fileDependencies;
const dependencies = [
path.resolve(__dirname, 'fixtures', 'invalid-source-map.map'),
];
dependencies.forEach((fixture) => {
expect(deps.has(fixture)).toBe(true);
});
expect(codeFromBundle.map).toBeUndefined();
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should warn on missing SourceMap', async () => {
const testId = 'missing-source-map.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
expect(codeFromBundle.map).toBeUndefined();
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should warn on missing source file', async () => {
const testId = 'missing-source-map2.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const deps = stats.compilation.fileDependencies;
const dependencies = [
path.resolve(__dirname, 'fixtures', 'missing-source-map2.map'),
];
dependencies.forEach((fixture) => {
expect(deps.has(fixture)).toBe(true);
});
expect(codeFromBundle.map).toBeDefined();
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should process inlined SourceMaps with charset', async () => {
const testId = 'charset-inline-source-map.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
expect(codeFromBundle.map).toBeDefined();
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should support absolute sourceRoot paths in sourcemaps', async () => {
const sourceRoot = path.resolve(__dirname, 'fixtures');
const javaScriptFilename = 'absolute-sourceRoot-source-map.js';
const sourceFilename = 'absolute-sourceRoot-source-map.txt';
const rootRelativeSourcePath = path.join(sourceRoot, sourceFilename);
const sourceMapPath = path.join(
sourceRoot,
'absolute-sourceRoot-source-map.map'
);
// Create the sourcemap file
const rawSourceMap = {
version: 3,
file: javaScriptFilename,
sourceRoot,
sources: [sourceFilename],
mappings: 'AAAA',
};
fs.writeFileSync(sourceMapPath, JSON.stringify(rawSourceMap));
const compiler = getCompiler(javaScriptFilename);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const deps = stats.compilation.fileDependencies;
const dependencies = [sourceMapPath, rootRelativeSourcePath];
dependencies.forEach((fixture) => {
expect(deps.has(fixture)).toBe(true);
});
expect(codeFromBundle.map).toBeDefined();
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should support relative sourceRoot paths in sourcemaps', async () => {
const sourceFilename = 'relative-sourceRoot-source-map.txt';
const rootRelativeSourcePath = path.join(
__dirname,
'fixtures',
'data',
sourceFilename
);
const sourceMapPath = path.join(
__dirname,
'fixtures',
'relative-sourceRoot-source-map.map'
);
const testId = 'relative-sourceRoot-source-map.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const deps = stats.compilation.fileDependencies;
const dependencies = [sourceMapPath, rootRelativeSourcePath];
dependencies.forEach((fixture) => {
expect(deps.has(fixture)).toBe(true);
});
expect(codeFromBundle.map).toBeDefined();
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should support indexed sourcemaps', async () => {
const currentDirPath = path.join(
__dirname,
'fixtures',
'indexed-sourcemap'
);
const testId = path.join(currentDirPath, 'file.js');
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const deps = stats.compilation.fileDependencies;
const dependencies = [
path.join(currentDirPath, 'file.js'),
path.join(currentDirPath, 'file.js.map'),
path.join(currentDirPath, 'nested1.js'),
`/different/root/nested2.js`,
];
dependencies.forEach((fixture) => {
expect(deps.has(fixture)).toBe(true);
});
expect(codeFromBundle.map).toBeDefined();
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
it('should transform to webpack', async () => {
const currentDirPath = path.join(
__dirname,
'fixtures',
'indexed-sourcemap'
);
const testId = path.join(currentDirPath, 'file.js');
const compiler = getCompiler(testId, {}, {}, true);
const stats = await compile(compiler);
const bundle = readAsset('main.bundle.js.map', compiler, stats);
const dependencies = [
'indexed-sourcemap/nested1.js',
'nested2.js',
'webpack/bootstrap',
];
// Todo: rewrite when we will fix issue whith unresolved paths
dependencies.forEach((fixture) => {
expect(bundle.indexOf(fixture) !== -1).toBe(true);
});
});
});