@@ -17,14 +17,14 @@ import {
17
17
SFCTemplateCompileOptions ,
18
18
SFCTemplateCompileResults ,
19
19
} from '@vue/compiler-sfc'
20
- // @ts -ignore
21
20
import createDebugger from 'debug'
22
21
import hash from 'hash-sum'
23
22
import { basename , relative } from 'path'
24
23
import qs from 'querystring'
25
24
import { Plugin , RollupError } from 'rollup'
26
25
import { createFilter } from 'rollup-pluginutils'
27
26
import { genCSSModulesCode } from './cssModules'
27
+ import { encode } from 'sourcemap-codec'
28
28
29
29
const debug = createDebugger ( 'rollup-plugin-vue' )
30
30
@@ -73,7 +73,6 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
73
73
return undefined
74
74
} ,
75
75
load ( id ) {
76
- debug ( `load(${ id } )` )
77
76
const query = parseVuePartRequest ( id )
78
77
79
78
if ( query . vue ) {
@@ -91,27 +90,52 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
91
90
: null
92
91
93
92
if ( block ) {
94
- return {
93
+ const result = {
95
94
code : block . content ,
96
- map : normalizeSourceMap ( block . map , 'load:' + id )
95
+ map : normalizeSourceMap ( block . map ) ,
96
+ }
97
+
98
+ if ( query . type === 'template' ) {
99
+ // generate source mapping for each character.
100
+ result . map . mappings = encode (
101
+ result . code . split ( / \r ? \n / ) . map ( ( line , index ) => {
102
+ const segments : [ number , number , number , number ] [ ] = [ ]
103
+ for ( let i = 0 ; i < line . length ; ++ i ) {
104
+ segments . push ( [ i , 0 , block . loc . start . line + index - 1 , i ] )
105
+ }
106
+
107
+ return segments
108
+ } )
109
+ )
97
110
}
111
+
112
+ debug (
113
+ `load(${ id } )` ,
114
+ '\n' +
115
+ result . code +
116
+ '\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,' +
117
+ Buffer . from ( JSON . stringify ( result . map ) , 'utf-8' ) . toString (
118
+ 'base64'
119
+ )
120
+ )
121
+
122
+ return result
98
123
}
99
124
}
100
125
101
126
return undefined
102
127
} ,
103
128
async transform ( code , id ) {
104
- debug ( `transform(${ id } )` )
105
129
const query = parseVuePartRequest ( id )
106
130
if ( query . vue ) {
107
131
const descriptor = getDescriptor ( query . filename )
108
132
if ( query . type === 'template' ) {
133
+ debug ( `transform(${ id } )` )
109
134
const block = descriptor . template !
110
135
const result = compileTemplate ( {
111
136
filename : query . filename ,
112
- source : block . content ,
137
+ source : code ,
113
138
preprocessLang : block . lang ,
114
- inMap : normalizeSourceMap ( block . map ! , 'transform:' + id ) ,
115
139
compiler : options . compiler ,
116
140
compilerOptions : options . compilerOptions ,
117
141
transformAssetUrls : options . transformAssetUrls ,
@@ -139,9 +163,10 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
139
163
140
164
return {
141
165
code : result . code ,
142
- map : normalizeSourceMap ( result . map ! , id ) ,
166
+ map : normalizeSourceMap ( result . map ! ) ,
143
167
}
144
168
} else if ( query . type === 'style' && query . scoped ) {
169
+ debug ( `transform(${ id } )` )
145
170
const block = descriptor . styles [ query . index ] !
146
171
const result = await compileStyleAsync ( {
147
172
filename : query . filename ,
@@ -163,11 +188,12 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
163
188
164
189
return {
165
190
code : result . code ,
166
- map : normalizeSourceMap ( result . map ! , id ) ,
191
+ map : normalizeSourceMap ( result . map ! ) ,
167
192
}
168
193
}
169
194
return null
170
195
} else if ( filter ( id ) ) {
196
+ debug ( `transform(${ id } )` )
171
197
const { descriptor, errors } = parseSFC ( code , id , rootContext )
172
198
173
199
if ( errors . length ) {
@@ -184,9 +210,13 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
184
210
options
185
211
)
186
212
213
+ debug ( 'transient .vue file:' , '\n' + output + '\n' )
214
+
187
215
return {
188
216
code : output ,
189
- map : null ,
217
+ map : {
218
+ mappings : '' ,
219
+ } ,
190
220
}
191
221
} else {
192
222
return null
@@ -262,6 +292,7 @@ function parseSFC(
262
292
sourceMap : true ,
263
293
filename : id ,
264
294
sourceRoot : sourceRoot ,
295
+ pad : 'line' ,
265
296
} )
266
297
267
298
cache . set ( id , descriptor )
@@ -294,8 +325,8 @@ function transformVueSFC(
294
325
const scriptImport = getScriptCode ( descriptor , resourcePath )
295
326
const stylesCode = getStyleCode ( descriptor , resourcePath , id )
296
327
const output = [
297
- templateImport ,
298
328
scriptImport ,
329
+ templateImport ,
299
330
stylesCode ,
300
331
`script.render = render` ,
301
332
]
@@ -416,13 +447,9 @@ function _(any: any) {
416
447
return JSON . stringify ( any )
417
448
}
418
449
419
- function normalizeSourceMap ( map : SFCTemplateCompileResults [ 'map' ] , extra ?: any ) : any {
450
+ function normalizeSourceMap ( map : SFCTemplateCompileResults [ 'map' ] ) : any {
420
451
if ( ! map ) return null as any
421
452
422
- if ( typeof map . mappings !== 'string' ) {
423
- console . log ( extra , map )
424
- }
425
-
426
453
return {
427
454
version : Number ( map . version ) ,
428
455
file : map . file ,
0 commit comments