Skip to content

Commit fa03c63

Browse files
committed
do not add mappings for empty/comment lines (fix module.exports source map bug)
1 parent 16832a6 commit fa03c63

File tree

4 files changed

+33
-26
lines changed

4 files changed

+33
-26
lines changed

lib/loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var defaultLang = {
1414
var defaultLoaders = {
1515
html: 'vue-html-loader',
1616
css: 'vue-style-loader!css-loader',
17-
js: 'babel-loader?presets[]=es2015&plugins[]=transform-runtime'
17+
js: 'babel-loader?presets[]=es2015&plugins[]=transform-runtime&comments=false'
1818
}
1919

2020
var rewriterInjectRE = /\b((css|(vue-)?html)(-loader)?(\?[^!]+)?!?)\b/

lib/parser.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,27 @@ module.exports = function (content, filename, needMap) {
106106
// generate source map
107107
map = new SourceMapGenerator()
108108
map.setSourceContent(filenameWithHash, content)
109+
110+
// do not add mappings for comment lines - babel's source map
111+
// somehow gets messed up because of it
112+
var isCommentLine = function (line) {
113+
return type === 'script' && !lang && line.indexOf('//') === 0
114+
}
115+
109116
result.split(splitRE).forEach(function (line, index) {
110-
map.addMapping({
111-
source: filenameWithHash,
112-
original: {
113-
line: index + 1 + lineOffset,
114-
column: 0
115-
},
116-
generated: {
117-
line: index + 1,
118-
column: 0
119-
}
120-
})
117+
if (!emptyRE.test(line) && !isCommentLine(line)) {
118+
map.addMapping({
119+
source: filenameWithHash,
120+
original: {
121+
line: index + 1 + lineOffset,
122+
column: 0
123+
},
124+
generated: {
125+
line: index + 1,
126+
column: 0
127+
}
128+
})
129+
}
121130
})
122131
// workaround for Webpack eval-source-map bug
123132
// https://github.com/webpack/webpack/pull/1816
@@ -145,19 +154,17 @@ function commentScript (content, lang) {
145154
return content
146155
.split(splitRE)
147156
.map(function (line) {
148-
if (emptyRE.test(line)) {
149-
return line
150-
}
157+
line = emptyRE.test(line) ? '' : ' ' + line
151158
switch (lang) {
152159
case 'coffee':
153160
case 'coffee-jsx':
154161
case 'coffee-redux':
155-
return '# ' + line
162+
return '#' + line
156163
case 'purs':
157164
case 'ulmus':
158-
return '-- ' + line
165+
return '--' + line
159166
default:
160-
return '// ' + line
167+
return '//' + line
161168
}
162169
})
163170
.join('\n')

test/fixtures/basic.vue

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
<style>
2-
comp-a h2 {
3-
color: #f00;
4-
}
5-
</style>
6-
71
<template>
82
<h2 class="red">{{msg}}</h2>
93
</template>
104

115
<script>
12-
export default {
6+
module.exports = {
137
data () {
148
return {
159
msg: 'Hello from Component A!'
1610
}
1711
}
1812
}
1913
</script>
14+
15+
<style>
16+
comp-a h2 {
17+
color: #f00;
18+
}
19+
</style>

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ describe('vue-loader', function () {
165165
column: col
166166
})
167167
expect(pos.source.indexOf('basic.vue') > -1)
168-
expect(pos.line).to.equal(15)
168+
expect(pos.line).to.equal(9)
169169
done()
170170
})
171171
})

0 commit comments

Comments
 (0)