-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
68 lines (57 loc) · 1.97 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
'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var Buffer = require('buffer').Buffer;
var path = require('path');
var fs = require('fs');
var File = gutil.File;
var glob = require("glob");
var regx_js = /\s*<script.*[^'|"]+['|"]([^'|"]*\.js)['|"].*/i
function jsContent(fileContent, options){
var contentArr = [];
var contArr = fileContent.split('document.write');
contArr.forEach(function(jsFile){
var jsFileArr = jsFile.match(regx_js);
if(!jsFileArr) return
var filePath = path.join(options.relativeUrls, jsFileArr[1]);
filePath = glob.sync(filePath).toString();
var content = fs.readFileSync(filePath).toString();
contentArr.push(content);
});
return contentArr.join(';')
}
function cssContent(fileContent, file, options){
var contentArr = [];
var cbFilePath = file.path.substring(0, file.path.lastIndexOf(path.sep));
var contArr = fileContent.split('@import');
contArr.forEach(function(cssFile){
var cssFileArr = cssFile.match(/url\s*\(\s*(?:['|"]?)([\w\/_\.:\-]+\.css)(?:\?[^'|")]*)?(?:['|"]?)\s*\)\s*[;]?/);
if(!cssFileArr) return;
var filePath = path.resolve(cbFilePath, cssFileArr[1]);
var content = fs.readFileSync(filePath).toString();
contentArr.push(content);
});
return contentArr.join('')
}
module.exports = function (options) {
var firstFile;
var content;
return through.obj(function (file, enc, cb){
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gule-file-concat', 'Streaming not supported'));
return cb();
}
if(firstFile) firstFile = file;
var fileName = path.basename(file.path);
var ext = fileName.split('.').pop().toLowerCase();
var fileContent = String(file.contents.toString());
content = ext == 'js' ? jsContent(fileContent, options) : cssContent(fileContent, file, options);
file.contents = new Buffer(content);
this.push(file);
cb();
});
};