1
1
module . exports = function ( environment ) {
2
2
3
+ /**
4
+ * @param source The code
5
+ * @param ignoredCharsCount Number of characters at the start of the file to ignore.
6
+ * @constructor
7
+ */
8
+ var FileInfo = function ( source , ignoredCharsCount ) {
9
+ this . ignoredCharsCount = ignoredCharsCount ;
10
+ this . source = source . slice ( ignoredCharsCount ) ;
11
+ this . sourceLines = this . source . split ( '\n' ) ;
12
+ } ;
13
+
14
+ /** Translate absolute source offset to line/column offset. */
15
+ FileInfo . prototype . getLocation = function ( index ) {
16
+ index = Math . max ( 0 , index - this . ignoredCharsCount ) ;
17
+ var line = 0 ;
18
+ for ( ; line < this . sourceLines . length && index >= this . sourceLines [ line ] . length + 1 ; line ++ ) {
19
+ index -= this . sourceLines [ line ] . length + 1 ; // +1 for the '\n' character
20
+ }
21
+ return { line : line + 1 , column : index } ;
22
+ } ;
23
+
3
24
var SourceMapOutput = function ( options ) {
4
25
this . _css = [ ] ;
5
26
this . _rootNode = options . rootNode ;
6
- this . _contentsMap = options . contentsMap ;
7
- this . _contentsIgnoredCharsMap = options . contentsIgnoredCharsMap ;
27
+
28
+ this . _contentsInfoMap = { } ;
29
+ for ( var key in options . contentsMap ) {
30
+ if ( options . contentsMap . hasOwnProperty ( key ) ) {
31
+ this . _contentsInfoMap [ key ] = new FileInfo (
32
+ options . contentsMap [ key ] , options . contentsIgnoredCharsMap [ key ] || 0 ) ;
33
+ }
34
+ }
35
+
8
36
if ( options . sourceMapFilename ) {
9
37
this . _sourceMapFilename = options . sourceMapFilename . replace ( / \\ / g, '/' ) ;
10
38
}
@@ -48,39 +76,22 @@ module.exports = function (environment) {
48
76
}
49
77
50
78
var lines ,
51
- sourceLines ,
52
79
columns ,
53
- sourceColumns ,
54
80
i ;
55
81
56
- if ( fileInfo ) {
57
- var inputSource = this . _contentsMap [ fileInfo . filename ] ;
58
-
59
- // remove vars/banner added to the top of the file
60
- if ( this . _contentsIgnoredCharsMap [ fileInfo . filename ] ) {
61
- // adjust the index
62
- index -= this . _contentsIgnoredCharsMap [ fileInfo . filename ] ;
63
- if ( index < 0 ) { index = 0 ; }
64
- // adjust the source
65
- inputSource = inputSource . slice ( this . _contentsIgnoredCharsMap [ fileInfo . filename ] ) ;
66
- }
67
- inputSource = inputSource . substring ( 0 , index ) ;
68
- sourceLines = inputSource . split ( "\n" ) ;
69
- sourceColumns = sourceLines [ sourceLines . length - 1 ] ;
70
- }
71
-
72
82
lines = chunk . split ( "\n" ) ;
73
83
columns = lines [ lines . length - 1 ] ;
74
84
75
85
if ( fileInfo ) {
86
+ var location = this . _contentsInfoMap [ fileInfo . filename ] . getLocation ( index ) ;
76
87
if ( ! mapLines ) {
77
88
this . _sourceMapGenerator . addMapping ( { generated : { line : this . _lineNumber + 1 , column : this . _column } ,
78
- original : { line : sourceLines . length , column : sourceColumns . length } ,
89
+ original : location ,
79
90
source : this . normalizeFilename ( fileInfo . filename ) } ) ;
80
91
} else {
81
92
for ( i = 0 ; i < lines . length ; i ++ ) {
82
93
this . _sourceMapGenerator . addMapping ( { generated : { line : this . _lineNumber + i + 1 , column : i === 0 ? this . _column : 0 } ,
83
- original : { line : sourceLines . length + i , column : i === 0 ? sourceColumns . length : 0 } ,
94
+ original : { line : location . line + i , column : i === 0 ? location . column : 0 } ,
84
95
source : this . normalizeFilename ( fileInfo . filename ) } ) ;
85
96
}
86
97
}
@@ -105,11 +116,8 @@ module.exports = function (environment) {
105
116
106
117
if ( this . _outputSourceFiles ) {
107
118
for ( var filename in this . _contentsMap ) {
108
- if ( this . _contentsMap . hasOwnProperty ( filename ) ) {
109
- var source = this . _contentsMap [ filename ] ;
110
- if ( this . _contentsIgnoredCharsMap [ filename ] ) {
111
- source = source . slice ( this . _contentsIgnoredCharsMap [ filename ] ) ;
112
- }
119
+ if ( this . _contentsInfoMap . hasOwnProperty ( filename ) ) {
120
+ var source = this . _contentsInfoMap [ filename ] . source ;
113
121
this . _sourceMapGenerator . setSourceContent ( this . normalizeFilename ( filename ) , source ) ;
114
122
}
115
123
}
0 commit comments