1 |
|
2 | (function() {
|
3 | var CleanCSS, RootsUtil, crypto, fs, minimatch, path, _;
|
4 |
|
5 | fs = require('fs');
|
6 |
|
7 | path = require('path');
|
8 |
|
9 | _ = require('lodash');
|
10 |
|
11 | minimatch = require('minimatch');
|
12 |
|
13 | CleanCSS = require('clean-css');
|
14 |
|
15 | crypto = require('crypto');
|
16 |
|
17 | RootsUtil = require('roots-util');
|
18 |
|
19 | module.exports = function(opts) {
|
20 | var CSSPipeline;
|
21 | opts = _.defaults(opts, {
|
22 | files: 'assets/css/**',
|
23 | out: false,
|
24 | minify: false,
|
25 | hash: false,
|
26 | opts: {}
|
27 | });
|
28 | return CSSPipeline = (function() {
|
29 |
|
30 | |
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function CSSPipeline(roots) {
|
39 | var _base;
|
40 | this.roots = roots;
|
41 | this.category = 'css-pipeline';
|
42 | this.contents = '';
|
43 | this.util = new RootsUtil(this.roots);
|
44 | if ((_base = this.roots.config).locals == null) {
|
45 | _base.locals = {};
|
46 | }
|
47 | this.roots.config.locals.css = (function(_this) {
|
48 | return function() {
|
49 | var files, paths;
|
50 | paths = [];
|
51 | if (opts.out) {
|
52 | paths.push(opts.out);
|
53 | } else {
|
54 | files = _this.util.files(opts.files);
|
55 | files = files.map(function(f) {
|
56 | return path.sep + _this.util.output_path(f.relative, 'css').relative;
|
57 | });
|
58 | paths = paths.concat(files);
|
59 | }
|
60 | return paths.map(function(p) {
|
61 | return "<link rel='stylesheet' href='" + p + "' />";
|
62 | }).join("\n");
|
63 | };
|
64 | })(this);
|
65 | }
|
66 |
|
67 |
|
68 | |
69 |
|
70 |
|
71 |
|
72 | CSSPipeline.prototype.fs = function() {
|
73 | return {
|
74 | extract: true,
|
75 | detect: function(f) {
|
76 | return minimatch(f.relative, opts.files);
|
77 | }
|
78 | };
|
79 | };
|
80 |
|
81 |
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 | CSSPipeline.prototype.compile_hooks = function() {
|
88 | return {
|
89 | after_file: (function(_this) {
|
90 | return function(ctx) {
|
91 | if (opts.out) {
|
92 | return _this.contents += ctx.content;
|
93 | }
|
94 | };
|
95 | })(this),
|
96 | write: function() {
|
97 | return !opts.out;
|
98 | }
|
99 | };
|
100 | };
|
101 |
|
102 |
|
103 | |
104 |
|
105 |
|
106 |
|
107 | CSSPipeline.prototype.category_hooks = function() {
|
108 | return {
|
109 | after: (function(_this) {
|
110 | return function(ctx) {
|
111 | var hash, res;
|
112 | if (!opts.out) {
|
113 | return;
|
114 | }
|
115 | if (opts.minify) {
|
116 | _this.contents = (new CleanCSS(opts.opts)).minify(_this.contents);
|
117 | }
|
118 | if (opts.hash) {
|
119 | hash = crypto.createHash('md5').update(_this.contents, 'utf8');
|
120 | res = opts.out.split('.');
|
121 | res.splice(-1, 0, hash.digest('hex'));
|
122 | opts.out = res.join('.');
|
123 | }
|
124 | return _this.util.write(opts.out, _this.contents);
|
125 | };
|
126 | })(this)
|
127 | };
|
128 | };
|
129 |
|
130 | return CSSPipeline;
|
131 |
|
132 | })();
|
133 | };
|
134 |
|
135 | }).call(this);
|