Skip to content

Commit f15d2f6

Browse files
committedNov 27, 2023
perf: avoid sfc source map unnecessary serialization and parsing
1 parent 157cfcb commit f15d2f6

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed
 

‎packages/compiler-core/src/codegen.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ function createCodegenContext(
207207
}
208208

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

‎packages/compiler-sfc/src/parse.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ function generateSourceMap(
360360
}
361361
}
362362
})
363-
return JSON.parse(map.toString())
363+
return map.toJSON()
364364
}
365365

366366
function padContent(

‎packages/compiler-sfc/src/style/preprocessors.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ const scss: StylePreprocessor = (source, map, options, load = require) => {
3838
if (map) {
3939
return {
4040
code: result.css.toString(),
41-
map: merge(map, JSON.parse(result.map.toString())),
41+
map: merge(
42+
map,
43+
result.map.toJSON
44+
? result.map.toJSON()
45+
: JSON.parse(result.map.toString())
46+
),
4247
errors: [],
4348
dependencies
4449
}

‎packages/global.d.ts

+18
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ declare module 'estree-walker' {
4444
)
4545
}
4646

47+
declare module 'source-map-js' {
48+
export interface SourceMapGenerator {
49+
// SourceMapGenerator has this method but the types do not include it
50+
toJSON(): RawSourceMap
51+
_names: Set<string>
52+
_mappings: {
53+
add(mapping: {
54+
originalLine: number
55+
originalColumn: number
56+
generatedLine: number
57+
generatedColumn: number
58+
source: string
59+
name: string | null
60+
}): void
61+
}
62+
}
63+
}
64+
4765
declare interface String {
4866
/**
4967
* @deprecated Please use String.prototype.slice instead of String.prototype.substring in the repository.

0 commit comments

Comments
 (0)
Please sign in to comment.