@@ -4,64 +4,65 @@ const libSourceMaps = require('istanbul-lib-source-maps')
4
4
const fs = require ( 'fs' )
5
5
const path = require ( 'path' )
6
6
7
- function SourceMaps ( opts ) {
8
- this . cache = opts . cache
9
- this . cacheDirectory = opts . cacheDirectory
10
- this . loadedMaps = { }
11
- this . _sourceMapCache = libSourceMaps . createSourceMapStore ( )
12
- }
7
+ class SourceMaps {
8
+ constructor ( opts ) {
9
+ this . cache = opts . cache
10
+ this . cacheDirectory = opts . cacheDirectory
11
+ this . loadedMaps = { }
12
+ this . _sourceMapCache = libSourceMaps . createSourceMapStore ( )
13
+ }
13
14
14
- SourceMaps . prototype . cachedPath = function ( source , hash ) {
15
- return path . join (
16
- this . cacheDirectory ,
17
- `${ path . parse ( source ) . name } -${ hash } .map`
18
- )
19
- }
15
+ cachedPath ( source , hash ) {
16
+ return path . join (
17
+ this . cacheDirectory ,
18
+ `${ path . parse ( source ) . name } -${ hash } .map`
19
+ )
20
+ }
20
21
21
- SourceMaps . prototype . purgeCache = function ( ) {
22
- this . _sourceMapCache = libSourceMaps . createSourceMapStore ( )
23
- this . loadedMaps = { }
24
- }
22
+ purgeCache ( ) {
23
+ this . _sourceMapCache = libSourceMaps . createSourceMapStore ( )
24
+ this . loadedMaps = { }
25
+ }
25
26
26
- SourceMaps . prototype . extractAndRegister = function ( code , filename , hash ) {
27
- var sourceMap = convertSourceMap . fromSource ( code ) || convertSourceMap . fromMapFileSource ( code , path . dirname ( filename ) )
28
- if ( sourceMap ) {
29
- if ( this . cache && hash ) {
30
- const mapPath = this . cachedPath ( filename , hash )
31
- fs . writeFileSync ( mapPath , sourceMap . toJSON ( ) )
32
- } else {
33
- this . _sourceMapCache . registerMap ( filename , sourceMap . sourcemap )
27
+ extractAndRegister ( code , filename , hash ) {
28
+ const sourceMap = convertSourceMap . fromSource ( code ) || convertSourceMap . fromMapFileSource ( code , path . dirname ( filename ) )
29
+ if ( sourceMap ) {
30
+ if ( this . cache && hash ) {
31
+ const mapPath = this . cachedPath ( filename , hash )
32
+ fs . writeFileSync ( mapPath , sourceMap . toJSON ( ) )
33
+ } else {
34
+ this . _sourceMapCache . registerMap ( filename , sourceMap . sourcemap )
35
+ }
34
36
}
37
+ return sourceMap
35
38
}
36
- return sourceMap
37
- }
38
39
39
- SourceMaps . prototype . remapCoverage = function ( obj ) {
40
- var transformed = this . _sourceMapCache . transformCoverage (
41
- libCoverage . createCoverageMap ( obj )
42
- )
43
- return transformed . map . data
44
- }
40
+ remapCoverage ( obj ) {
41
+ const transformed = this . _sourceMapCache . transformCoverage (
42
+ libCoverage . createCoverageMap ( obj )
43
+ )
44
+ return transformed . map . data
45
+ }
45
46
46
- SourceMaps . prototype . reloadCachedSourceMaps = function ( report ) {
47
- Object . keys ( report ) . forEach ( ( absFile ) => {
48
- var fileReport = report [ absFile ]
49
- if ( fileReport && fileReport . contentHash ) {
50
- var hash = fileReport . contentHash
51
- if ( ! ( hash in this . loadedMaps ) ) {
52
- try {
53
- const mapPath = this . cachedPath ( absFile , hash )
54
- this . loadedMaps [ hash ] = JSON . parse ( fs . readFileSync ( mapPath , 'utf8' ) )
55
- } catch ( e ) {
56
- // set to false to avoid repeatedly trying to load the map
57
- this . loadedMaps [ hash ] = false
47
+ reloadCachedSourceMaps ( report ) {
48
+ Object . entries ( report ) . forEach ( ( [ absFile , fileReport ] ) => {
49
+ if ( fileReport && fileReport . contentHash ) {
50
+ const hash = fileReport . contentHash
51
+ if ( ! ( hash in this . loadedMaps ) ) {
52
+ try {
53
+ const mapPath = this . cachedPath ( absFile , hash )
54
+ this . loadedMaps [ hash ] = JSON . parse ( fs . readFileSync ( mapPath , 'utf8' ) )
55
+ } catch ( e ) {
56
+ // set to false to avoid repeatedly trying to load the map
57
+ this . loadedMaps [ hash ] = false
58
+ }
59
+ }
60
+ if ( this . loadedMaps [ hash ] ) {
61
+ this . _sourceMapCache . registerMap ( absFile , this . loadedMaps [ hash ] )
58
62
}
59
63
}
60
- if ( this . loadedMaps [ hash ] ) {
61
- this . _sourceMapCache . registerMap ( absFile , this . loadedMaps [ hash ] )
62
- }
63
- }
64
- } )
64
+ } )
65
+ }
65
66
}
66
67
67
68
module . exports = SourceMaps
0 commit comments