Skip to content

Commit

Permalink
Merge pull request #42 from storybookjs/valentin/fix-source-maps-for-…
Browse files Browse the repository at this point in the history
…webpack5

Fix sourcemap generation for Webpack5 projects
  • Loading branch information
valentinpalkovic authored May 24, 2024
2 parents eecd87c + 041aa23 commit 0329397
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 48 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"convert-source-map": "^2.0.0",
"espree": "^9.6.1",
"istanbul-lib-instrument": "^6.0.1",
"source-map": "^0.7.4",
"test-exclude": "^6.0.0",
"vite-plugin-istanbul": "^3.0.1"
}
Expand Down
52 changes: 5 additions & 47 deletions src/loader/webpack5-istanbul-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as espree from "espree";
import fs from "fs";
import path from "path";
import { LoaderContext } from "webpack";
import { SourceMapGenerator, StartOfSourceMap } from "source-map";

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

Expand All @@ -26,39 +25,17 @@ type RawSourceMap = {
};

function sanitizeSourceMap(rawSourceMap: RawSourceMap | string): RawSourceMap {
if (typeof rawSourceMap === 'string') return JSON.parse(rawSourceMap);
const { sourcesContent, ...sourceMap } = rawSourceMap ?? {};

// JSON parse/stringify trick required for istanbul to accept the SourceMap
return JSON.parse(JSON.stringify(sourceMap));
}

function createIdentitySourceMap(
file: string,
source: string,
option: StartOfSourceMap
) {
const gen = new SourceMapGenerator(option);
const tokens = espree.tokenize(source, { loc: true, ecmaVersion: "latest" });

tokens.forEach((token: any) => {
const loc = token.loc.start;
gen.addMapping({
source: file,
original: loc,
generated: loc,
});
});

return JSON.parse(gen.toString());
return rawSourceMap === "string" ? JSON.parse(rawSourceMap) : rawSourceMap;
}

export default function (
this: LoaderContext<Options>,
source: string,
sourceMap?: RawSourceMap
) {
let map = sourceMap ?? getInlineSourceMap.call(this, source);
let map = sourceMap
? sanitizeSourceMap(sourceMap)
: getInlineSourceMap.call(this, source);
const options = this.getOptions();
const callback = this.async();

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

const combinedSourceMap = sanitizeSourceMap(sourceMap);

const code = instrumenter.instrumentSync(
source,
this.resourcePath,
combinedSourceMap as any
);

const identitySourceMap = sanitizeSourceMap(
createIdentitySourceMap(this.resourcePath, source, {
file: combinedSourceMap.file,
sourceRoot: combinedSourceMap.sourceRoot,
})
);

instrumenter.instrumentSync(
source,
this.resourcePath,
identitySourceMap as any
);
const code = instrumenter.instrumentSync(source, this.resourcePath, map);

const lastSourceMap = instrumenter.lastSourceMap();

Expand Down

0 comments on commit 0329397

Please sign in to comment.