Skip to content

Commit

Permalink
perf: avoid sfc source map unnecessary serialization and parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Nov 27, 2023
1 parent 157cfcb commit f15d2f6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
7 changes: 3 additions & 4 deletions packages/compiler-core/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ function createCodegenContext(
}

function addMapping(loc: Position, name: string | null = null) {
// @ts-ignore we use the private property to directly add the mapping
// we use the private property to directly add the mapping
// because the addMapping() implementation in source-map-js has a bunch of
// unnecessary arg and validation checks that are pure overhead in our case.
const { _names, _mappings } = context.map
const { _names, _mappings } = context.map!
if (name !== null && !_names.has(name)) _names.add(name)
_mappings.add({
originalLine: loc.line,
Expand Down Expand Up @@ -354,8 +354,7 @@ export function generate(
ast,
code: context.code,
preamble: isSetupInlined ? preambleContext.code : ``,
// SourceMapGenerator does have toJSON() method but it's not in the types
map: context.map ? (context.map as any).toJSON() : undefined
map: context.map ? context.map.toJSON() : undefined
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ function generateSourceMap(
}
}
})
return JSON.parse(map.toString())
return map.toJSON()
}

function padContent(
Expand Down
7 changes: 6 additions & 1 deletion packages/compiler-sfc/src/style/preprocessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ const scss: StylePreprocessor = (source, map, options, load = require) => {
if (map) {
return {
code: result.css.toString(),
map: merge(map, JSON.parse(result.map.toString())),
map: merge(
map,
result.map.toJSON
? result.map.toJSON()
: JSON.parse(result.map.toString())
),
errors: [],
dependencies
}
Expand Down
18 changes: 18 additions & 0 deletions packages/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ declare module 'estree-walker' {
)
}

declare module 'source-map-js' {
export interface SourceMapGenerator {
// SourceMapGenerator has this method but the types do not include it
toJSON(): RawSourceMap
_names: Set<string>
_mappings: {
add(mapping: {
originalLine: number
originalColumn: number
generatedLine: number
generatedColumn: number
source: string
name: string | null
}): void
}
}
}

declare interface String {
/**
* @deprecated Please use String.prototype.slice instead of String.prototype.substring in the repository.
Expand Down

0 comments on commit f15d2f6

Please sign in to comment.