-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (78 loc) · 2.62 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
'use strict';
var through = require('through2'),
gutil = require('gulp-util');
var rework = require('rework'),
url = require('rework-plugin-url');
var path = require('path'),
fs = require('fs'),
urlNode = require('url');
var revHash = require('rev-hash'),
revPath = require('rev-path'),
modifyFilename = require('modify-filename');
function transformFilename(file) {
file.revOrigPath = file.path;
file.revOrigBase = file.base;
file.revHash = revHash(file.contents);
file.path = modifyFilename(file.path, function(filename, extension) {
var extIndex = filename.indexOf('.');
filename = extIndex === -1 ?
revPath(filename, file.revHash) :
revPath(filename.slice(0, extIndex), file.revHash) + filename.slice(extIndex);
return filename + extension;
});
}
var PLUGIN_NAME = 'nodelol';
module.exports = {
append: function() {
var isInline = new RegExp("^data:", "i");
var isURL = new RegExp("^http", "i");
var subDirectory = 'assets';
var assets = [];
return through.obj(function(file, enc, cb) {
if (file.isStream()) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streams are not supported!'));
return cb();
}
if (file.isBuffer()) {
var example = rework(file.contents.toString('utf8'), {
source: 'source.css'
})
.use(url(function(url) {
if (isInline.test(url) || isURL.test(url)) return url;
var newFile = urlNode.parse(url);
var dirname = path.dirname(file.path);
var fileRequired = path.resolve(dirname, newFile.pathname);
var newFileVinyl = file.clone();
newFileVinyl.path = path.join(file.bundleOptions.bundleName, path.basename(newFile.pathname));
newFileVinyl.contents = fs.readFileSync(fileRequired);
transformFilename(newFileVinyl);
assets.push(newFileVinyl);
return urlNode.format({
pathname: path.join(path.basename(file.bundleOptions.bundleName), path.basename(newFileVinyl.path)),
hash: newFile.hash
});
}))
.toString({
sourcemap: false
});
var newFile = file.clone();
newFile.contents = new Buffer(example);
newFile._assets = assets;
this.push(newFile);
cb();
}
});
},
addMissingAssets: function(rev) {
return through.obj(function(file, enc, cb) {
if (file._assets) {
var self = this;
file._assets.forEach(function(file, index) {
self.push(file);
});
}
this.push(file);
cb();
});
}
};