Skip to content

Commit

Permalink
fix: 修复输出小程序&web sourcemap不准问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackgan3 committed Feb 11, 2025
1 parent 296482b commit 12cff9d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
10 changes: 6 additions & 4 deletions packages/webpack-plugin/lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ module.exports = (content, { filePath, needMap, mode, env }) => {
output.script.map = generateSourceMap(
filename,
content,
output.script.content
output.script.content,
output.script.offset
)
}
if (output.styles) {
Expand All @@ -34,7 +35,8 @@ module.exports = (content, { filePath, needMap, mode, env }) => {
style.map = generateSourceMap(
filename,
content,
style.content
style.content,
output.script.offset
)
}
})
Expand All @@ -45,15 +47,15 @@ module.exports = (content, { filePath, needMap, mode, env }) => {
return output
}

function generateSourceMap (filename, source, generated) {
function generateSourceMap (filename, source, generated, offset = 0) {
const map = new SourceMapGenerator()
map.setSourceContent(filename, source)
generated.split(splitRE).forEach((line, index) => {
if (!emptyRE.test(line)) {
map.addMapping({
source: filename,
original: {
line: index + 1,
line: offset + index + 1,
column: 0
},
generated: {
Expand Down
13 changes: 10 additions & 3 deletions packages/webpack-plugin/lib/template-compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ function parseComponent (content, options) {
tag,
content: '',
start: end,
offset: 0,
attrs: attrs.reduce(function (cumulated, ref) {
const name = ref.name
const value = ref.value
Expand Down Expand Up @@ -580,20 +581,26 @@ function parseComponent (content, options) {
let text = content.slice(currentBlock.start, currentBlock.end)
// pad content so that linters and pre-processors can output correct
// line numbers in errors and warnings
let offset = content.slice(0, currentBlock.start).split(splitRE).length
if (options.pad) {
text = padContent(currentBlock, options.pad) + text
text = padContent(currentBlock, options.pad, offset) + text
}
if (options.pad !== 'line') {
if (text[0] === '\n') {
offset-- // 去掉换行符影响
}
currentBlock.offset = offset
}
currentBlock.content = text
currentBlock = null
}
depth--
}

function padContent (block, pad) {
function padContent (block, pad, offset) {
if (pad === 'space') {
return content.slice(0, block.start).replace(replaceRE, ' ')
} else {
const offset = content.slice(0, block.start).split(splitRE).length
const padChar = '\n'
return Array(offset).join(padChar)
}
Expand Down

0 comments on commit 12cff9d

Please sign in to comment.