This repository has been archived by the owner on May 22, 2020. It is now read-only.
forked from zont/gulp-usemin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (128 loc) · 4.14 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
var path = require('path');
var fs = require('fs');
var EOL = require('os').EOL;
var through = require('through2');
var gutil = require('gulp-util');
var glob = require('glob');
module.exports = function(options) {
options = options || {};
var startReg = /<!--\s*build:(\w+)(?:\(([^\)]+?)\))?\s+(\/?([^\s]+?))\s*-->/gim;
var endReg = /<!--\s*endbuild\s*-->/gim;
var jsReg = /<\s*script\s+.*?src\s*=\s*"([^"]+?)".*?><\s*\/\s*script\s*>/gi;
var cssReg = /<\s*link\s+.*?href\s*=\s*"([^"]+)".*?>/gi;
var basePath, mainPath, mainName, alternatePath;
function createFile(name, content) {
return new gutil.File({
path: path.join(path.relative(basePath, mainPath), name),
contents: new Buffer(content)
})
}
function getBlockType(content) {
return jsReg.test(content) ? 'js' : 'css';
}
function getFiles(content, reg) {
var paths = [];
var files = [];
content
.replace(/<!--(?:(?:.|\r|\n)*?)-->/gim, '')
.replace(reg, function (a, b) {
var filePath = path.resolve(path.join(alternatePath || mainPath, b));
if (options.assetsDir)
filePath = path.resolve(path.join(options.assetsDir, path.relative(basePath, filePath)));
paths.push(filePath);
});
for (var i = 0, l = paths.length; i < l; ++i) {
var filepath = glob.sync(paths[i])[0];
files.push(new gutil.File({
path: filepath,
contents: fs.readFileSync(filepath)
}));
}
return files;
}
function concat(files, name) {
var buffer = [];
files.forEach(function(file) {
buffer.push(String(file.contents));
});
return createFile(name, buffer.join(EOL));
}
function processTask(index, tasks, name, files, callback) {
var newFiles = [];
if (tasks[index] == 'concat') {
newFiles = [concat(files, name)];
}
else {
var stream = tasks[index];
function write(file) {
newFiles.push(file);
}
stream.on('data', write);
files.forEach(function(file) {
stream.write(file);
});
stream.removeListener('data', write);
}
if (tasks[++index])
processTask(index, tasks, name, newFiles, callback);
else
newFiles.forEach(callback);
}
function process(name, files, pipelineId, callback) {
var tasks = options[pipelineId] || [];
if (tasks.indexOf('concat') == -1)
tasks.unshift('concat');
processTask(0, tasks, name, files, callback);
}
function processHtml(content, push, callback) {
var html = [];
var sections = content.split(endReg);
for (var i = 0, l = sections.length; i < l; ++i)
if (sections[i].match(startReg)) {
var section = sections[i].split(startReg);
alternatePath = section[2];
html.push(section[0]);
if (getBlockType(section[5]) == 'js')
process(section[4], getFiles(section[5], jsReg), section[1], function(name, file) {
push(file);
if (path.extname(file.path) == '.js')
html.push('<script src="' + name.replace(path.basename(name), path.basename(file.path)) + '"></script>');
}.bind(this, section[3]));
else
process(section[4], getFiles(section[5], cssReg), section[1], function(name, file) {
// Ulgy hack in order to support media and id attribute on our stylesheets
var cssExtraAttrReg = /(?:media|id)=['"][^"']*['"]/g;
var extraAttr = [];
var match;
while((match = cssExtraAttrReg.exec(section[5])) !== null) {
extraAttr.push(match[0]);
}
var extraAttrStr = extraAttr.join(' ');
push(file);
html.push('<link rel="stylesheet" href="' + name.replace(path.basename(name), path.basename(file.path)) + '" ' + extraAttrStr +'/>');
}.bind(this, section[3]));
}
else
html.push(sections[i]);
process(mainName, [createFile(mainName, html.join(''))], 'html', function(file) {
push(file);
callback();
});
}
return through.obj(function(file, enc, callback) {
if (file.isNull()) {
this.push(file); // Do nothing if no contents
callback();
}
else if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-usemin', 'Streams are not supported!'));
callback();
}
else {
basePath = file.base;
mainPath = path.dirname(file.path);
mainName = path.basename(file.path);
processHtml(String(file.contents), this.push.bind(this), callback);
}
});
};