33var fs = require ( 'fs' )
44var path = require ( 'path' )
55var mkdirp = require ( 'mkdirp' )
6- var mime = require ( 'mime' )
76
87var defaultOptions = {
98 img : 'src' ,
10- image : 'xlink:href' ,
11- limit : 10 * 1024
9+ image : 'xlink:href'
1210}
1311
1412module . exports = ( userOptions , fileOptions ) => {
@@ -25,46 +23,29 @@ module.exports = (userOptions, fileOptions) => {
2523
2624function transform ( node , options , fileOptions ) {
2725 for ( var tag in options ) {
28- if ( tag !== 'limit' && node . tag === tag && node . attrs ) {
26+ if ( node . tag === tag && node . attrs ) {
2927 var attributes = options [ tag ]
3028 if ( typeof attributes === 'string' ) {
31- rewrite ( node . attrsMap , attributes , fileOptions , options . limit )
29+ rewrite ( node . attrsMap , attributes , fileOptions )
3230 } else if ( Array . isArray ( attributes ) ) {
33- attributes . forEach ( item => rewrite ( node . attrsMap , item , fileOptions , options . limit ) )
31+ attributes . forEach ( item => rewrite ( node . attrsMap , item , fileOptions ) )
3432 }
3533 }
3634 }
3735}
3836
39- function rewrite ( attrsMap , name , fileOptions , limit ) {
37+ function rewrite ( attrsMap , name , fileOptions ) {
4038 var value = attrsMap [ name ]
4139 if ( value ) {
4240 var firstChar = value . charAt ( 0 )
4341 if ( firstChar === '.' ) {
4442 // 资源路径
4543 var assetPath = path . resolve ( path . dirname ( fileOptions . resourcePath ) , value )
46- // 小于limit的资源转base64
47- var str = assetToBase64 ( assetPath , limit )
48- if ( str ) {
49- attrsMap [ name ] = `data:${ mime . getType ( assetPath ) || '' } ;base64,${ str } `
50- } else {
51- // 重写路径,为了避免重名,在webpack输出目录下新建copy-asset目录,资源保存到这里
52- var assetOutputPath = path . join ( 'copy-asset' , path . relative ( process . cwd ( ) , assetPath ) . replace ( / ^ s r c / , '' ) )
53- attrsMap [ name ] = `/${ assetOutputPath . split ( path . sep ) . join ( '/' ) } `
54- copyAsset ( assetPath , path . resolve ( fileOptions . outputPath , assetOutputPath ) )
55- }
56- }
57- }
58- }
59-
60- function assetToBase64 ( assetPath , limit ) {
61- try {
62- var buffer = fs . readFileSync ( assetPath )
63- if ( buffer . length <= limit ) {
64- return buffer . toString ( 'base64' )
44+ // 重写路径,为了避免重名,在webpack输出目录下新建copy-asset目录,资源保存到这里
45+ var assetOutputPath = path . join ( 'copy-asset' , path . relative ( process . cwd ( ) , assetPath ) . replace ( / ^ s r c / , '' ) )
46+ attrsMap [ name ] = `/${ assetOutputPath . split ( path . sep ) . join ( '/' ) } `
47+ copyAsset ( assetPath , path . resolve ( fileOptions . outputPath , assetOutputPath ) )
6548 }
66- } catch ( err ) {
67- console . error ( 'ReadFile Error:' + err )
6849 }
6950}
7051
0 commit comments