Skip to content

Commit 212ab57

Browse files
committed
{refactor} use convert-source-map instead of trying to parse sourcemappingUrls
1 parent a4c9c78 commit 212ab57

File tree

4 files changed

+84
-46
lines changed

4 files changed

+84
-46
lines changed

.pnp.cjs

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"devDependencies": {
3131
"@jridgewell/trace-mapping": "^0.3.17",
3232
"@playwright/test": "^1.14.1",
33+
"@types/convert-source-map": "^2.0.3",
3334
"@types/istanbul-lib-coverage": "^2.0.3",
3435
"@types/istanbul-lib-report": "^3.0.0",
3536
"@types/istanbul-reports": "^3.0.1",
@@ -42,6 +43,7 @@
4243
"dependencies": {
4344
"@bcoe/v8-coverage": "^0.2.3",
4445
"comlink": "^4.4.2",
46+
"convert-source-map": "^2.0.0",
4547
"istanbul-lib-coverage": "^3.2.2",
4648
"istanbul-lib-report": "^3.0.1",
4749
"istanbul-reports": "^3.1.7",

src/data.ts

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {isMatch} from 'micromatch';
77
import {join, posix} from 'path';
88
import {pathToFileURL, URL} from 'url';
99
import v8ToIstanbul from 'v8-to-istanbul';
10+
import * as convertSourceMap from 'convert-source-map';
1011

1112
export const attachmentName = '@bgotink/playwright-coverage';
1213

@@ -44,55 +45,62 @@ export async function getSourceMap(
4445
url: string,
4546
source: string,
4647
): Promise<EncodedSourceMap | undefined> {
47-
const match = source.match(/\/\/# *sourceMappingURL=(.*)/);
48-
49-
if (match == null) {
50-
try {
51-
const response = await (
52-
await fetch
53-
).default(`${url}.map`, {
54-
method: 'GET',
55-
});
56-
57-
return (await response.json()) as EncodedSourceMap;
58-
} catch {
59-
return undefined;
60-
}
48+
const inlineMap = convertSourceMap.fromSource(source);
49+
if (inlineMap != null) {
50+
return inlineMap.sourcemap;
6151
}
6252

63-
const resolved = new URL(match[1]!, url);
64-
6553
try {
66-
switch (resolved.protocol) {
67-
case 'file:':
68-
return JSON.parse(await fs.readFile(resolved, 'utf8'));
69-
case 'data:': {
70-
if (!/^application\/json[,;]/.test(resolved.pathname)) {
71-
return undefined;
54+
const linkedMap = await convertSourceMap.fromMapFileSource(
55+
source,
56+
async (file: string): Promise<string> => {
57+
const resolved = new URL(file, url);
58+
59+
switch (resolved.protocol) {
60+
case 'file:':
61+
return await fs.readFile(resolved, 'utf8');
62+
case 'data:': {
63+
const comma = resolved.pathname.indexOf(',');
64+
const rawData = resolved.pathname.slice(comma + 1);
65+
const between = resolved.pathname
66+
.slice('application/json;'.length, comma)
67+
.split(';');
68+
69+
const dataString = between.includes('base64')
70+
? Buffer.from(rawData, 'base64url').toString('utf8')
71+
: rawData;
72+
73+
return dataString;
74+
}
75+
default: {
76+
const response = await (
77+
await fetch
78+
).default(resolved.href, {
79+
method: 'GET',
80+
});
81+
82+
return await response.text();
83+
}
7284
}
85+
},
86+
);
7387

74-
const comma = resolved.pathname.indexOf(',');
75-
const rawData = resolved.pathname.slice(comma + 1);
76-
const between = resolved.pathname
77-
.slice('application/json;'.length, comma)
78-
.split(';');
88+
if (linkedMap != null) {
89+
return linkedMap.sourcemap;
90+
}
91+
} catch {
92+
return null!;
93+
}
7994

80-
const dataString = between.includes('base64')
81-
? Buffer.from(rawData, 'base64url').toString('utf8')
82-
: rawData;
95+
// No source map comments, try to find an implicit sourcemap at `${url}.map`
96+
try {
97+
const response = await (
98+
await fetch
99+
).default(`${url}.map`, {
100+
method: 'GET',
101+
});
83102

84-
return JSON.parse(dataString);
85-
}
86-
default: {
87-
const response = await (
88-
await fetch
89-
).default(resolved.href, {
90-
method: 'GET',
91-
});
92-
93-
return (await response.json()) as EncodedSourceMap;
94-
}
95-
}
103+
return (await response.json()) as EncodedSourceMap;
96104
} catch {
97105
return undefined;
98106
}
@@ -163,10 +171,16 @@ export async function convertToIstanbulCoverage(
163171
// This path is used to resolve files, but the filename doesn't matter
164172
join(sourceRoot, 'unused.js'),
165173
0,
166-
{
167-
source,
168-
sourceMap: {sourcemap: sourceMap},
169-
},
174+
sourceMap?.mappings
175+
? {
176+
source,
177+
sourceMap: {sourcemap: sourceMap},
178+
}
179+
: {
180+
source: convertSourceMap.removeMapFileComments(
181+
convertSourceMap.removeComments(source),
182+
),
183+
},
170184
path => {
171185
let isExcluded = isExcludedCache.get(path);
172186

yarn.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,13 +592,15 @@ __metadata:
592592
"@bcoe/v8-coverage": ^0.2.3
593593
"@jridgewell/trace-mapping": ^0.3.17
594594
"@playwright/test": ^1.14.1
595+
"@types/convert-source-map": ^2.0.3
595596
"@types/istanbul-lib-coverage": ^2.0.3
596597
"@types/istanbul-lib-report": ^3.0.0
597598
"@types/istanbul-reports": ^3.0.1
598599
"@types/micromatch": ^4.0.2
599600
"@types/node": ^16.9.3
600601
"@types/node-fetch": ^2
601602
comlink: ^4.4.2
603+
convert-source-map: ^2.0.0
602604
istanbul-lib-coverage: ^3.2.2
603605
istanbul-lib-report: ^3.0.1
604606
istanbul-reports: ^3.1.7
@@ -704,6 +706,13 @@ __metadata:
704706
languageName: node
705707
linkType: hard
706708

709+
"@types/convert-source-map@npm:^2.0.3":
710+
version: 2.0.3
711+
resolution: "@types/convert-source-map@npm:2.0.3"
712+
checksum: 411cf9a02cf5dbe204e325dd5ebf50de00b58b38d1d2a3064c6ea28417c23bae956206eaa9ed3a75a994909b4ab3f9c6389073d0636a62500fa6d6333c64d45a
713+
languageName: node
714+
linkType: hard
715+
707716
"@types/istanbul-lib-coverage@npm:*, @types/istanbul-lib-coverage@npm:^2.0.0, @types/istanbul-lib-coverage@npm:^2.0.1, @types/istanbul-lib-coverage@npm:^2.0.3":
708717
version: 2.0.3
709718
resolution: "@types/istanbul-lib-coverage@npm:2.0.3"

0 commit comments

Comments
 (0)