-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcss_wrap.js
50 lines (50 loc) · 1.36 KB
/
css_wrap.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
49
50
/*
* css-wrap
* https://github.com/benignware/css-wrap
*
* @See https://github.com/benignware/grunt-css-wrap
*
* Forked and enhanced
* https://github.com/zanzamar/grunt-css-wrap
*
* Copyright (c) 2014 Rafael Nowrotek
* Licensed under the MIT license.
*
* Copyright (c) 2014 Zanzamar
*
*/
var
path = require( 'path' ),
fs = require( 'fs' ),
deepmerge = require( 'deepmerge' ),
css_parse = require( 'css-parse' ),
css_stringify = require( 'css-stringify' ),
processRules = function( list, options ) {
return list.map( function( r ) {
if ( r.selectors ) {
r.selectors.forEach( function( s, index ) {
if (options.skip && options.skip.test(s)) return
var selector = options.selector ? options.selector + " " + s : s;
r.selectors[ index ] = selector;
});
}
if ( r.type === "media" ) {
r.rules = processRules( r.rules, options );
}
return r;
});
},
css_wrap = function( string, options ) {
options = deepmerge({
// Defaults
selector: ".css-wrap",
skip: null
}, options || {});
if (fs.existsSync(path.resolve(string))) {
string = fs.readFileSync(string).toString();
}
var css = css_parse( string );
css.stylesheet.rules = processRules( css.stylesheet.rules, options );
return css_stringify( css );
};
module.exports = css_wrap;