forked from Bitaru/gulp-rev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
173 lines (139 loc) · 3.79 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
var crypto = require('crypto');
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var objectAssign = require('object-assign');
var file = require('vinyl-file');
var _SALT_ = '';
function md5(str) {
return crypto.createHash('md5').update(_SALT_+str).digest('hex');
}
function relPath(base, filePath) {
if (filePath.indexOf(base) !== 0) {
return filePath.replace(/\\/g, '/');
}
var newPath = filePath.substr(base.length).replace(/\\/g, '/');
if (newPath[0] === '/') {
return newPath.substr(1);
}
return newPath;
}
function getManifestFile(opts, cb) {
file.read(opts.path, opts, function (err, manifest) {
if (err) {
// not found
if (err.code === 'ENOENT') {
cb(null, new gutil.File(opts));
} else {
cb(err);
}
return;
}
cb(null, manifest);
});
}
function transformFilename(file) {
// save the old path for later
file.revOrigPath = file.path;
file.revOrigBase = file.base;
var hash = file.revHash = md5(file.contents).slice(0, 8);
var ext = path.extname(file.path);
var filename = path.basename(file.path, ext) + '-' + hash + ext;
file.path = path.join(path.dirname(file.path), filename);
}
var plugin = function (salt) {
var sourcemaps = [];
var pathMap = {};
_SALT_ = salt;
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError('gulp-rev', 'Streaming not supported'));
return;
}
// This is a sourcemap, hold until the end
if (path.extname(file.path) === '.map') {
sourcemaps.push(file);
cb();
return;
}
var oldPath = file.path;
transformFilename(file);
pathMap[oldPath] = file.revHash;
cb(null, file);
}, function(cb) {
sourcemaps.forEach(function (file) {
var reverseFilename;
// attempt to parse the sourcemap's JSON to get the reverse filename
try {
reverseFilename = JSON.parse(file.contents.toString()).file;
} catch (err) {}
if (!reverseFilename) {
reverseFilename = path.relative(path.dirname(file.path), path.basename(file.path, '.map'));
}
if (pathMap[reverseFilename]) {
// save the old path for later
file.revOrigPath = file.path;
file.revOrigBase = file.base;
var hash = pathMap[reverseFilename];
var origPath = path.join(path.dirname(file.path), path.basename(file.path, '.map'));
var ext = path.extname(origPath);
var filename = path.basename(origPath, ext) + '-' + hash + ext + '.map';
file.path = path.join(path.dirname(origPath), filename);
} else {
transformFilename(file);
}
this.push(file);
}, this);
cb();
});
};
plugin.manifest = function (pth, opts) {
if (typeof pth === 'string') {
pth = {path: pth};
}
opts = objectAssign({
path: 'rev-manifest.json',
merge: false
}, opts, pth);
var firstFile = null;
var manifest = {};
return through.obj(function (file, enc, cb) {
// ignore all non-rev'd files
if (!file.path || !file.revOrigPath) {
cb();
return;
}
firstFile = firstFile || file;
var newPath = relPath(firstFile.base, file.path);
manifest[newPath.replace('-'+file.revHash, '')] = (opts.prefix || '') + newPath;
cb();
}, function (cb) {
// no need to write a manifest file if there's nothing to manifest
if (Object.keys(manifest).length === 0) {
cb();
return;
}
getManifestFile(opts, function (err, manifestFile) {
if (err) {
cb(err);
return;
}
if (opts.merge && !manifestFile.isNull()) {
var oldManifest = {};
try {
oldManifest = JSON.parse(manifestFile.contents.toString());
} catch (err) {}
manifest = objectAssign(oldManifest, manifest);
}
manifestFile.contents = new Buffer(JSON.stringify(manifest, null, ' '));
this.push(manifestFile);
cb();
}.bind(this));
});
};
module.exports = plugin;