Skip to content

Commit 90e0880

Browse files
Split the inline-source-map code into a separate function
1 parent 0102271 commit 90e0880

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

src/index.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,9 @@ export default function(this: loader.LoaderContext, source: string, sourceMap?:
2020
options = Object.assign(defaultOptions, options);
2121
validateOptions(optionsSchema, options, "Coverage Istanbul Loader");
2222

23-
try {
24-
if (!sourceMap) {
25-
// Check for an inline source map
26-
const inlineSourceMap = convert.fromSource(source)
27-
|| convert.fromMapFileSource(source, path.dirname(this.resourcePath));
28-
29-
if (inlineSourceMap) {
30-
// Use the inline source map
31-
sourceMap = inlineSourceMap.sourcemap as RawSourceMap;
32-
}
33-
}
34-
} catch (e) {
35-
// Exception is thrown by fromMapFileSource when there is no source map file
36-
if (e instanceof Error && e.message.includes("An error occurred while trying to read the map file at")) {
37-
this.emitWarning(e);
38-
} else {
39-
throw e;
40-
}
23+
// If there's no external sourceMap file, then check for an inline sourceMap
24+
if (!sourceMap) {
25+
sourceMap = getInlineSourceMap.call(this, source);
4126
}
4227

4328
// Instrument the code
@@ -56,3 +41,28 @@ export default function(this: loader.LoaderContext, source: string, sourceMap?:
5641
this.callback(error, instrumentedSource, instrumentedSourceMap);
5742
}
5843
}
44+
45+
/**
46+
* If the source code has an inline base64-encoded source map,
47+
* then this function decodes it, parses it, and returns it.
48+
*/
49+
function getInlineSourceMap(this: loader.LoaderContext, source: string): RawSourceMap | undefined {
50+
try {
51+
// Check for an inline source map
52+
const inlineSourceMap = convert.fromSource(source)
53+
|| convert.fromMapFileSource(source, path.dirname(this.resourcePath));
54+
55+
if (inlineSourceMap) {
56+
// Use the inline source map
57+
return inlineSourceMap.sourcemap as RawSourceMap;
58+
}
59+
}
60+
catch (e) {
61+
// Exception is thrown by fromMapFileSource when there is no source map file
62+
if (e instanceof Error && e.message.includes("An error occurred while trying to read the map file at")) {
63+
this.emitWarning(e);
64+
} else {
65+
throw e;
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)