4
4
parseVuePartRequest ,
5
5
resolveVuePart ,
6
6
isVuePartRequest ,
7
- transformRequireToImport ,
7
+ transformRequireToImport
8
8
} from './utils'
9
9
import {
10
10
createDefaultCompiler ,
@@ -13,14 +13,17 @@ import {
13
13
StyleOptions ,
14
14
TemplateOptions ,
15
15
StyleCompileResult ,
16
- DescriptorCompileResult ,
16
+ DescriptorCompileResult
17
17
} from '@vue/component-compiler'
18
18
import MagicString from 'magic-string'
19
19
import { Plugin , RawSourceMap } from 'rollup'
20
20
import * as path from 'path'
21
21
import { parse , SFCDescriptor , SFCBlock } from '@vue/component-compiler-utils'
22
22
import debug from 'debug'
23
- import { VueTemplateCompiler , VueTemplateCompilerParseOptions } from '@vue/component-compiler-utils/dist/types'
23
+ import {
24
+ VueTemplateCompiler ,
25
+ VueTemplateCompilerParseOptions
26
+ } from '@vue/component-compiler-utils/dist/types'
24
27
25
28
const templateCompiler = require ( 'vue-template-compiler' )
26
29
const hash = require ( 'hash-sum' )
@@ -164,20 +167,28 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
164
167
const isProduction =
165
168
opts . template && typeof opts . template . isProduction === 'boolean'
166
169
? opts . template . isProduction
167
- : process . env . NODE_ENV === 'production' || process . env . BUILD === 'production'
170
+ : process . env . NODE_ENV === 'production' ||
171
+ process . env . BUILD === 'production'
168
172
169
173
d ( 'Version ' + version )
170
174
d ( `Build environment: ${ isProduction ? 'production' : 'development' } ` )
171
175
d ( `Build target: ${ process . env . VUE_ENV || 'browser' } ` )
172
176
173
- if ( ! opts . normalizer ) opts . normalizer = '~' + 'vue-runtime-helpers/dist/normalize-component.mjs'
174
- if ( ! opts . styleInjector ) opts . styleInjector = '~' + 'vue-runtime-helpers/dist/inject-style/browser.mjs'
175
- if ( ! opts . styleInjectorSSR ) opts . styleInjectorSSR = '~' + 'vue-runtime-helpers/dist/inject-style/server.mjs'
176
- if ( ! opts . styleInjectorSSR ) opts . styleInjectorShadow = '~' + 'vue-runtime-helpers/dist/inject-style/shadow.mjs'
177
+ if ( ! opts . normalizer )
178
+ opts . normalizer = '~' + 'vue-runtime-helpers/dist/normalize-component.mjs'
179
+ if ( ! opts . styleInjector )
180
+ opts . styleInjector =
181
+ '~' + 'vue-runtime-helpers/dist/inject-style/browser.mjs'
182
+ if ( ! opts . styleInjectorSSR )
183
+ opts . styleInjectorSSR =
184
+ '~' + 'vue-runtime-helpers/dist/inject-style/server.mjs'
185
+ if ( ! opts . styleInjectorSSR )
186
+ opts . styleInjectorShadow =
187
+ '~' + 'vue-runtime-helpers/dist/inject-style/shadow.mjs'
177
188
178
189
createVuePartRequest . defaultLang = {
179
190
...createVuePartRequest . defaultLang ,
180
- ...opts . defaultLang ,
191
+ ...opts . defaultLang
181
192
}
182
193
183
194
const shouldExtractCss = opts . css === false
@@ -197,9 +208,12 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
197
208
}
198
209
const isAllowed = createCustomBlockFilter ( opts . customBlocks || customBlocks )
199
210
200
- const beforeAssemble = opts . beforeAssemble || ( ( d : DescriptorCompileResult ) : DescriptorCompileResult => d )
211
+ const beforeAssemble =
212
+ opts . beforeAssemble ||
213
+ ( ( d : DescriptorCompileResult ) : DescriptorCompileResult => d )
201
214
202
- const exposeFilename = typeof opts . exposeFilename === 'boolean' ? opts . exposeFilename : false
215
+ const exposeFilename =
216
+ typeof opts . exposeFilename === 'boolean' ? opts . exposeFilename : false
203
217
204
218
const data : VuePluginOptionsData = ( opts . data || { } ) as any
205
219
@@ -219,9 +233,9 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
219
233
video : [ 'src' , 'poster' ] ,
220
234
source : 'src' ,
221
235
img : 'src' ,
222
- image : 'xlink:href' ,
236
+ image : 'xlink:href'
223
237
} ,
224
- ...opts . template ,
238
+ ...opts . template
225
239
} as any
226
240
227
241
if ( opts . template && typeof opts . template . isProduction === 'undefined' ) {
@@ -233,11 +247,16 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
233
247
234
248
if ( opts . css === false ) d ( 'Running in CSS extract mode' )
235
249
236
- function prependStyle ( id : string , lang : string , code : string , map : any ) : { code : string } {
250
+ function prependStyle (
251
+ id : string ,
252
+ lang : string ,
253
+ code : string ,
254
+ map : any
255
+ ) : { code : string } {
237
256
if ( ! ( lang in data ) ) return { code }
238
257
const ms = new MagicString ( code , {
239
258
filename : id ,
240
- indentExclusionRanges : [ ] ,
259
+ indentExclusionRanges : [ ]
241
260
} )
242
261
243
262
const value : string | ( ( ) => string ) = ( data as any ) [ lang ]
@@ -270,7 +289,7 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
270
289
return path . resolve ( path . dirname ( ref . filename ) , src as string )
271
290
} else {
272
291
return require . resolve ( src , {
273
- paths : [ path . dirname ( ref . filename ) ] ,
292
+ paths : [ path . dirname ( ref . filename ) ]
274
293
} )
275
294
}
276
295
}
@@ -312,22 +331,35 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
312
331
compiler : opts . compiler || templateCompiler ,
313
332
compilerParseOptions : opts . compilerParseOptions ,
314
333
sourceRoot : opts . sourceRoot ,
315
- needMap : 'needMap' in opts ? ( opts as any ) . needMap : true ,
334
+ needMap : 'needMap' in opts ? ( opts as any ) . needMap : true
316
335
} )
317
336
)
318
337
)
319
338
320
339
descriptors . set ( filename , descriptor )
321
340
322
- const scopeId = 'data-v-' + ( isProduction ? hash ( path . basename ( filename ) + source ) : hash ( filename + source ) )
341
+ const scopeId =
342
+ 'data-v-' +
343
+ ( isProduction
344
+ ? hash ( path . basename ( filename ) + source )
345
+ : hash ( filename + source ) )
323
346
324
347
const styles = await Promise . all (
325
348
descriptor . styles . map ( async style => {
326
349
if ( style . content ) {
327
- style . content = prependStyle ( filename , style . lang || 'css' , style . content , style . map ) . code
350
+ style . content = prependStyle (
351
+ filename ,
352
+ style . lang || 'css' ,
353
+ style . content ,
354
+ style . map
355
+ ) . code
328
356
}
329
357
330
- const compiled = await compiler . compileStyleAsync ( filename , scopeId , style )
358
+ const compiled = await compiler . compileStyleAsync (
359
+ filename ,
360
+ scopeId ,
361
+ style
362
+ )
331
363
if ( compiled . errors . length > 0 ) throw Error ( compiled . errors [ 0 ] )
332
364
return compiled
333
365
} )
@@ -336,11 +368,14 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
336
368
const input : any = {
337
369
scopeId,
338
370
styles,
339
- customBlocks : [ ] ,
371
+ customBlocks : [ ]
340
372
}
341
373
342
374
if ( descriptor . template ) {
343
- input . template = compiler . compileTemplate ( filename , descriptor . template )
375
+ input . template = compiler . compileTemplate (
376
+ filename ,
377
+ descriptor . template
378
+ )
344
379
345
380
input . template . code = transformRequireToImport ( input . template . code )
346
381
@@ -356,17 +391,29 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
356
391
input . script = descriptor . script
357
392
? {
358
393
code : `
359
- export * from '${ createVuePartRequest ( filename , descriptor . script . lang || 'js' , 'script' ) } '
360
- import script from '${ createVuePartRequest ( filename , descriptor . script . lang || 'js' , 'script' ) } '
394
+ export * from '${ createVuePartRequest (
395
+ filename ,
396
+ descriptor . script . lang || 'js' ,
397
+ 'script'
398
+ ) } '
399
+ import script from '${ createVuePartRequest (
400
+ filename ,
401
+ descriptor . script . lang || 'js' ,
402
+ 'script'
403
+ ) } '
361
404
export default script
362
405
${
363
406
exposeFilename
364
407
? `
365
408
// For security concerns, we use only base name in production mode. See https://github.com/vuejs/rollup-plugin-vue/issues/258
366
- script.__file = ${ isProduction ? JSON . stringify ( path . basename ( filename ) ) : JSON . stringify ( filename ) } `
409
+ script.__file = ${
410
+ isProduction
411
+ ? JSON . stringify ( path . basename ( filename ) )
412
+ : JSON . stringify ( filename )
413
+ } `
367
414
: ''
368
415
}
369
- ` ,
416
+ `
370
417
}
371
418
: { code : '' }
372
419
@@ -375,7 +422,14 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
375
422
. map ( ( style : StyleCompileResult , index : number ) => {
376
423
; ( descriptor . styles [ index ] as any ) . code = style . code
377
424
378
- input . script . code += '\n' + `import '${ createVuePartRequest ( filename , 'css' , 'styles' , index ) } '`
425
+ input . script . code +=
426
+ '\n' +
427
+ `import '${ createVuePartRequest (
428
+ filename ,
429
+ 'css' ,
430
+ 'styles' ,
431
+ index
432
+ ) } '`
379
433
380
434
if ( style . module || descriptor . styles [ index ] . scoped ) {
381
435
return { ...style , code : '' , map : undefined }
@@ -402,22 +456,32 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
402
456
) } '`
403
457
} )
404
458
405
- dT ( `id: ${ filename } \ncode:\n${ result . code } \n\nmap:\n${ JSON . stringify ( result . map , null , 2 ) } \n` )
459
+ dT (
460
+ `id: ${ filename } \ncode:\n${ result . code } \n\nmap:\n${ JSON . stringify (
461
+ result . map ,
462
+ null ,
463
+ 2
464
+ ) } \n`
465
+ )
406
466
407
467
result . map = result . map || { mappings : '' }
408
468
409
469
return result
410
470
}
411
- } ,
471
+ }
412
472
}
413
473
}
414
474
415
- function createCustomBlockFilter ( customBlocks ?: string [ ] | ( ( tag : string ) => boolean ) ) : ( tag : string ) => boolean {
475
+ function createCustomBlockFilter (
476
+ customBlocks ?: string [ ] | ( ( tag : string ) => boolean )
477
+ ) : ( tag : string ) => boolean {
416
478
if ( typeof customBlocks === 'function' ) return customBlocks
417
479
if ( ! Array . isArray ( customBlocks ) ) return ( ) => false
418
480
419
481
const allowed = new Set ( customBlocks . filter ( tag => ! tag . startsWith ( '!' ) ) )
420
- const notAllowed = new Set ( customBlocks . filter ( tag => tag . startsWith ( '!' ) ) . map ( tag => tag . substr ( 1 ) ) )
482
+ const notAllowed = new Set (
483
+ customBlocks . filter ( tag => tag . startsWith ( '!' ) ) . map ( tag => tag . substr ( 1 ) )
484
+ )
421
485
422
486
return tag => {
423
487
if ( allowed . has ( tag ) ) return true
0 commit comments