Skip to content

Commit

Permalink
chore(plugin-vue): replace source-map with gen/trace-mapping (#8155)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored May 13, 2022
1 parent be1d42c commit 5713a87
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/plugin-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
"vue": "^3.2.25"
},
"devDependencies": {
"@jridgewell/gen-mapping": "^0.3.1",
"@jridgewell/trace-mapping": "^0.3.10",
"debug": "^4.3.4",
"rollup": "^2.72.1",
"slash": "^4.0.0",
Expand Down
28 changes: 21 additions & 7 deletions packages/plugin-vue/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import type { SFCBlock, SFCDescriptor } from 'vue/compiler-sfc'
import type { PluginContext, SourceMap, TransformPluginContext } from 'rollup'
import { normalizePath } from '@rollup/pluginutils'
import type { RawSourceMap } from 'source-map'
import { SourceMapConsumer, SourceMapGenerator } from 'source-map'
import type { EncodedSourceMap as TraceEncodedSourceMap } from '@jridgewell/trace-mapping'
import { TraceMap, eachMapping } from '@jridgewell/trace-mapping'
import type { EncodedSourceMap as GenEncodedSourceMap } from '@jridgewell/gen-mapping'
import { addMapping, fromMap, toEncodedMap } from '@jridgewell/gen-mapping'
import { transformWithEsbuild } from 'vite'
import {
createDescriptor,
Expand Down Expand Up @@ -158,13 +161,19 @@ export async function transformMain(
// of templateMap, we need to concatenate the two source maps.
let resolvedMap = options.sourceMap ? map : undefined
if (resolvedMap && templateMap) {
const generator = SourceMapGenerator.fromSourceMap(
new SourceMapConsumer(map)
const gen = fromMap(
// version property of result.map is declared as string
// but actually it is `3`
map as Omit<RawSourceMap, 'version'> as TraceEncodedSourceMap
)
const tracer = new TraceMap(
// same above
templateMap as Omit<RawSourceMap, 'version'> as TraceEncodedSourceMap
)
const offset = (scriptCode.match(/\r?\n/g)?.length ?? 0) + 1
const templateMapConsumer = new SourceMapConsumer(templateMap)
templateMapConsumer.eachMapping((m) => {
generator.addMapping({
eachMapping(tracer, (m) => {
if (m.source == null) return
addMapping(gen, {
source: m.source,
original: { line: m.originalLine, column: m.originalColumn },
generated: {
Expand All @@ -173,7 +182,12 @@ export async function transformMain(
}
})
})
resolvedMap = (generator as any).toJSON() as RawSourceMap

// same above
resolvedMap = toEncodedMap(gen) as Omit<
GenEncodedSourceMap,
'version'
> as RawSourceMap
// if this is a template only update, we will be reusing a cached version
// of the main module compile result, which has outdated sourcesContent.
resolvedMap.sourcesContent = templateMap.sourcesContent
Expand Down

0 comments on commit 5713a87

Please sign in to comment.