This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
forked from thetoke/navicon-transformicons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.js
202 lines (168 loc) · 5.33 KB
/
builder.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
var gulp = require('gulp'),
gutil = require('gulp-util'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
autoprefixer = require('gulp-autoprefixer'),
sass = require('gulp-sass'),
_ = require('lodash'),
url = require('url'),
deps = require('./deps.json'),
FILENAME = 'transformicons',
ROOT = __dirname + '/dist/',
ROOT_SCSS = ROOT + '/sass/',
TEMPLATES = __dirname + '/site/templates/includes/tcons/',
BASE = '/build/',
MSG_START = 'WARN: "',
MSG_END = '" transform is not defined in deps.json',
cacheFile = {},
contentMap = {
'css': {
build: buildCSS,
mime : 'text/css'
},
'scss': {
build: buildSCSS,
mime: 'text/plain'
},
'js': {
build: buildJS,
mime: 'text/javascript'
},
'html': {
build: buildHTML,
mime : 'text/html'
}
};
// Dependencies for all transformations
function getDependencies(params) {
var files = ['base/_config-globals.scss',
'base/_global-styles.scss',
'base/_config-utilities.scss'];
for(var initial in params) {
// only array to simplify processing
if (params[initial] && !Array.isArray(params[initial])) {
params[initial] = [params[initial]];
}
params[initial].forEach(function(target) {
if (deps[initial]) {
if (deps[initial][target]) {
deps[initial][target].forEach(function(file) {
files.push(file);
});
} else {
console.warn(MSG_START + initial + '" to "' + target + MSG_END);
}
} else {
console.warn(MSG_START + initial + MSG_END);
}
});
}
return _.uniq(files);
}
function getFiles(params) {
return getDependencies(params).map(function(path) {
return ROOT_SCSS + path;
});
}
function buildHTML(params, key, cb) {
var markupFiles = [];
for(var initial in params) {
// only array to simplify processing
if (params[initial] && !Array.isArray(params[initial])) {
params[initial] = [params[initial]];
}
params[initial].forEach(function(target) {
markupFiles.push(TEMPLATES + initial + '-' + target + '.hbs');
});
}
// TODO: use assemble with layout for button if provide
gulp.src(markupFiles)
.pipe(concat('./markup' + new Date().getTime()))
.pipe(gutil.buffer(function(err, data) {
console.log(err);
cb && cb(err, key, data);
}));
}
function buildStylesheet(isCSS, params, cb) {
gulp.src(getFiles(params))
.pipe(concat('./tmp' + new Date().getTime()))
.pipe(isCSS ? sass() : gutil.noop())
.pipe(isCSS ? autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}) : gutil.noop())
.pipe(gutil.buffer(function(err, data) {
cb && cb(err, data);
}));
}
function buildSCSS(params, key, cb) {
buildStylesheet(false, params, function(err, data) {
cb && cb(err, key, data);
});
}
function buildCSS(params, key, cb) {
buildStylesheet(true, params, function(err, data) {
cb && cb(err,key,data);
});
}
function buildJS(params, key, cb) {
gulp.src(ROOT + '/js/' + FILENAME + '.js')
.pipe(params.minified === 'true' ? uglify() : gutil.noop())
.pipe(gutil.buffer(function(err, files) {
if (err) {
cb & cb(err);
return;
}
cb && cb(null, key, files);
}));
}
function process(res, data) {
var contents = data.buffer[0]._contents;
if (data.minified) {
data.filename += '.min';
}
res.setHeader('Content-Disposition', 'attachment; filename="' + data.filename + '.' + data.type + '"');
res.setHeader('Content-Type', contentMap[data.type].mime);
res.setHeader('Content-Length', contents.length);
res.end(contents);
}
exports.middleware = function() {
return function(req, res, next) {
var parseUrl = url.parse(req.url);
pathname = parseUrl.pathname,
key = parseUrl.path,
filename = FILENAME;
if (pathname.indexOf(BASE) === -1) {
next();
return;
}
var type = pathname.substring(BASE.length, pathname.length);
if (!type || !contentMap[type]) {
next();
return;
}
if (cacheFile[key]) {
process(res, cacheFile[key]);
next();
return;
}
contentMap[type].build(req.query, key, function(err, key, buffer) {
if (err) {
console.log(err);
next();
return;
}
var data = {
buffer: buffer,
filename: filename,
type: type,
minified: !!req.query.minified
};
if (buffer) {
process(res, data);
}
cacheFile[key] = data;
next();
});
};
};