-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathsourcemap.ts
More file actions
58 lines (50 loc) · 1.44 KB
/
Copy pathsourcemap.ts
File metadata and controls
58 lines (50 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import path from "node:path";
import type { OutputChunk } from "./rollup-types.js";
import type { RawSourceMap } from "source-map";
import { SourceMapConsumer } from "source-map";
interface SourceMapModuleRenderInfo {
id: string;
renderedLength: number;
code: string[];
}
const getBytesPerFileUsingSourceMap = (
bundleId: string,
code: string,
map: SourceMapConsumer,
dir: string,
) => {
const modules: Record<string, SourceMapModuleRenderInfo> = {};
let line = 1;
let column = 0;
const codeChars = [...code];
for (let i = 0; i < codeChars.length; i++, column++) {
const { source } = map.originalPositionFor({
line,
column,
});
if (source != null) {
const id = path.resolve(path.dirname(path.join(dir, bundleId)), source);
const char = codeChars[i];
modules[id] = modules[id] || { id, renderedLength: 0, code: [] };
modules[id].renderedLength += Buffer.byteLength(char);
modules[id].code.push(char);
}
if (code[i] === "\n") {
line += 1;
column = -1;
}
}
return modules;
};
export const getSourcemapModules = (
id: string,
outputChunk: OutputChunk,
dir: string,
): Promise<Record<string, SourceMapModuleRenderInfo>> => {
if (outputChunk.map == null) {
return Promise.resolve({});
}
return SourceMapConsumer.with(outputChunk.map as RawSourceMap, null, (map) => {
return getBytesPerFileUsingSourceMap(id, outputChunk.code, map, dir);
});
};