1
1
import path from 'path'
2
2
import type { SFCBlock , SFCDescriptor } from 'vue/compiler-sfc'
3
- import type { PluginContext , SourceMap , TransformPluginContext } from 'rollup'
3
+ import type { PluginContext , TransformPluginContext } from 'rollup'
4
4
import type { RawSourceMap } from 'source-map'
5
5
import type { EncodedSourceMap as TraceEncodedSourceMap } from '@jridgewell/trace-mapping'
6
6
import { TraceMap , eachMapping } from '@jridgewell/trace-mapping'
@@ -46,7 +46,7 @@ export async function transformMain(
46
46
const hasScoped = descriptor . styles . some ( ( s ) => s . scoped )
47
47
48
48
// script
49
- const { code : scriptCode , map } = await genScriptCode (
49
+ const { code : scriptCode , map : scriptMap } = await genScriptCode (
50
50
descriptor ,
51
51
options ,
52
52
pluginContext ,
@@ -58,7 +58,7 @@ export async function transformMain(
58
58
descriptor . template && ! isUseInlineTemplate ( descriptor , ! devServer )
59
59
60
60
let templateCode = ''
61
- let templateMap : RawSourceMap | undefined
61
+ let templateMap : RawSourceMap | undefined = undefined
62
62
if ( hasTemplateImport ) {
63
63
; ( { code : templateCode , map : templateMap } = await genTemplateCode (
64
64
descriptor ,
@@ -156,40 +156,46 @@ export async function transformMain(
156
156
)
157
157
}
158
158
159
- // if the template is inlined into the main module (indicated by the presence
160
- // of templateMap, we need to concatenate the two source maps.
161
- let resolvedMap = options . sourceMap ? map : undefined
162
- if ( resolvedMap && templateMap ) {
163
- const gen = fromMap (
164
- // version property of result.map is declared as string
165
- // but actually it is `3`
166
- map as Omit < RawSourceMap , 'version' > as TraceEncodedSourceMap
167
- )
168
- const tracer = new TraceMap (
169
- // same above
170
- templateMap as Omit < RawSourceMap , 'version' > as TraceEncodedSourceMap
171
- )
172
- const offset = ( scriptCode . match ( / \r ? \n / g) ?. length ?? 0 ) + 1
173
- eachMapping ( tracer , ( m ) => {
174
- if ( m . source == null ) return
175
- addMapping ( gen , {
176
- source : m . source ,
177
- original : { line : m . originalLine , column : m . originalColumn } ,
178
- generated : {
179
- line : m . generatedLine + offset ,
180
- column : m . generatedColumn
181
- }
159
+ let resolvedMap : RawSourceMap | undefined = undefined
160
+ if ( options . sourceMap ) {
161
+ if ( scriptMap && templateMap ) {
162
+ // if the template is inlined into the main module (indicated by the presence
163
+ // of templateMap, we need to concatenate the two source maps.
164
+
165
+ const gen = fromMap (
166
+ // version property of result.map is declared as string
167
+ // but actually it is `3`
168
+ scriptMap as Omit < RawSourceMap , 'version' > as TraceEncodedSourceMap
169
+ )
170
+ const tracer = new TraceMap (
171
+ // same above
172
+ templateMap as Omit < RawSourceMap , 'version' > as TraceEncodedSourceMap
173
+ )
174
+ const offset = ( scriptCode . match ( / \r ? \n / g) ?. length ?? 0 ) + 1
175
+ eachMapping ( tracer , ( m ) => {
176
+ if ( m . source == null ) return
177
+ addMapping ( gen , {
178
+ source : m . source ,
179
+ original : { line : m . originalLine , column : m . originalColumn } ,
180
+ generated : {
181
+ line : m . generatedLine + offset ,
182
+ column : m . generatedColumn
183
+ }
184
+ } )
182
185
} )
183
- } )
184
186
185
- // same above
186
- resolvedMap = toEncodedMap ( gen ) as Omit <
187
- GenEncodedSourceMap ,
188
- 'version'
189
- > as RawSourceMap
190
- // if this is a template only update, we will be reusing a cached version
191
- // of the main module compile result, which has outdated sourcesContent.
192
- resolvedMap . sourcesContent = templateMap . sourcesContent
187
+ // same above
188
+ resolvedMap = toEncodedMap ( gen ) as Omit <
189
+ GenEncodedSourceMap ,
190
+ 'version'
191
+ > as RawSourceMap
192
+ // if this is a template only update, we will be reusing a cached version
193
+ // of the main module compile result, which has outdated sourcesContent.
194
+ resolvedMap . sourcesContent = templateMap . sourcesContent
195
+ } else {
196
+ // if one of `scriptMap` and `templateMap` is empty, use the other one
197
+ resolvedMap = scriptMap ?? templateMap
198
+ }
193
199
}
194
200
195
201
if ( ! attachedProps . length ) {
@@ -287,10 +293,10 @@ async function genScriptCode(
287
293
ssr : boolean
288
294
) : Promise < {
289
295
code : string
290
- map : RawSourceMap
296
+ map : RawSourceMap | undefined
291
297
} > {
292
298
let scriptCode = `const _sfc_main = {}`
293
- let map : RawSourceMap | SourceMap | undefined
299
+ let map : RawSourceMap | undefined
294
300
295
301
const script = resolveScript ( descriptor , options , ssr )
296
302
if ( script ) {
@@ -322,7 +328,7 @@ async function genScriptCode(
322
328
}
323
329
return {
324
330
code : scriptCode ,
325
- map : map as any
331
+ map
326
332
}
327
333
}
328
334
0 commit comments