Skip to content

Commit 713508b

Browse files
authored
Merge pull request #2091 from didi/fix-source-map-error
Fix source map error
2 parents 3ada3fd + f6f03bd commit 713508b

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

packages/webpack-plugin/lib/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = (content, { filePath, needMap, mode, env }) => {
1414
output = compiler.parseComponent(content, {
1515
mode,
1616
filePath,
17-
// pad: 'line', // stylus编译遇到大量空行时会出现栈溢出,故注释掉
17+
pad: 'line',
1818
env
1919
})
2020
if (needMap) {

packages/webpack-plugin/lib/script-setup-compiler/index.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const babylon = require('@babel/parser')
22
const MagicString = require('magic-string')
3+
const { SourceMapConsumer, SourceMapGenerator } = require('source-map')
34
const traverse = require('@babel/traverse').default
45
const t = require('@babel/types')
56
const formatCodeFrame = require('@babel/code-frame')
@@ -625,7 +626,12 @@ function compileScriptSetup (
625626
_s.appendRight(endOffset, '})')
626627

627628
return {
628-
content: _s.toString()
629+
content: _s.toString(),
630+
map: _s.generateMap({
631+
source: filePath,
632+
hires: true,
633+
includeContent: true
634+
})
629635
}
630636
}
631637

@@ -1165,14 +1171,30 @@ function getCtor (ctorType) {
11651171
return ctor
11661172
}
11671173

1168-
module.exports = function (content) {
1174+
module.exports = async function (content, sourceMap) {
11691175
const { queryObj } = parseRequest(this.resource)
11701176
const { ctorType, lang } = queryObj
11711177
const filePath = this.resourcePath
1172-
const { content: callbackContent } = compileScriptSetup({
1178+
const callback = this.async()
1179+
let finalSourceMap = null
1180+
const {
1181+
content: callbackContent,
1182+
map
1183+
} = compileScriptSetup({
11731184
content,
11741185
lang
11751186
}, ctorType, filePath)
1176-
1177-
this.callback(null, callbackContent)
1187+
finalSourceMap = map
1188+
if (sourceMap) {
1189+
const compiledMapConsumer = await new SourceMapConsumer(map)
1190+
const compiledMapGenerator = SourceMapGenerator.fromSourceMap(compiledMapConsumer)
1191+
1192+
const originalConsumer = await new SourceMapConsumer(sourceMap)
1193+
compiledMapGenerator.applySourceMap(
1194+
originalConsumer,
1195+
filePath // 需要确保与原始映射的source路径一致
1196+
)
1197+
finalSourceMap = compiledMapGenerator.toJSON()
1198+
}
1199+
callback(null, callbackContent, finalSourceMap)
11781200
}

packages/webpack-plugin/lib/template-compiler/compiler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,8 @@ function parseComponent (content, options) {
581581
let text = content.slice(currentBlock.start, currentBlock.end)
582582
// pad content so that linters and pre-processors can output correct
583583
// line numbers in errors and warnings
584-
if (options.pad) {
584+
// stylus编译遇到大量空行时会出现栈溢出,故针对stylus不走pad
585+
if (options.pad && !(currentBlock.tag === 'style' && currentBlock.lang === 'stylus')) {
585586
text = padContent(currentBlock, options.pad) + text
586587
}
587588
currentBlock.content = text

0 commit comments

Comments
 (0)