Skip to content

Commit 0329397

Browse files
Merge pull request #42 from storybookjs/valentin/fix-source-maps-for-webpack5
Fix sourcemap generation for Webpack5 projects
2 parents eecd87c + 041aa23 commit 0329397

File tree

2 files changed

+5
-48
lines changed

2 files changed

+5
-48
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
"convert-source-map": "^2.0.0",
8282
"espree": "^9.6.1",
8383
"istanbul-lib-instrument": "^6.0.1",
84-
"source-map": "^0.7.4",
8584
"test-exclude": "^6.0.0",
8685
"vite-plugin-istanbul": "^3.0.1"
8786
}

src/loader/webpack5-istanbul-loader.ts

+5-47
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import * as espree from "espree";
66
import fs from "fs";
77
import path from "path";
88
import { LoaderContext } from "webpack";
9-
import { SourceMapGenerator, StartOfSourceMap } from "source-map";
109

1110
import { AddonOptionsWebpack } from "../types";
1211

@@ -26,39 +25,17 @@ type RawSourceMap = {
2625
};
2726

2827
function sanitizeSourceMap(rawSourceMap: RawSourceMap | string): RawSourceMap {
29-
if (typeof rawSourceMap === 'string') return JSON.parse(rawSourceMap);
30-
const { sourcesContent, ...sourceMap } = rawSourceMap ?? {};
31-
32-
// JSON parse/stringify trick required for istanbul to accept the SourceMap
33-
return JSON.parse(JSON.stringify(sourceMap));
34-
}
35-
36-
function createIdentitySourceMap(
37-
file: string,
38-
source: string,
39-
option: StartOfSourceMap
40-
) {
41-
const gen = new SourceMapGenerator(option);
42-
const tokens = espree.tokenize(source, { loc: true, ecmaVersion: "latest" });
43-
44-
tokens.forEach((token: any) => {
45-
const loc = token.loc.start;
46-
gen.addMapping({
47-
source: file,
48-
original: loc,
49-
generated: loc,
50-
});
51-
});
52-
53-
return JSON.parse(gen.toString());
28+
return rawSourceMap === "string" ? JSON.parse(rawSourceMap) : rawSourceMap;
5429
}
5530

5631
export default function (
5732
this: LoaderContext<Options>,
5833
source: string,
5934
sourceMap?: RawSourceMap
6035
) {
61-
let map = sourceMap ?? getInlineSourceMap.call(this, source);
36+
let map = sourceMap
37+
? sanitizeSourceMap(sourceMap)
38+
: getInlineSourceMap.call(this, source);
6239
const options = this.getOptions();
6340
const callback = this.async();
6441

@@ -70,26 +47,7 @@ export default function (
7047
// Instrument the code
7148
const instrumenter = options.instrumenter;
7249

73-
const combinedSourceMap = sanitizeSourceMap(sourceMap);
74-
75-
const code = instrumenter.instrumentSync(
76-
source,
77-
this.resourcePath,
78-
combinedSourceMap as any
79-
);
80-
81-
const identitySourceMap = sanitizeSourceMap(
82-
createIdentitySourceMap(this.resourcePath, source, {
83-
file: combinedSourceMap.file,
84-
sourceRoot: combinedSourceMap.sourceRoot,
85-
})
86-
);
87-
88-
instrumenter.instrumentSync(
89-
source,
90-
this.resourcePath,
91-
identitySourceMap as any
92-
);
50+
const code = instrumenter.instrumentSync(source, this.resourcePath, map);
9351

9452
const lastSourceMap = instrumenter.lastSourceMap();
9553

0 commit comments

Comments
 (0)