@@ -6,46 +6,77 @@ var url = require('rework-plugin-url');
6
6
var through = require ( 'through2' ) ;
7
7
var mkpath = require ( 'mkpath' ) ;
8
8
var fs = require ( 'fs' ) ;
9
+ var merge = require ( 'merge' ) ;
10
+ var md5 = require ( 'md5' ) ;
11
+ var fileExists = require ( 'file-exists' ) ;
9
12
10
- module . exports = function ( filePath , publicPrefix ) {
11
- return through . obj ( function ( file , enc , cb ) {
12
- if ( file . isNull ( ) ) {
13
- return ;
14
- }
13
+ module . exports = function ( opt ) {
14
+ var options = merge ( {
15
+ output : null ,
16
+ exclude : [ ] ,
17
+ overwrite : false
18
+ } , opt ) ;
15
19
16
- if ( file . isStream ( ) ) {
17
- return this . emit ( 'error' , PluginError ( 'gulp-css-rebase' , 'Streaming not supported' ) ) ;
18
- }
20
+ return through . obj ( function ( file , enc , cb ) {
21
+ if ( file . isNull ( ) ) {
22
+ return ;
23
+ }
19
24
20
- var adjusted = adjust ( file ) ;
21
- file . contents = new Buffer ( adjusted ) ;
25
+ if ( file . isStream ( ) ) {
26
+ return this . emit ( 'error' , PluginError ( 'gulp-css-rebase' , 'Streaming not supported' ) ) ;
27
+ }
22
28
23
- cb ( null , file ) ;
24
- } ) ;
29
+ var adjusted = adjust ( file ) ;
30
+ file . contents = new Buffer ( adjusted ) ;
25
31
26
- function adjust ( file ) {
27
- var css = file . contents . toString ( ) ;
32
+ cb ( null , file ) ;
33
+ } ) ;
28
34
29
- return rework ( css )
30
- . use ( url ( function ( url ) {
31
- if ( ! / ^ ( d a t a | \/ | \w + : \/ \/ ) / . test ( url ) ) {
32
- var assetPath = path . join ( path . dirname ( file . path ) , url ) ;
33
- var assetFolder = path . basename ( path . dirname ( assetPath ) )
34
- var newPath = path . join ( filePath , assetFolder , path . basename ( assetPath ) ) . replace ( / [ \# | \? ] .* $ / , '' ) ;
35
+ function adjust ( file ) {
36
+ var css = file . contents . toString ( ) ;
35
37
36
- mkpath ( path . dirname ( newPath ) , function ( err ) {
37
- if ( err ) {
38
- throw err ;
39
- }
38
+ return rework ( css )
39
+ . use ( url ( function ( url ) {
40
+ if ( ! / ^ ( d a t a | \/ | \w + : \/ \/ ) / . test ( url ) ) {
41
+ var assetPath = path . join ( path . dirname ( file . path ) , url ) ;
42
+ var assetFolder = md5 ( path . relative ( process . cwd ( ) , path . dirname ( assetPath ) ) ) ;
43
+ var IsExclude = false ;
40
44
41
- fs . createReadStream ( assetPath . replace ( / [ \# | \? ] .* $ / , '' ) ) . pipe ( fs . createWriteStream ( newPath ) ) ;
42
- } ) ;
45
+ for ( var index in options . exclude ) {
46
+ if ( options . exclude [ index ] === assetPath . substr ( 0 , options . exclude [ index ] . length ) ) {
47
+ IsExclude = true ;
48
+ break ;
49
+ }
50
+ }
43
51
44
- url = path . normalize ( path . join ( publicPrefix , assetFolder , path . basename ( assetPath ) ) ) . replace ( / \\ / g, '/' ) ;
45
- }
52
+ var newPath = ! IsExclude
53
+ ? path . normalize ( path . join ( options . output , assetFolder , path . basename ( assetPath ) ) )
54
+ : path . normalize ( assetPath )
55
+ ;
46
56
47
- return url ;
48
- } ) )
49
- . toString ( ) ;
50
- }
57
+ if (
58
+ ( ! IsExclude && ! fileExists ( newPath ) )
59
+ ||
60
+ ( ! IsExclude && options . overwrite )
61
+ ) {
62
+ mkpath ( path . dirname ( newPath ) , function ( err ) {
63
+ if ( err ) {
64
+ throw err ;
65
+ }
66
+
67
+ fs
68
+ . createReadStream ( assetPath . replace ( / [ \# | \? ] .* $ / , '' ) )
69
+ . pipe ( fs . createWriteStream ( newPath . replace ( / [ \# | \? ] .* $ / , '' ) ) )
70
+ ;
71
+ } ) ;
72
+
73
+ }
74
+
75
+ url = path . relative ( options . output , newPath ) ;
76
+ }
77
+
78
+ return url ;
79
+ } ) )
80
+ . toString ( ) ;
81
+ }
51
82
} ;
0 commit comments