@@ -2,9 +2,10 @@ var parse5 = require('parse5')
2
2
var parser = new parse5 . Parser ( null , { locationInfo : true } )
3
3
var cache = require ( 'lru-cache' ) ( 100 )
4
4
var SourceMapGenerator = require ( 'source-map' ) . SourceMapGenerator
5
- var splitRE = / \r ? \n / g
6
- var trimRE = / ^ ( \s | \r ? \n ) + /
7
5
var hash = require ( 'hash-sum' )
6
+ var deindent = require ( './deindent' )
7
+ var splitRE = / \r ? \n / g
8
+ var emptyRE = / ^ \s * $ /
8
9
9
10
module . exports = function ( content , filename ) {
10
11
@@ -78,16 +79,28 @@ module.exports = function (content, filename) {
78
79
}
79
80
}
80
81
82
+ // extract part
81
83
var start = node . childNodes [ 0 ] . __location . start
82
84
var end = node . childNodes [ node . childNodes . length - 1 ] . __location . end
83
- var result = content . slice ( start , end )
84
- var trimmedResult = result . replace ( trimRE , '' )
85
- var trimmed = trimmedResult . length - result . length
86
- var lineOffset = content . slice ( 0 , start + trimmed ) . split ( splitRE ) . length
85
+ var result
86
+ var lineOffset
87
+ if ( type === 'script' ) {
88
+ lineOffset = 0
89
+ // preserve other parts as commenets so that linters
90
+ // and babel can output correct line numbers in warnings
91
+ result =
92
+ commentScript ( content . slice ( 0 , start ) , lang ) +
93
+ deindent ( content . slice ( start , end ) ) +
94
+ commentScript ( content . slice ( end ) , lang )
95
+ } else {
96
+ lineOffset = content . slice ( 0 , start ) . split ( splitRE ) . length - 1
97
+ result = content . slice ( start , end )
98
+ }
99
+
100
+ // generate source map
87
101
var map = new SourceMapGenerator ( )
88
102
map . setSourceContent ( filenameWithHash , content )
89
-
90
- trimmedResult . split ( splitRE ) . forEach ( function ( line , index ) {
103
+ result . split ( splitRE ) . forEach ( function ( line , index ) {
91
104
map . addMapping ( {
92
105
source : filenameWithHash ,
93
106
original : {
@@ -101,10 +114,18 @@ module.exports = function (content, filename) {
101
114
} )
102
115
} )
103
116
117
+ // workaround for Webpack eval-source-map bug
118
+ // https://github.com/webpack/webpack/pull/1816
119
+ // in case the script was piped through another loader
120
+ // that doesn't pass down the source map properly.
121
+ if ( type === 'script' ) {
122
+ result += '\n/* generated by vue-loader */\n'
123
+ }
124
+
104
125
output [ type ] . push ( {
105
126
lang : lang ,
106
127
scoped : scoped ,
107
- content : trimmedResult ,
128
+ content : result ,
108
129
map : map . toJSON ( )
109
130
} )
110
131
} )
@@ -113,6 +134,28 @@ module.exports = function (content, filename) {
113
134
return output
114
135
}
115
136
137
+ function commentScript ( content , lang ) {
138
+ return content
139
+ . split ( splitRE )
140
+ . map ( function ( line ) {
141
+ if ( emptyRE . test ( line ) ) {
142
+ return line
143
+ }
144
+ switch ( lang ) {
145
+ case 'coffee' :
146
+ case 'coffee-jsx' :
147
+ case 'coffee-redux' :
148
+ return '# ' + line
149
+ case 'purs' :
150
+ case 'ulmus' :
151
+ return '-- ' + line
152
+ default :
153
+ return '// ' + line
154
+ }
155
+ } )
156
+ . join ( '\n' )
157
+ }
158
+
116
159
function getAttribute ( node , name ) {
117
160
if ( node . attrs ) {
118
161
var i = node . attrs . length
0 commit comments