From a1e3b1cdd4282e23d05f37cc1b64d8106f866a93 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Wed, 15 Jun 2022 19:21:51 +0800 Subject: [PATCH] fix(plugin-vue): fix sourcemap when no script block in sfc (close #8601) --- packages/plugin-vue/src/main.ts | 78 +++++++++++++++++---------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/packages/plugin-vue/src/main.ts b/packages/plugin-vue/src/main.ts index dda9c75d04ec78..853474e8f8e294 100644 --- a/packages/plugin-vue/src/main.ts +++ b/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' @@ -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, @@ -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 as TraceEncodedSourceMap - ) - const tracer = new TraceMap( - // same above - templateMap as Omit 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 as TraceEncodedSourceMap + ) + const tracer = new TraceMap( + // same above + templateMap as Omit 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) { @@ -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) { @@ -322,7 +326,7 @@ async function genScriptCode( } return { code: scriptCode, - map: map as any + map } }