1
1
import path from "path" ;
2
2
import { OutputChunk } from "rollup" ;
3
+ import type { RawSourceMap } from "source-map" ;
3
4
import { SourceMapConsumer } from "source-map" ;
4
5
5
6
interface SourceMapModuleRenderInfo {
@@ -17,7 +18,8 @@ const getBytesPerFileUsingSourceMap = (
17
18
18
19
let line = 1 ;
19
20
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 ++ ) {
21
23
const { source } = map . originalPositionFor ( {
22
24
line,
23
25
column,
@@ -26,7 +28,7 @@ const getBytesPerFileUsingSourceMap = (
26
28
const id = path . resolve ( path . dirname ( path . join ( dir , bundleId ) ) , source ) ;
27
29
28
30
modules [ id ] = modules [ id ] || { id, renderedLength : 0 } ;
29
- modules [ id ] . renderedLength += 1 ;
31
+ modules [ id ] . renderedLength += Buffer . byteLength ( codeChars [ i ] ) ;
30
32
}
31
33
32
34
if ( code [ i ] === "\n" ) {
@@ -43,10 +45,10 @@ export const getSourcemapModules = (
43
45
outputChunk : OutputChunk ,
44
46
dir : string
45
47
) : Promise < Record < string , SourceMapModuleRenderInfo > > => {
46
- if ( ! outputChunk . map ) {
48
+ if ( outputChunk . map == null ) {
47
49
return Promise . resolve ( { } ) ;
48
50
}
49
- return SourceMapConsumer . with ( outputChunk . map , null , ( map ) => {
51
+ return SourceMapConsumer . with ( outputChunk . map as RawSourceMap , null , ( map ) => {
50
52
return getBytesPerFileUsingSourceMap ( id , outputChunk . code , map , dir ) ;
51
53
} ) ;
52
54
} ;
0 commit comments