-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
67 lines (55 loc) · 1.72 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
"use strict";
var _ = require("lodash");
var through = require("through2");
var awspublish = require("gulp-awspublish");
var passThroughStream = require("./lib/utils/passThroughStream");
var applyCacheHeaders = require("./lib/utils/applyCacheHeaders");
var initFile = require("./lib/utils/initFile");
var cacheDefaults = {
cacheTime: null,
public: true,
allowTransform: false,
useExpires: false
};
module.exports = function (options) {
var cacheOptions = _.extend({}, cacheDefaults, options.cache);
var routes = _.map(options.routes, function (value, key) {
if ( typeof value === "string" ) {
value = { key: value };
}
return _.extend({}, cacheOptions, {
route: key,
routeMatcher: new RegExp(key),
key: "$&",
gzip: false
}, value);
});
return through.obj(function (file, encoding, callback) {
if ( file.isNull() ) {
callback();
return;
}
var self = this;
initFile(file);
var route = _.find(routes, function (route) {
return route.routeMatcher.test(file.relative);
});
file.s3.path = file.s3.path.replace(route.routeMatcher, route.key);
applyCacheHeaders(file, route);
_.extend(file.s3.headers, route.headers);
if ( route.gzip ) {
if ( route.gzip === true ) {
route.gzip = {};
}
passThroughStream({
target: self,
modifier: awspublish.gzip(route.gzip),
callback: callback,
file: file
});
} else {
self.push(file);
callback();
}
});
};