Skip to content

Commit 442517c

Browse files
author
Denis Bardadym
committed
Change a way how sourcemap is traversed
1 parent 3ebec96 commit 442517c

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

plugin/sourcemap.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from "path";
22
import { OutputChunk } from "rollup";
3+
import type { RawSourceMap } from "source-map";
34
import { SourceMapConsumer } from "source-map";
45

56
interface SourceMapModuleRenderInfo {
@@ -17,7 +18,8 @@ const getBytesPerFileUsingSourceMap = (
1718

1819
let line = 1;
1920
let column = 0;
20-
for (let i = 0; i < code.length; i++, column++) {
21+
const codeChars = [...code];
22+
for (let i = 0; i < codeChars.length; i++, column++) {
2123
const { source } = map.originalPositionFor({
2224
line,
2325
column,
@@ -26,7 +28,7 @@ const getBytesPerFileUsingSourceMap = (
2628
const id = path.resolve(path.dirname(path.join(dir, bundleId)), source);
2729

2830
modules[id] = modules[id] || { id, renderedLength: 0 };
29-
modules[id].renderedLength += 1;
31+
modules[id].renderedLength += Buffer.byteLength(codeChars[i]);
3032
}
3133

3234
if (code[i] === "\n") {
@@ -43,10 +45,10 @@ export const getSourcemapModules = (
4345
outputChunk: OutputChunk,
4446
dir: string
4547
): Promise<Record<string, SourceMapModuleRenderInfo>> => {
46-
if (!outputChunk.map) {
48+
if (outputChunk.map == null) {
4749
return Promise.resolve({});
4850
}
49-
return SourceMapConsumer.with(outputChunk.map, null, (map) => {
51+
return SourceMapConsumer.with(outputChunk.map as RawSourceMap, null, (map) => {
5052
return getBytesPerFileUsingSourceMap(id, outputChunk.code, map, dir);
5153
});
5254
};

0 commit comments

Comments
 (0)