From 12cff9d55f0206a849815ef5633c29cd40834410 Mon Sep 17 00:00:00 2001 From: xuegan Date: Tue, 11 Feb 2025 18:38:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F&web=20sourcemap=E4=B8=8D=E5=87=86?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/webpack-plugin/lib/parser.js | 10 ++++++---- .../lib/template-compiler/compiler.js | 13 ++++++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/webpack-plugin/lib/parser.js b/packages/webpack-plugin/lib/parser.js index 97db82f730..8ff081c3e7 100644 --- a/packages/webpack-plugin/lib/parser.js +++ b/packages/webpack-plugin/lib/parser.js @@ -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) { @@ -34,7 +35,8 @@ module.exports = (content, { filePath, needMap, mode, env }) => { style.map = generateSourceMap( filename, content, - style.content + style.content, + output.script.offset ) } }) @@ -45,7 +47,7 @@ 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) => { @@ -53,7 +55,7 @@ function generateSourceMap (filename, source, generated) { map.addMapping({ source: filename, original: { - line: index + 1, + line: offset + index + 1, column: 0 }, generated: { diff --git a/packages/webpack-plugin/lib/template-compiler/compiler.js b/packages/webpack-plugin/lib/template-compiler/compiler.js index b8779b6933..dce889739f 100644 --- a/packages/webpack-plugin/lib/template-compiler/compiler.js +++ b/packages/webpack-plugin/lib/template-compiler/compiler.js @@ -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 @@ -580,8 +581,15 @@ 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 @@ -589,11 +597,10 @@ function parseComponent (content, options) { 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) }