forked from zont/gulp-usemin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
193 lines (159 loc) · 6.48 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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*("|')([^"']+?)\1.*?><\s*\/\s*script\s*>/gi;
var cssReg = /<\s*link\s+.*?href\s*=\s*("|')([^"']+)\1.*?>/gi;
var startCondReg = /<!--\[[^\]]+\]>/gim;
var endCondReg = /<!\[endif\]-->/gim;
var basePath, mainPath, mainName, alternatePath;
function createFile(name, content) {
var filePath = path.join(path.relative(basePath, mainPath), name);
var isStatic = name.split('.').pop() === 'js' || name.split('.').pop() === 'css';
if (options.outputRelativePath && isStatic)
filePath = options.outputRelativePath + name;
return new gutil.File({
path: filePath,
contents: new Buffer(content)
})
}
function getBlockType(content) {
return jsReg.test(content) ? 'js' : 'css';
}
function getFiles(content, reg) {
var paths = [];
var files = [];
content
.replace(startCondReg, '')
.replace(endCondReg, '')
.replace(/<!--(?:(?:.|\r|\n)*?)-->/gim, '')
.replace(reg, function (a, quote, b) {
if (options.srcPathRemove) {
for (var i = 0; i < options.srcPathRemove.length; i++) {
b = b.replace(options.srcPathRemove[i],'');
}
}
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 filepaths = glob.sync(paths[i]);
if(filepaths[0] === undefined) {
throw new gutil.PluginError('gulp-usemin', 'Path ' + paths[i] + ' not found!');
}
filepaths.forEach(function (filepath) {
files.push(new gutil.File({
path: filepath,
contents: fs.readFileSync(filepath)
}));
});
}
//console.log(filepaths);
// console.log(files);
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);
var srcPath = null;
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]);
var startCondLine = section[5].match(startCondReg);
var endCondLine = section[5].match(endCondReg);
if (startCondLine && endCondLine)
html.push(startCondLine[0]);
if (section[1] !== 'remove') {
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') {
srcPath = name.replace(path.basename(name), path.basename(file.path));
if (options.websitePath)
srcPath = options.websitePath + srcPath;
html.push('<script src="' + srcPath + '"></script>');
}
}.bind(this, section[3]));
} else {
process(section[4], getFiles(section[5], cssReg), section[1], function(name, file) {
push(file);
srcPath = name.replace(path.basename(name), path.basename(file.path));
if (options.websitePath)
srcPath = options.websitePath + srcPath;
html.push('<link rel="stylesheet" href="' + srcPath + '"/>');
}.bind(this, section[3]));
}
}
if (startCondLine && endCondLine) {
html.push(endCondLine[0]);
}
} 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);
}
});
};