-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (71 loc) · 2.76 KB
/
index.js
File metadata and controls
89 lines (71 loc) · 2.76 KB
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
'use strict';
var through = require('through'),
gutil = require('gulp-util'),
fs = require('fs'),
path = require('path');
module.exports = function (opts) {
opts = opts || {};
opts.js = 'js' in opts ? opts.js : true;
opts.css = 'css' in opts ? opts.css : false;
opts.cwd = 'cwd' in opts ? opts.cwd : false;
function findJavascriptResources(htmlStr) {
var buildTag = typeof opts.js === 'string' ? opts.js : 'js',
BUILD_REGEX = new RegExp('<!-- build:' + buildTag + ' -->([\\s\\S]*?)<!-- endbuild -->', 'g'),
JS_REGEX = /<script.*?src=(?:'|")(.*?)(?:'|")/g,
buildStr = BUILD_REGEX.exec(htmlStr),
resultsArray = [],
matchArray;
while (matchArray = JS_REGEX.exec(buildStr === null ? htmlStr : buildStr[1])) {
resultsArray.push(matchArray[1]);
}
return resultsArray;
}
function findCSSResources(htmlStr) {
var buildTag = typeof opts.css === 'string' ? opts.css : 'css',
BUILD_REGEX = new RegExp('<!-- build:' + buildTag + ' -->([\\s\\S]*?)<!-- endbuild -->', 'g'),
CSS_REGEX = /<link.*?href=(?:'|")(.*?\.css)(?:'|")/gi,
buildStr = BUILD_REGEX.exec(htmlStr),
resultsArray = [],
matchArray;
while (matchArray = CSS_REGEX.exec(buildStr === null ? htmlStr : buildStr[1])) {
resultsArray.push(matchArray[1]);
}
return resultsArray;
}
return through(function (file) {
if (!(file.contents instanceof Buffer)) {
return this.emit('error', new gutil.PluginError('gulp-assets', 'Streams not supported'));
}
if (opts.cwd === false) {
opts.cwd = file.base;
} else {
opts.cwd = path.join(file.base, opts.cwd);
}
var htmlContent = String(file.contents),
currentStream = this,
filesSrc = [];
if (opts.js) {
filesSrc = filesSrc.concat(findJavascriptResources(htmlContent));
}
if (opts.css) {
filesSrc = filesSrc.concat(findCSSResources(htmlContent));
}
filesSrc.forEach(function (fileSrc) {
var filePath = path.join(opts.cwd, fileSrc);
if(fs.existsSync(filePath)) {
currentStream.queue(new gutil.File({
base: file.base,
cwd: file.cwd,
path: filePath,
contents: fs.readFileSync(filePath)
}));
}
});
});
};
module.exports.js = function(tag) {
return this({ css: false, js: typeof tag === 'undefined' ? true : tag });
};
module.exports.css = function(tag) {
return this({ css: typeof tag === 'undefined' ? true : tag, js: false });
};