-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
48 lines (40 loc) · 1.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var evs = require( 'event-stream' );
var _ = require( 'lodash' );
var through = require( 'through2' );
var builtPatterns = [];
function buildRegex( templates, prefix ) {
builtPatterns = templates.map( function( template ) {
var patt = template.replace( "$PREFIX$", prefix );
return new RegExp( patt, "gm" );
} );
}
module.exports = function( opt ) {
var options = _.extend( {
patterns: [
"(React.DOM.img\\((?:.|[\\n\\r])*src:\\s*['\"](?!$PREFIX$))(\\/.*)(['\"])",
"(<img.*src=['\"](?!$PREFIX$))(\\/.*)(['\"].*>)",
"(<link.*href=['\"](?!$PREFIX$))(\\/.*)(['\"].*>)",
"(<script.*src=['\"](?!$PREFIX$))(\\/.*)(['\"].*>)",
"(url.*\\(.*['\"](?!$PREFIX$))(\\/.*)(['\"]\\))"
]
}, opt );
buildRegex( options.patterns, options.prefix );
function prefixAllTheThings( file, enc, cb ) {
if ( file._contents !== null ) {
var contents = String( file._contents );
builtPatterns.forEach( function( pattern ) {
contents = contents.replace(
pattern,
"$1" + options.prefix + "$2$3"
);
} );
file._contents = new Buffer( contents );
cb( null, file );
} else {
cb();
}
}
return through.obj( prefixAllTheThings, function( cb ) {
cb( null );
} );
};