Skip to content

Commit

Permalink
fix(x-android): 修复 sourcemap
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed Jul 19, 2024
1 parent d84c929 commit 991cd98
Showing 1 changed file with 102 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import type {
Statement,
} from '@babel/types'
import { walk } from 'estree-walker'
import type { RawSourceMap } from 'source-map-js'
import {
type RawSourceMap,
SourceMapConsumer,
SourceMapGenerator,
} from 'source-map-js'
import { processNormalScript, processTemplate } from './script/normalScript'
import type { SFCTemplateCompileOptions } from '@vue/compiler-sfc'
import { warnOnce } from './warn'
Expand Down Expand Up @@ -919,26 +923,7 @@ __ins.emit(event, ...do_not_transform_spread)
if (destructureElements.length) {
args += `, { ${destructureElements.join(', ')} }: SetupContext`
}

// 10. generate return statement
// 剩余由 rust 编译器处理
if (options.componentType !== 'app') {
const { code, importsCode, preamble } = processTemplate(sfc, {
relativeFilename,
bindingMetadata: ctx.bindingMetadata,
rootDir: options.root,
className: options.className,
})
if (importsCode) {
ctx.s.prepend(importsCode)
}
if (preamble) {
ctx.s.prepend(preamble)
}
ctx.s.appendRight(endOffset, `\nreturn ${code}\n}\n\n`)
// TODO sourceMap
}
// 11. finalize default export
// 10. finalize default export
const genDefaultAs = options.genDefaultAs
? `const ${options.genDefaultAs} =`
: `export default`
Expand Down Expand Up @@ -1003,7 +988,54 @@ const _ctx = __ins.proxy;
const _cache = __ins.renderCache;
${exposeCall}`
)
ctx.s.appendRight(endOffset, `})`)

let scriptMap: RawSourceMap | undefined
// 11. generate return statement
// 剩余由 rust 编译器处理
if (options.componentType !== 'app') {
const { code, importsCode, preamble, map } = processTemplate(sfc, {
relativeFilename,
bindingMetadata: ctx.bindingMetadata,
rootDir: options.root,
className: options.className,
})
if (importsCode) {
ctx.s.prepend(importsCode)
}
if (preamble) {
ctx.s.prepend(preamble)
}

// 放到最后,以免查找 offset 有问题
let offset = map ? (ctx.s.toString().match(/\r?\n/g)?.length ?? 0) + 1 : 1
if (options.genDefaultAs) {
offset = offset - 2 // 排除 export default __sfc__
}
ctx.s.appendRight(endOffset, `\nreturn ${code}\n}\n\n})`)
ctx.s.trim()
scriptMap =
options.sourceMap !== false
? (ctx.s.generateMap({
source: relativeFilename,
hires: true,
includeContent: true,
}) as unknown as RawSourceMap)
: undefined
if (map && scriptMap) {
scriptMap = generateScriptMap(offset, map, scriptMap)
}
} else {
ctx.s.appendRight(endOffset, `})`)
ctx.s.trim()
scriptMap =
options.sourceMap !== false
? (ctx.s.generateMap({
source: relativeFilename,
hires: true,
includeContent: true,
}) as unknown as RawSourceMap)
: undefined
}

// 12. finalize Vue helper imports
// if (ctx.helperImports.size > 0) {
Expand All @@ -1014,27 +1046,65 @@ ${exposeCall}`
// )
// }

ctx.s.trim()

return {
...scriptSetup,
bindings: ctx.bindingMetadata,
imports: ctx.userImports,
content: ctx.s.toString(),
map:
options.sourceMap !== false
? (ctx.s.generateMap({
source: relativeFilename,
hires: true,
includeContent: true,
}) as unknown as RawSourceMap)
: undefined,
map: scriptMap,
scriptAst: scriptAst?.body,
scriptSetupAst: scriptSetupAst?.body,
deps: ctx.deps ? [...ctx.deps] : undefined,
}
}

function generateScriptMap(
offset: number,
templateMap: RawSourceMap,
scriptMap: RawSourceMap
): RawSourceMap {
const templateMapConsumer = new SourceMapConsumer(templateMap)
const scriptMapConsumer = new SourceMapConsumer(scriptMap)
const scriptMapGenerator = new SourceMapGenerator()
scriptMapConsumer.eachMapping((m) => {
scriptMapGenerator.addMapping({
original: {
line: m.originalLine,
column: m.originalColumn,
},
generated: {
line: m.generatedLine,
column: m.generatedColumn,
},
source: m.source,
name: m.name,
})
})
templateMapConsumer.eachMapping((m) => {
scriptMapGenerator.addMapping({
original: {
line: m.originalLine,
column: m.originalColumn,
},
generated: {
line: m.generatedLine + offset,
column: m.generatedColumn,
},
source: m.source,
name: m.name,
})
})

scriptMap.sources.forEach(function (sourceFile) {
const sourceContent = scriptMapConsumer.sourceContentFor(sourceFile)
if (sourceContent != null) {
scriptMapGenerator.setSourceContent(sourceFile, sourceContent)
}
})

return JSON.parse(scriptMapGenerator.toString())
}

function registerBinding(
bindings: Record<string, BindingTypes>,
node: Identifier,
Expand Down

0 comments on commit 991cd98

Please sign in to comment.