Skip to content

Commit 1a87ddd

Browse files
committed
Cache instances of SourceMapConsumer
Creating these was apparently a little expensive. This fixes #1256 [1]. [1] #1256
1 parent 598f968 commit 1a87ddd

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ All notable changes to this project will be documented in this file.
66

77
- Correctly represent consecutive BeforeAll & AfterAll hooks in the command log, relates to [#1250](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1250).
88

9+
- Cache instances of SourceMapConsumer, fixes [#1256](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1256).
10+
911
## v21.0.2
1012

1113
- Cache requested source maps, fixes [#1245](https://github.com/badeball/cypress-cucumber-preprocessor/discussions/1245).

lib/helpers/source-map.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function sourceMapWarn(message: string) {
2121
isSourceMapWarned = true;
2222
}
2323

24-
const cache = new Map<string, string | undefined>();
24+
const cache = new Map<string, SourceMapConsumer | undefined>();
2525

2626
/**
2727
* Taken from https://github.com/evanw/node-source-map-support/blob/v0.5.21/source-map-support.js#L148-L177.
@@ -63,11 +63,36 @@ export function retrieveSourceMapURL(source: string) {
6363
return lastMatch[1];
6464
}
6565

66-
export function cachedRetrieveSourceMapURL(source: string): string | undefined {
66+
export function createSourceMapConsumer(
67+
source: string,
68+
): SourceMapConsumer | undefined {
69+
const sourceMappingURL = retrieveSourceMapURL(source);
70+
71+
if (!sourceMappingURL) {
72+
return;
73+
}
74+
75+
const rawSourceMap = JSON.parse(
76+
new TextDecoder().decode(
77+
toByteArray(sourceMappingURL.slice(sourceMappingURL.indexOf(",") + 1)),
78+
),
79+
);
80+
81+
// Why? Because of Vite. Vite fails building the source-map module properly and this errors with "x is not a constructor".
82+
if (typeof SourceMapConsumer !== "function") {
83+
return;
84+
}
85+
86+
return new SourceMapConsumer(rawSourceMap);
87+
}
88+
89+
export function cachedCreateSourceMapConsumer(
90+
source: string,
91+
): SourceMapConsumer | undefined {
6792
if (cache.has(source)) {
6893
return cache.get(source);
6994
} else {
70-
const result = retrieveSourceMapURL(source);
95+
const result = createSourceMapConsumer(source);
7196
cache.set(source, result);
7297
return result;
7398
}
@@ -80,25 +105,12 @@ export function maybeRetrievePositionFromSourceMap(): Position | undefined {
80105
return;
81106
}
82107

83-
const sourceMappingURL = cachedRetrieveSourceMapURL(stack[0].fileName);
108+
const sourceMap = cachedCreateSourceMapConsumer(stack[0].fileName);
84109

85-
if (!sourceMappingURL) {
110+
if (!sourceMap) {
86111
return;
87112
}
88113

89-
const rawSourceMap = JSON.parse(
90-
new TextDecoder().decode(
91-
toByteArray(sourceMappingURL.slice(sourceMappingURL.indexOf(",") + 1)),
92-
),
93-
);
94-
95-
// Why? Because of Vite. Vite fails building the source-map module properly and this errors with "x is not a constructor".
96-
if (typeof SourceMapConsumer !== "function") {
97-
return;
98-
}
99-
100-
const sourceMap = new SourceMapConsumer(rawSourceMap);
101-
102114
const relevantFrame = stack[3];
103115

104116
const position = sourceMap.originalPositionFor({

0 commit comments

Comments
 (0)