Skip to content

Commit

Permalink
fix(plugin-vue): fix sourcemap when no script block in sfc (close vit…
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Jun 15, 2022
1 parent 4e6c26f commit a1e3b1c
Showing 1 changed file with 41 additions and 37 deletions.
78 changes: 41 additions & 37 deletions packages/plugin-vue/src/main.ts
@@ -1,6 +1,6 @@
import path from 'path'
import type { SFCBlock, SFCDescriptor } from 'vue/compiler-sfc'
import type { PluginContext, SourceMap, TransformPluginContext } from 'rollup'
import type { PluginContext, TransformPluginContext } from 'rollup'
import type { RawSourceMap } from 'source-map'
import type { EncodedSourceMap as TraceEncodedSourceMap } from '@jridgewell/trace-mapping'
import { TraceMap, eachMapping } from '@jridgewell/trace-mapping'
Expand Down Expand Up @@ -46,7 +46,7 @@ export async function transformMain(
const hasScoped = descriptor.styles.some((s) => s.scoped)

// script
const { code: scriptCode, map } = await genScriptCode(
const { code: scriptCode, map: scriptMap } = await genScriptCode(
descriptor,
options,
pluginContext,
Expand Down Expand Up @@ -156,40 +156,44 @@ export async function transformMain(
)
}

// if the template is inlined into the main module (indicated by the presence
// of templateMap, we need to concatenate the two source maps.
let resolvedMap = options.sourceMap ? map : undefined
if (resolvedMap && templateMap) {
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
eachMapping(tracer, (m) => {
if (m.source == null) return
addMapping(gen, {
source: m.source,
original: { line: m.originalLine, column: m.originalColumn },
generated: {
line: m.generatedLine + offset,
column: m.generatedColumn
}
let resolvedMap: RawSourceMap | undefined = undefined
if (options.sourceMap) {
// if the template is inlined into the main module (indicated by the presence
// of templateMap, we need to concatenate the two source maps.
if (scriptMap && templateMap) {
const gen = fromMap(
// version property of result.map is declared as string
// but actually it is `3`
scriptMap 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
eachMapping(tracer, (m) => {
if (m.source == null) return
addMapping(gen, {
source: m.source,
original: { line: m.originalLine, column: m.originalColumn },
generated: {
line: m.generatedLine + offset,
column: m.generatedColumn
}
})
})
})

// 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
// 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
} else {
resolvedMap = scriptMap ?? templateMap
}
}

if (!attachedProps.length) {
Expand Down Expand Up @@ -287,10 +291,10 @@ async function genScriptCode(
ssr: boolean
): Promise<{
code: string
map: RawSourceMap
map: RawSourceMap | undefined
}> {
let scriptCode = `const _sfc_main = {}`
let map: RawSourceMap | SourceMap | undefined
let map: RawSourceMap | undefined

const script = resolveScript(descriptor, options, ssr)
if (script) {
Expand Down Expand Up @@ -322,7 +326,7 @@ async function genScriptCode(
}
return {
code: scriptCode,
map: map as any
map
}
}

Expand Down

0 comments on commit a1e3b1c

Please sign in to comment.