/
index.js
67 lines (54 loc) · 1.85 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 gutil = require('gulp-util');
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
var slash = require('slash');
var jsonFile = require('jsonfile')
var through = require('through');
function bundlemanifest(options) {
options = _.defaults(options || {}, {
dest: process.cwd(),
filename: 'webpack-stats.json'
});
var outputFilePath = path.resolve(options.dest, options.filename);
var hashes = {
"chunks": {}
};
var pluginName = 'gulp-webpack-bundle-tracker';
function computePaths(file) {
if (file.named === undefined) {
this.emit('error', new gutil.PluginError(pluginName, 'Stream must be named. See https://github.com/shama/vinyl-named'));
}
if (file.isNull()) {
return;
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError(pluginName, 'Streams not supported'));
return;
}
var filePath = path.resolve(options.dest, file.path);
var relativeFilePath = slash(path.relative(path.dirname(outputFilePath), filePath));
if (hashes.chunks[file.named] === undefined) { hashes.chunks[file.named] = [] };
hashes.chunks[file.named].push({
"name": path.basename(file.path),
"path": file.path
});
this.push(file);
}
function writePaths() {
var self = this;
if (!fs.existsSync(outputFilePath)) {
mkdirp(path.dirname(outputFilePath));
}
jsonFile.writeFile(outputFilePath, hashes, function (err) {
if (err) {
self.emit('error', new gutil.PluginError(pluginName, err));
}
});
this.emit('end');
}
return through(computePaths, writePaths);
}
module.exports = bundlemanifest;