Skip to content

Commit 1f3bdcd

Browse files
+change sourcemap testing way
1 parent 85f6b9c commit 1f3bdcd

File tree

10 files changed

+82
-19
lines changed

10 files changed

+82
-19
lines changed

cases/babel/reference/build.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = [
2+
{
3+
original: { source: 'webpack:///file-to-annotate.js', line: 33, column: 2 },
4+
generated: { line: 84, column: 3},
5+
},
6+
{
7+
original: { source: 'webpack:///file-to-annotate.js', line: 41, column: 0 },
8+
generated: { line: 95, column: 0 },
9+
},
10+
];

cases/simple/reference/build.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = [
2+
{
3+
original: { source: 'webpack:///file-to-annotate.js', line: 24, column: 5 },
4+
generated: { line: 71, column: 3},
5+
},
6+
{
7+
original: { source: 'webpack:///file-to-annotate.js', line: 11, column: 5 },
8+
generated: { line: 58, column: 3 },
9+
},
10+
];

cases/typescript/reference/build.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = [
2+
{
3+
original: { source: 'webpack:///file-to-annotate.ts', line: 6, column: 0 },
4+
generated: { line: 50, column: 0},
5+
},
6+
{
7+
original: { source: 'webpack:///file-to-annotate.ts', line: 16, column: 5 },
8+
generated: { line: 58, column: 0 },
9+
},
10+
{
11+
original: { source: 'webpack:///file-to-annotate.ts', line: 30, column: 3 },
12+
generated: { line: 70, column: 0 },
13+
},
14+
];

cases/uglifyjs/reference/build.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = [
2+
{
3+
original: { source: 'webpack:///file-to-annotate.ts', line: 6, column: 0 },
4+
generated: { line: 1, column: 303},
5+
},
6+
{
7+
original: { source: 'webpack:///file-to-annotate.ts', line: 16, column: 5 },
8+
generated: { line: 1, column: 502 },
9+
},
10+
{
11+
original: { source: 'webpack:///file-to-annotate.ts', line: 30, column: 3 },
12+
generated: { line: 1, column: 253 },
13+
},
14+
];

loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var SourceMapGenerator = require('source-map').SourceMapGenerator;
55
var normalizePath = require('normalize-path');
66

77
function loadPlugins(pluginNames) {
8-
var pluginNames = pluginNames || [];
8+
pluginNames = pluginNames || [];
99
return pluginNames.map(function(name) {
1010
return require(name);
1111
});

run-tests.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
const test = require('tape');
44
const webpack = require('webpack');
55
const fs = require('fs');
6-
const crlf = require('crlf-helper');
6+
const SourceMapConsumer = require('source-map').SourceMapConsumer;
77

88
test.createStream()
99
.pipe(require('tap-spec')())
1010
.pipe(process.stdout);
1111

12-
const cases = ['typescript', 'babel', 'simple', 'uglifyjs'];
12+
const cases = ['typescript','simple', 'babel', 'uglifyjs'];
1313

1414
for (let testCase of cases) {
1515
test('Acceptance tests. Case ' + testCase, (t) => {
@@ -35,24 +35,43 @@ for (let testCase of cases) {
3535

3636
t.equal(actualSource, expectedSource, 'Test annotated source');
3737

38-
const actualMap = prepareMap(fs.readFileSync(folder + '/dist/build.js.map', 'utf8'));
39-
const expectedMap = prepareMap(fs.readFileSync(folder + '/reference/build.js.map', 'utf8'));
40-
41-
t.deepEqual(actualMap.sourcesContent, expectedMap.sourcesContent, 'Test source map sourceContent');
42-
t.deepEqual(actualMap.sources, expectedMap.sources, 'Test source map sources');
43-
t.equal(actualMap.mappings, expectedMap.mappings, 'Test source map mappings');
38+
const actualMap = fs.readFileSync(folder + '/dist/build.js.map', 'utf8');
39+
testMap(t, actualMap, require(folder + '/reference/sourcemap-checkpoints'));
4440
});
4541

46-
t.plan(4);
42+
t.plan(2);
4743
});
4844
}
4945

50-
function prepareMap(content){
51-
const map = JSON.parse(content.replace(/webpack\/bootstrap [\d\w]+/g, 'webpack/bootstrap [hash]')); // remove hash from map
46+
/**
47+
*
48+
* @param t
49+
* @param content
50+
* @param {array<{original, generated}>} checkpoints
51+
*/
52+
function testMap(t, content, checkpoints){
53+
t.test('Check source map cases', (t) => {
54+
const rawMap = JSON.parse(content);
55+
const map = new SourceMapConsumer(rawMap);
56+
57+
const sources = rawMap.sources.map((source) => {
58+
const matches = source.match(/[^/]+\..+$/);
59+
return matches ? matches[0] : source;
60+
});
61+
62+
t.equal(sources.length, uniq(sources).length, 'No duplicates in sourcemap sources');
5263

53-
map.sourcesContent = map.sourcesContent.map((sourceContent) => {
54-
return crlf.setLineEnding(sourceContent, 'LF');
64+
for(let point of checkpoints) {
65+
const result = map.generatedPositionFor(point.original);
66+
67+
t.deepEqual({line: result.line, column: result.column}, point.generated);
68+
}
5569
});
5670

57-
return map;
71+
t.end();
72+
}
73+
74+
75+
function uniq(a) {
76+
return Array.from(new Set(a));
5877
}

0 commit comments

Comments
 (0)