8000 proper source map support · rspack-contrib/rspack-vue-loader@23aee81 · GitHub
[go: up one dir, main page]

Skip to content

Commit 23aee81

Browse files
committed
proper source map support
1 parent c75837c commit 23aee81

File tree

4 files changed

+35
-44
lines changed

4 files changed

+35
-44
lines changed

lib/loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ module.exports = function (content) {
130130
}
131131
}
132132

133-
var parts = parse(content)
133+
var parts = parse(content, fileName)
134134
var hasLocalStyles = false
135135
var output = 'var __vue_script__, __vue_template__\n'
136136

lib/parser.js

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
var parse5 = require('parse5')
22
var parser = new parse5.Parser(null, { locationInfo: true })
33
var cache = require('lru-cache')(100)
4+
var SourceNode = require('source-map').SourceNode
5+
var SourceMapGenerator = require('source-map').SourceMapGenerator
6+
var splitRE = /\n\r|\n|\r/g
47

5-
module.exports = function (content) {
8+
module.exports = function (content, filename) {
69

7-
var output = cache.get(content)
10+
var cacheKey = filename + 'XXX' + content
11+
var output = cache.get(cacheKey)
812
if (output) return output
913

1014
output = {
@@ -73,54 +77,37 @@ module.exports = function (content) {
7377

7478
var start = node.childNodes[0].__location.start
7579
var end = node.childNodes[node.childNodes.length - 1].__location.end
76-
77-
var result
78-
if (type === 'script') {
79-
result =
80-
commentScript(content.slice(0, start), lang) +
81-
content.slice(start, end) +
82-
commentScript(content.slice(end), lang) +
83-
// workaround for Webpack eval-source-map bug
84-
// https://github.com/webpack/webpack/pull/1816
85-
'\n/* generated by vue-loader */\n'
86-
} else {
87-
result = content.slice(start, end).trim()
88-
}
80+
var result = content.slice(start, end)
81+
var lineOffset = content.slice(0, start).split(splitRE).length - 1
82+
var map = new SourceMapGenerator()
83+
map.setSourceContent(filename, content)
84+
85+
result.split(splitRE).forEach(function (line, index) {
86+
map.addMapping({
87+
source: filename,
88+
original: {
89+
line: index + 1 + lineOffset,
90+
column: 0
91+
},
92+
generated: {
93+
line: index + 1,
94+
column: 0
95+
}
96+
})
97+
})
8998

9099
output[type].push({
91100
lang: lang,
92101
scoped: scoped,
93-
content: result
102+
content: result,
103+
map: map.toJSON()
94104
})
95105
})
96106

97-
cache.set(content, output)
107+
cache.set(cacheKey, output)
98108
return output
99109
}
100110

101-
function commentScript (content, lang) {
102-
return content
103-
.split(/\n\r|\n|\r/g)
104-
.map(function (line) {
105-
if (line.trim() === '') {
106-
return line
107-
}
108-
109-
switch (lang) {
110-
case 'coffee':
111-
case 'coffee-jsx':
112-
case 'coffee-redux':
113-
return '# ' + line
114-
case 'purs':
115-
case 'ulmus':
116-
return '-- ' + line
117-
default:
118-
return '// ' + line
119-
}
120-
})
121-
.join('\n')
122-
}
123-
124111
function getAttribute (node, name) {
125112
if (node.attrs) {
126113
var i = node.attrs.length

lib/selector.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
var path = require('path')
12
var parse = require('./parser')
23
var loaderUtils = require('loader-utils')
34

45
module.exports = function (content) {
56
this.cacheable()
67
var query = loaderUtils.parseQuery(this.query)
7-
var parts = parse(content)
8-
return parts[query.type][query.index].content
8+
var filename = path.basename(this.resourcePath)
9+
var parts = parse(content, filename)
10+
var part = parts[query.type][query.index]
11+
this.callback(null, part.content, part.map)
912
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"object-assign": "^4.0.0",
3737
"parse5": "^1.5.0",
3838
"postcss": "^5.0.10",
39-
"postcss-selector-parser": "^1.1.2"
39+
"postcss-selector-parser": "^1.1.2",
40+
"source-map": "^0.5.3"
4041
},
4142
"peerDependencies": {
4243
"vue-html-loader": "^1.0.0",

0 commit comments

Comments
 (0)
0