Skip to content

Commit d32ac75

Browse files
committed
backport comment fix to 7.x
1 parent db7737e commit d32ac75

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

lib/parser.js

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ var hash = require('hash-sum')
66
var deindent = require('./deindent')
77
var splitRE = /\r?\n/g
88
var emptyRE = /^\s*$/
9+
var commentSymbols = {
10+
'coffee': '#',
11+
'coffee-jsx': '#',
12+
'coffee-redux': '#',
13+
'purs': '--',
14+
'ulmus': '--'
15+
}
916

1017
module.exports = function (content, filename) {
1118

@@ -97,36 +104,47 @@ module.exports = function (content, filename) {
97104
result = content.slice(start, end)
98105
}
99106

100-
// generate source map
101-
var map = new SourceMapGenerator()
102-
map.setSourceContent(filenameWithHash, content)
103-
result.split(splitRE).forEach(function (line, index) {
104-
map.addMapping({
105-
source: filenameWithHash,
106-
original: {
107-
line: index + 1 + lineOffset,
108-
column: 0
109-
},
110-
generated: {
111-
line: index + 1,
112-
column: 0
107+
if (needMap) {
108+
// generate source map
109+
var map = new SourceMapGenerator()
110+
map.setSourceContent(filenameWithHash, content)
111+
112+
// do not add mappings for comment lines - babel's source map
113+
// somehow gets messed up because of it
114+
var isCommentLine = function (line) {
115+
return type === 'script' &&
116+
line.indexOf(getCommentSymbol(lang)) === 0
117+
}
118+
119+
result.split(splitRE).forEach(function (line, index) {
120+
if (!emptyRE.test(line) && !isCommentLine(line)) {
121+
map.addMapping({
122+
source: filenameWithHash,
123+
original: {
124+
line: index + 1 + lineOffset,
125+
column: 0
126+
},
127+
generated: {
128+
line: index + 1,
129+
column: 0
130+
}
131+
})
113132
}
114133
})
115-
})
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' && !lang) {
122-
result += '\n/* generated by vue-loader */\n'
134+
// workaround for Webpack eval-source-map bug
135+
// https://github.com/webpack/webpack/pull/1816
136+
// in case the script was piped through another loader
137+
// that doesn't pass down the source map properly.
138+
if (type === 'script' && !lang) {
139+
result += '\n/* generated by vue-loader */\n'
140+
}
123141
}
124142

125143
output[type].push({
126144
lang: lang,
127145
scoped: scoped,
128146
content: result,
129-
map: map.toJSON()
147+
map: map && map.toJSON()
130148
})
131149
})
132150

@@ -135,27 +153,19 @@ module.exports = function (content, filename) {
135153
}
136154

137155
function commentScript (content, lang) {
156+
var symbol = getCommentSymbol(lang)
138157
return content
139158
.split(splitRE)
140159
.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-
}
160+
return symbol + (emptyRE.test(line) ? '' : ' ' + line)
155161
})
156162
.join('\n')
157163
}
158164

165+
function getCommentSymbol (lang) {
166+
return commentSymbols[lang] || '//'
167+
}
168+
159169
function getAttribute (node, name) {
160170
if (node.attrs) {
161171
var i = node.attrs.length

0 commit comments

Comments
 (0)