UNPKG

@storybook/addon-coverage

Version:

Tools to support code coverage in Storybook

40 lines (39 loc) 1.37 kB
// src/loader/webpack5-istanbul-loader.ts import { fromSource, fromMapFileSource } from "convert-source-map"; import fs from "fs"; import path from "path"; function sanitizeSourceMap(rawSourceMap) { return typeof rawSourceMap === "string" ? JSON.parse(rawSourceMap) : rawSourceMap; } function webpack5_istanbul_loader_default(source, sourceMap) { let map = sourceMap ? sanitizeSourceMap(sourceMap) : getInlineSourceMap.call(this, source); const options = this.getOptions(); const callback = this.async(); if (!map) { callback(null, source, sourceMap); return; } const instrumenter = options.instrumenter; const code = instrumenter.instrumentSync(source, this.resourcePath, map); const lastSourceMap = instrumenter.lastSourceMap(); callback(null, code, lastSourceMap); } function getInlineSourceMap(source) { try { const inlineSourceMap = fromSource(source) || fromMapFileSource(source, function(filename) { return fs.readFileSync(path.resolve(path.dirname(this.resourcePath), filename), "utf-8"); }); if (inlineSourceMap) { return inlineSourceMap.sourcemap; } } catch (e) { if (e instanceof Error && e.message.includes("An error occurred while trying to read the map file at")) { this.emitWarning(e); } else { throw e; } } } export { webpack5_istanbul_loader_default as default };