3
3
var fs = require ( 'fs' )
4
4
var path = require ( 'path' )
5
5
var mkdirp = require ( 'mkdirp' )
6
- var mime = require ( 'mime' )
7
6
8
7
var defaultOptions = {
9
8
img : 'src' ,
10
- image : 'xlink:href' ,
11
- limit : 10 * 1024
9
+ image : 'xlink:href'
12
10
}
13
11
14
12
module . exports = ( userOptions , fileOptions ) => {
@@ -25,46 +23,29 @@ module.exports = (userOptions, fileOptions) => {
25
23
26
24
function transform ( node , options , fileOptions ) {
27
25
for ( var tag in options ) {
28
- if ( tag !== 'limit' && node . tag === tag && node . attrs ) {
26
+ if ( node . tag === tag && node . attrs ) {
29
27
var attributes = options [ tag ]
30
28
if ( typeof attributes === 'string' ) {
31
- rewrite ( node . attrsMap , attributes , fileOptions , options . limit )
29
+ rewrite ( node . attrsMap , attributes , fileOptions )
32
30
} 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 ) )
34
32
}
35
33
}
36
34
}
37
35
}
38
36
39
- function rewrite ( attrsMap , name , fileOptions , limit ) {
37
+ function rewrite ( attrsMap , name , fileOptions ) {
40
38
var value = attrsMap [ name ]
41
39
if ( value ) {
42
40
var firstChar = value . charAt ( 0 )
43
41
if ( firstChar === '.' ) {
44
42
// 资源路径
45
43
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 ) )
65
48
}
66
- } catch ( err ) {
67
- console . error ( 'ReadFile Error:' + err )
68
49
}
69
50
}
70
51
0 commit comments