perf(compiler-sfc): use faster source map addMapping

This commit is contained in:
Evan You 2023-11-27 14:42:12 +08:00
parent d193666fdc
commit 50cde7cfbc
3 changed files with 9 additions and 10 deletions

View File

@ -227,7 +227,6 @@ function createCodegenContext(
// lazy require source-map implementation, only in non-browser builds // lazy require source-map implementation, only in non-browser builds
context.map = new SourceMapGenerator() context.map = new SourceMapGenerator()
context.map.setSourceContent(filename, context.source) context.map.setSourceContent(filename, context.source)
// @ts-ignore
context.map._sources.add(filename) context.map._sources.add(filename)
} }

View File

@ -339,22 +339,21 @@ function generateSourceMap(
sourceRoot: sourceRoot.replace(/\\/g, '/') sourceRoot: sourceRoot.replace(/\\/g, '/')
}) })
map.setSourceContent(filename, source) map.setSourceContent(filename, source)
map._sources.add(filename)
generated.split(splitRE).forEach((line, index) => { generated.split(splitRE).forEach((line, index) => {
if (!emptyRE.test(line)) { if (!emptyRE.test(line)) {
const originalLine = index + 1 + lineOffset const originalLine = index + 1 + lineOffset
const generatedLine = index + 1 const generatedLine = index + 1
for (let i = 0; i < line.length; i++) { for (let i = 0; i < line.length; i++) {
if (!/\s/.test(line[i])) { if (!/\s/.test(line[i])) {
map.addMapping({ map._mappings.add({
originalLine,
originalColumn: i,
generatedLine,
generatedColumn: i,
source: filename, source: filename,
original: { // @ts-ignore
line: originalLine, name: null
column: i
},
generated: {
line: generatedLine,
column: i
}
}) })
} }
} }

View File

@ -48,6 +48,7 @@ declare module 'source-map-js' {
export interface SourceMapGenerator { export interface SourceMapGenerator {
// SourceMapGenerator has this method but the types do not include it // SourceMapGenerator has this method but the types do not include it
toJSON(): RawSourceMap toJSON(): RawSourceMap
_sources: Set<string>
_names: Set<string> _names: Set<string>
_mappings: { _mappings: {
add(mapping: MappingItem): void add(mapping: MappingItem): void