Skip to content

Commit 911efa2

Browse files
committed
move source map logic into vue-loader
1 parent 60703ba commit 911efa2

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

lib/parser.js

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,59 @@
11
var compiler = require('vue-template-compiler')
22
var cache = require('lru-cache')(100)
33
var hash = require('hash-sum')
4+
var SourceMapGenerator = require('source-map').SourceMapGenerator
5+
6+
var splitRE = /\r?\n/g
7+
var emptyRE = /^(?:\/\/)?\s*$/
48

59
module.exports = function (content, filename, needMap) {
610
var cacheKey = hash(filename + content)
711
// source-map cache busting for hot-reloadded modules
812
var filenameWithHash = filename + '?' + cacheKey
913
var output = cache.get(cacheKey)
1014
if (output) return output
11-
output = compiler.parseComponent(content, {
12-
pad: true,
13-
map: needMap
14-
? { filename: filenameWithHash }
15-
: false
16-
})
15+
output = compiler.parseComponent(content, { pad: true })
16+
if (needMap) {
17+
if (output.script && !output.script.src) {
18+
output.script.map = generateSourceMap(
19+
filenameWithHash,
20+
content,
21+
output.script.content
22+
)
23+
}
24+
if (output.styles) {
25+
output.styles.forEach(style => {
26+
if (!style.src) {
27+
style.map = generateSourceMap(
28+
filenameWithHash,
29+
content,
30+
style.content
31+
)
32+
}
33+
})
34+
}
35+
}
1736
cache.set(cacheKey, output)
1837
return output
1938
}
39+
40+
function generateSourceMap (filename, source, generated) {
41+
var map = new SourceMapGenerator()
42+
map.setSourceContent(filename, source)
43+
generated.split(splitRE).forEach((line, index) => {
44+
if (!emptyRE.test(line)) {
45+
map.addMapping({
46+
source: filename,
47+
original: {
48+
line: index + 1,
49+
column: 0
50+
},
51+
generated: {
52+
line: index + 1,
53+
column: 0
54+
}
55+
})
56+
}
57+
})
58+
return map.toJSON()
59+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"object-assign": "^4.0.0",
3737
"postcss": "^5.0.10",
3838
"postcss-selector-parser": "^2.0.0",
39+
"source-map": "^0.5.6",
3940
"vue-hot-reload-api": "^2.0.1",
4041
"vue-style-loader": "^1.0.0",
4142
"vue-template-compiler": "^2.0.0-alpha.2"

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe('vue-loader', function () {
157157
it('source map', function (done) {
158158
var config = Object.assign({}, globalConfig, {
159159
entry: './test/fixtures/basic.vue',
160-
devtool: 'source-map'
160+
devtool: '#source-map'
161161
})
162162
bundle(config, function (code) {
163163
var map = mfs.readFileSync('/test.build.js.map').toString()

0 commit comments

Comments
 (0)