Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(plugin-vue): replace source-map with gen/trace-mapping #8155

Merged
merged 1 commit into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

source-map is still needed for type. (not included in bundle though)

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
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.