@@ -4,27 +4,34 @@ import {
4
4
parseVuePartRequest ,
5
5
resolveVuePart ,
6
6
isVuePartRequest ,
7
- transformRequireToImport
7
+ transformRequireToImport ,
8
8
} from './utils'
9
9
import {
10
10
createDefaultCompiler ,
11
11
assemble ,
12
12
ScriptOptions ,
13
13
StyleOptions ,
14
14
TemplateOptions ,
15
- StyleCompileResult
15
+ StyleCompileResult ,
16
16
} from '@vue/component-compiler'
17
17
import { Plugin } from 'rollup'
18
18
import * as path from 'path'
19
19
import { parse , SFCDescriptor , SFCBlock } from '@vue/component-compiler-utils'
20
20
import debug from 'debug'
21
- import { VueTemplateCompiler , VueTemplateCompilerParseOptions } from '@vue/component-compiler-utils/dist/types'
21
+ import {
22
+ VueTemplateCompiler ,
23
+ VueTemplateCompilerParseOptions ,
24
+ } from '@vue/component-compiler-utils/dist/types'
22
25
23
26
const templateCompiler = require ( 'vue-template-compiler' )
24
27
const hash = require ( 'hash-sum' )
25
- const d = debug ( 'rollup-plugin-vue' )
26
28
const { version } = require ( '../package.json' )
27
29
30
+ const d = debug ( 'rollup-plugin-vue' )
31
+ const dR = debug ( 'rollup-plugin-vue:resolve' )
32
+ const dL = debug ( 'rollup-plugin-vue:load' )
33
+ const dT = debug ( 'rollup-plugin-vue:transform' )
34
+
28
35
export interface VuePluginOptions {
29
36
/**
30
37
* Include files or directories.
@@ -115,13 +122,16 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
115
122
d ( `Build environment: ${ isProduction ? 'production' : 'development' } ` )
116
123
d ( `Build target: ${ process . env . VUE_ENV || 'browser' } ` )
117
124
118
- if ( ! opts . normalizer ) opts . normalizer = '~' + require . resolve ( 'vue-runtime-helpers/normalize-component.js' )
119
- if ( ! opts . styleInjector ) opts . styleInjector = '~' + require . resolve ( 'vue-runtime-helpers/inject-style/browser.js' )
120
- if ( ! opts . styleInjectorSSR ) opts . styleInjectorSSR = '~' + require . resolve ( 'vue-runtime-helpers/inject-style/server.js' )
125
+ if ( ! opts . normalizer )
126
+ opts . normalizer = '~' + 'vue-runtime-helpers/normalize-component.js'
127
+ if ( ! opts . styleInjector )
128
+ opts . styleInjector = '~' + 'vue-runtime-helpers/inject-style/browser.js'
129
+ if ( ! opts . styleInjectorSSR )
130
+ opts . styleInjectorSSR = '~' + 'vue-runtime-helpers/inject-style/server.js'
121
131
122
132
createVuePartRequest . defaultLang = {
123
133
...createVuePartRequest . defaultLang ,
124
- ...opts . defaultLang
134
+ ...opts . defaultLang ,
125
135
}
126
136
127
137
const shouldExtractCss = opts . css === false
@@ -144,9 +154,9 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
144
154
video : [ 'src' , 'poster' ] ,
145
155
source : 'src' ,
146
156
img : 'src' ,
147
- image : 'xlink:href'
157
+ image : 'xlink:href' ,
148
158
} ,
149
- ...opts . template
159
+ ...opts . template ,
150
160
} as any
151
161
if ( opts . template && typeof opts . template . isProduction === 'undefined' ) {
152
162
opts . template . isProduction = isProduction
@@ -160,6 +170,12 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
160
170
name : 'VuePlugin' ,
161
171
162
172
resolveId ( id , importer ) {
173
+ const request = id
174
+ if ( id . startsWith ( 'vue-runtime-helpers/' ) ) {
175
+ id = require . resolve ( id )
176
+ dR ( `form: ${ request } \nto: ${ id } \n` )
177
+ return id
178
+ }
163
179
if ( ! isVuePartRequest ( id ) ) return
164
180
id = path . resolve ( path . dirname ( importer ) , id )
165
181
const ref = parseVuePartRequest ( id )
@@ -174,6 +190,7 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
174
190
}
175
191
}
176
192
193
+ dR ( `from: ${ request } \nto: ${ id } \n` )
177
194
return id
178
195
}
179
196
} ,
@@ -184,24 +201,31 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
184
201
if ( ! request ) return
185
202
186
203
const element = resolveVuePart ( descriptors , request )
187
- const code = 'code' in element
188
- ? ( ( element as any ) . code as string ) // .code is set when extract styles is used. { css: false }
189
- : element . content
204
+ const code =
205
+ 'code' in element
206
+ ? ( ( element as any ) . code as string ) // .code is set when extract styles is used. { css: false }
207
+ : element . content
190
208
const map = element . map as any
191
209
210
+ dL ( `id: ${ id } \ncode: \n${ code } \nmap: ${ JSON . stringify ( map , null , 2 ) } \n\n` )
211
+
192
212
return { code, map }
193
213
} ,
194
214
195
215
async transform ( source : string , filename : string ) {
196
216
if ( isVue ( filename ) ) {
197
- const descriptor : SFCDescriptor = JSON . parse ( JSON . stringify ( parse ( {
198
- filename,
199
- source,
200
- compiler : opts . compiler || templateCompiler ,
201
- compilerParseOptions : opts . compilerParseOptions ,
202
- sourceRoot : opts . sourceRoot ,
203
- needMap : true
204
- } ) ) )
217
+ const descriptor : SFCDescriptor = JSON . parse (
218
+ JSON . stringify (
219
+ parse ( {
220
+ filename,
221
+ source,
222
+ compiler : opts . compiler || templateCompiler ,
223
+ compilerParseOptions : opts . compilerParseOptions ,
224
+ sourceRoot : opts . sourceRoot ,
225
+ needMap : true ,
226
+ } )
227
+ )
228
+ )
205
229
206
230
const scopeId =
207
231
'data-v-' +
@@ -212,7 +236,11 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
212
236
213
237
const styles = await Promise . all (
214
238
descriptor . styles . map ( async style => {
215
- const compiled = await compiler . compileStyleAsync ( filename , scopeId , style )
239
+ const compiled = await compiler . compileStyleAsync (
240
+ filename ,
241
+ scopeId ,
242
+ style
243
+ )
216
244
if ( compiled . errors . length > 0 ) throw Error ( compiled . errors [ 0 ] )
217
245
return compiled
218
246
} )
@@ -221,7 +249,7 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
221
249
const input : any = {
222
250
scopeId,
223
251
styles,
224
- customBlocks : [ ]
252
+ customBlocks : [ ] ,
225
253
}
226
254
227
255
if ( descriptor . template ) {
@@ -230,9 +258,7 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
230
258
descriptor . template
231
259
)
232
260
233
- input . template . code = transformRequireToImport (
234
- input . template . code
235
- )
261
+ input . template . code = transformRequireToImport ( input . template . code )
236
262
237
263
if ( input . template . errors && input . template . errors . length ) {
238
264
input . template . errors . map ( ( error : Error ) => this . error ( error ) )
@@ -257,9 +283,10 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
257
283
'script'
258
284
) } '
259
285
export default script
260
- `
286
+ ` ,
287
+ map : { mappings : '' } ,
261
288
}
262
- : { code : '' }
289
+ : { code : '' , map : { mappings : '' } }
263
290
264
291
if ( shouldExtractCss ) {
265
292
input . styles = input . styles
@@ -276,12 +303,14 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
276
303
) } '`
277
304
278
305
if ( style . module || descriptor . styles [ index ] . scoped ) {
279
- return { ...style , code : '' }
306
+ return { ...style , code : '' , map : { mappings : '' } }
280
307
}
281
308
} )
282
309
. filter ( Boolean )
283
310
}
284
311
312
+ input . script . code = input . script . code . replace ( / ^ \s + / mg, '' )
313
+
285
314
const result = assemble ( compiler , filename , input , opts )
286
315
287
316
descriptor . customBlocks . forEach ( ( block , index ) => {
@@ -298,8 +327,10 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
298
327
) } '`
299
328
} )
300
329
330
+ dT ( `id: ${ filename } \ncode:\n${ result . code } \n\nmap:\n${ JSON . stringify ( result . map , null , 2 ) } \n` )
331
+
301
332
return result
302
333
}
303
- }
334
+ } ,
304
335
}
305
336
}
0 commit comments