-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (117 loc) · 4.06 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var fs = require('fs'),
path = require('path'),
gutil = require('gulp-util'),
PluginError = gutil.PluginError,
through = require('through2'),
defaults = require('lodash.defaults'),
parseCss = require('css-parse');
var PLUGIN_NAME = 'gulp-extract-css';
module.exports = function (options) {
'use strict';
// Default options
var options = defaults(options || {}, {
log: false,
takeout: []
});
// Log info only when 'options.log' is set to true
var log = function (message) {
if (options.log) {
gutil.log(message);
}
};
// Define a counter that counts the rules that are iterated over
var rulecount = 0;
// Distribute content to the cssfiles object
function distribute(filename, content){
content = content;
fs.appendFile(filename, content + "\n");
log(rulecount + ': Append to ' + filename + ' -> ' + content);
}
// Check if a rule has selectors
function hasSelectors(rule){
return rule.selectors.length > 0;
}
// Wrap output inside media query
function wrapMedia(mq, output){
return '@media ' + mq + ' { ' + output + ' }';
}
// Detect weather a selector should be extracted or not
function detectTakeout(selectors){
var properties = {
takeout: false
};
options.takeout.forEach(function (takeout) {
selectors.forEach(function (selector) {
if (selector.indexOf(takeout.ruleprefix) === 0) {
properties.takeout = true;
properties.filename = takeout.filename;
}
});
});
return properties;
}
function transform(file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported'));
return cb();
}
// Check if configured to extract styles
if (options.takeout && options.takeout.length > 0) {
var filename = path.relative(file.cwd, file.path);
var source = file.contents.toString('utf8');
var cssJson = parseCss(source);
var outputDir = path.relative(file.cwd, path.dirname(file.path));
// Prepare files
fs.writeFile(filename, '');
options.takeout.forEach(function (takeout) {
fs.writeFile(outputDir + '/' + takeout.filename, '');
});
cssJson.stylesheet.rules.forEach(function(rule){
if (rule.type === "media"){
rule.rules.forEach(function(subrule) {
if (typeof subrule.selectors !== 'undefined' && typeof subrule.declarations !== 'undefined'){
var declarations = '';
subrule.declarations.forEach(function(declaration){
if (declaration.type === 'declaration'){
declarations += declaration.property + ': ' + declaration.value + ';';
}
});
var styles = subrule.selectors + ' { ' + declarations + ' } ';
var check = detectTakeout(subrule.selectors);
if (check.takeout){
distribute(outputDir + '/' + check.filename, wrapMedia(rule.media, styles));
} else {
distribute(filename, wrapMedia(rule.media, styles));
}
}
});
} else {
if (typeof rule.selectors !== 'undefined' && typeof rule.declarations !== 'undefined') {
var declarations = '';
rule.declarations.forEach(function(declaration){
if (declaration.type === 'declaration'){
declarations += declaration.property + ': ' + declaration.value + ';';
}
});
var styles = rule.selectors + ' { ' + declarations + ' } ';
var check = detectTakeout(rule.selectors);
if (check.takeout){
distribute(outputDir + '/' + check.filename, styles);
} else {
distribute(filename, styles);
}
}
}
rulecount++;
});
} else {
log(PLUGIN_NAME + ' is not configured to extract anything');
}
cb();
}
return through.obj(transform);
};