Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sourcemap generation for Webpack5 projects #42

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading