forked from paazmaya/gulp-sakugawa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (59 loc) · 1.93 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
/**
* gulp-sakugawa
* https://github.com/paazmaya/gulp-sakugawa
*
* Copyright (c) Juga Paazmaya
* Licensed under the MIT license.
*/
'use strict';
// Add addImport options
// addImport == true => modify existing file to import from generated files
// through2 is a thin wrapper around node transform streams
var through = require('through2');
var File = require('vinyl');
var path = require('path');
var sakugawa = require('sakugawa');
var gutil = require('gulp-util');
var StringDecoder = require('string_decoder').StringDecoder;
module.exports = function gulpSakugawa(opts) {
var options = {
maxSelectors: opts.maxSelectors || 4090,
minSheets: opts.minSheets || 1,
mediaQueries: opts.mediaQueries || 'normal',
addImport: opts.addImport || false
};
var suffix = opts.suffix || '_';
var stream = through.obj(function(chunk, enc, cb) {
if (!chunk.isNull()) {
var _self = this;
var decoder = new StringDecoder(enc);
var css = decoder.write(chunk.contents);
var pages = sakugawa(css, options);
var imports = "";
pages.forEach(function(page, index) {
var cssFileName = chunk.relative.replace(/\.css$/, '') + suffix + (index + 1) + '.css';
imports = imports + "@import '" + cssFileName + "';\n";
// add new source map file to stream
var cssFile = new File({
cwd: chunk.cwd,
base: chunk.base,
path: path.join(chunk.base, '', cssFileName),
// path: path.join(chunk.base, '', chunk.relative) + suffix + (index + 1) + '.css',
contents: new Buffer(page)
});
_self.push(cssFile);
});
if (options.addImport) {
var cssFile = new File({
cwd: chunk.cwd,
base: chunk.base,
path: path.join(chunk.base, '', chunk.relative),
contents: new Buffer(imports)
});
_self.push(cssFile);
}
}
return cb();
});
return stream;
};