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' ) ;
55
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+ }
1110
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+ } ;
1415
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 ) || { } ;
1720
18- const cb = this . async ( ) ;
19- let component ;
21+ const {
22+ svgo : svgoConfig = { } ,
23+ functional = false ,
24+ } = options ;
2025
21- svg
22- . optimize ( content , { path } )
26+ getSvg ( content , path , svgoConfig )
2327 . then ( ( result ) => {
24- const compiled = compiler . compile ( result . data , {
28+ let { render : renderFn } = compile ( result , {
2529 preserveWhitespace : false ,
2630 modules : [
2731 ! ! functional && {
@@ -31,27 +35,32 @@ module.exports = function (content) {
3135 el . styleBinding = '[data.style, data.staticStyle]' ;
3236 }
3337 } ,
34- }
38+ } ,
3539 ] ,
3640 } ) ;
37-
38- const transpileCode = `var render = function (${ functional ? '_h, _vm' : '' } ) { ${ compiled . render } };` ;
3941
40- const transpileOptions = {
42+ renderFn = `
43+ function render(${ functional ? '_h, _vm' : '' } ) {
44+ ${ renderFn } ;
45+ };
46+ ` ;
47+
48+ renderFn = stripWith ( renderFn , {
4149 transforms : {
4250 stripWithFunctional : ! ! functional ,
4351 } ,
44- } ;
52+ } ) ;
4553
46- component = `${ transpile ( transpileCode , transpileOptions ) } \n` ;
54+ const component = `
55+ ${ renderFn }
4756
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+ ` ;
5362
54- cb ( null , component ) ;
63+ callback ( null , component ) ;
5564 } )
56- . catch ( cb ) ;
65+ . catch ( callback ) ;
5766} ;
0 commit comments