1
- const svgo = require ( 'svgo' ) ;
2
- const loaderUtils = require ( 'loader-utils' ) ;
3
- const compiler = require ( 'vue-template-compiler' ) ;
4
- const transpile = require ( 'vue-template-es2015-compiler' ) ;
1
+ const SVGO = require ( 'svgo' ) ;
2
+ const { getOptions } = require ( 'loader-utils' ) ;
3
+ const { compile } = require ( 'vue-template-compiler' ) ;
4
+ const stripWith = require ( 'vue-template-es2015-compiler' ) ;
5
5
6
- module . exports = function ( content ) {
7
- const {
8
- svgo : svgoOptions = { } ,
9
- functional = false ,
10
- } = loaderUtils . getOptions ( this ) || { } ;
6
+ function getSvg ( content , path , svgoConfig ) {
7
+ if ( svgoConfig === false ) {
8
+ return Promise . resolve ( content ) ;
9
+ }
11
10
12
- const svg = new svgo ( svgoOptions ) ;
13
- const path = this . resourcePath ;
11
+ return new SVGO ( svgoConfig )
12
+ . optimize ( content , { path } )
13
+ . then ( result => result . data ) ;
14
+ } ;
14
15
15
- this . cacheable && this . cacheable ( true ) ;
16
- this . addDependency ( path ) ;
16
+ module . exports = function ( content ) {
17
+ const path = this . resourcePath ;
18
+ const callback = this . async ( ) ;
19
+ const options = getOptions ( this ) || { } ;
17
20
18
- const cb = this . async ( ) ;
19
- let component ;
21
+ const {
22
+ svgo : svgoConfig = { } ,
23
+ functional = false ,
24
+ } = options ;
20
25
21
- svg
22
- . optimize ( content , { path } )
26
+ getSvg ( content , path , svgoConfig )
23
27
. then ( ( result ) => {
24
- const compiled = compiler . compile ( result . data , {
28
+ let { render : renderFn } = compile ( result , {
25
29
preserveWhitespace : false ,
26
30
modules : [
27
31
! ! functional && {
@@ -31,27 +35,32 @@ module.exports = function (content) {
31
35
el . styleBinding = '[data.style, data.staticStyle]' ;
32
36
}
33
37
} ,
34
- }
38
+ } ,
35
39
] ,
36
40
} ) ;
37
-
38
- const transpileCode = `var render = function (${ functional ? '_h, _vm' : '' } ) { ${ compiled . render } };` ;
39
41
40
- const transpileOptions = {
42
+ renderFn = `
43
+ function render(${ functional ? '_h, _vm' : '' } ) {
44
+ ${ renderFn } ;
45
+ };
46
+ ` ;
47
+
48
+ renderFn = stripWith ( renderFn , {
41
49
transforms : {
42
50
stripWithFunctional : ! ! functional ,
43
51
} ,
44
- } ;
52
+ } ) ;
45
53
46
- component = `${ transpile ( transpileCode , transpileOptions ) } \n` ;
54
+ const component = `
55
+ ${ renderFn }
47
56
48
- if ( functional ) {
49
- component += 'module.exports = { functional: true, render: render };' ;
50
- } else {
51
- component += 'module.exports = { render: render };' ;
52
- }
57
+ module.exports = {
58
+ ${ functional ? ' functional: true,' : '' }
59
+ render: render,
60
+ } ;
61
+ ` ;
53
62
54
- cb ( null , component ) ;
63
+ callback ( null , component ) ;
55
64
} )
56
- . catch ( cb ) ;
65
+ . catch ( callback ) ;
57
66
} ;
0 commit comments