This repository has been archived by the owner on Mar 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
245 lines (206 loc) · 7.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var path = require('path');
var fs = require('fs');
var finder = require('fs-finder');
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 = /script.+src\s*=\s*['"]([^"']+)['"]/gim;
var cssReg = /link.+href\s*=\s*['"]([^"']+)['"]/gim;
var startCondReg = /<!--\[[^\]]+\]>/gim;
var endCondReg = /<!\[endif\]-->/gim;
var patterns = [
/img.+src\s*=\s*['"]+([\.\/]+[^"']+)['"]/gim,
/link.+href\s*=\s*['"]+([\.\/]+[^"']+)['"]/gim,
/script.+src\s*=\s*['"]+([\.\/]+[^"']+)['"]/gim,
/meta.+content\s*=\s*['"]+([\.\/]+[^"']+)['"]/gim,
/data-src\s*=\s*['"]+([\.\/]+[^"']+)['"]/gim,
/data-at2x\s*=\s*['"]+([\.\/]+[^"']+)['"]/gim,
/video.+src\s*=\s*['"]+([\.\/]+[^"']+)['"]/gim,
/video.+poster\s*=\s*['"]+([\.\/]+[^"']+)['"]/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)
if (isStatic) {
filePath = options.outputRelativePath + name;
if (options.outputRelativePath)
filePath = path.join(options.outputRelativePath, name);
if (options.outputBasePath)
filePath = path.join(options.outputBasePath, filePath);
}
return new gutil.File({
path: filePath,
contents: new Buffer(content)
});
}
function getBlockType(content) {
var result = jsReg.test(content) ? 'js' : 'css';
jsReg.lastIndex = 0;
return result;
}
function getFiles(content, reg) {
var paths = [];
var files = [];
content
.replace(startCondReg, '')
.replace(endCondReg, '')
.replace(/<!--(?:(?:.|\r|\n)*?)-->/gim, '')
.replace(reg, function (a,b) {
var filePath = path.resolve(path.join(alternatePath || options.path || 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-jade-usemin', 'Path ' + paths[i] + ' not found!');
}
filepaths.forEach(pushFile)
}
function pushFile(filepath) {
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 = [];
function writeNewFile(file) {
newFiles.push(file);
}
if (tasks[index] === 'concat') {
newFiles = [concat(files, name)];
}
else {
var stream = tasks[index];
if (options.maxListeners) {
stream.setMaxListeners(options.maxListeners);
}
stream.on('error', function(err) {
console.log(err);
throw new gutil.PluginError('gulp-jade-usemin', err.message);
});
stream.on('data', writeNewFile);
stream.on('end', function() {
stream.removeListener('data', writeNewFile);
});
files.forEach(function(file) {
stream.write(file);
});
}
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 renderAttributes(content, url) {
var attributes = [];
if( getBlockType(content) === 'js' ){
attributes = content.match(/((?!src|type\b)\b\w+)=("[^<>"]*"|'[^<>']*'|\w+)/gi) || [];
attributes.push("src='" + url + "'");
attributes.push("type='text/javascript'");
} else if( getBlockType(content) === 'css'){
attributes = content.match(/((?!href|rel\b)\b\w+)=("[^<>"]*"|'[^<>']*'|\w+)/gi) || [];
attributes.push("href='" + url + "'");
attributes.push("rel='stylesheet'");
}
return attributes.join(", ");
}
function processJade(content, push, callback) {
var jade = [];
var sections = content.split(endReg);
function jsRegPush(name, file) {
push(file);
name = options.outputRelativePath ? path.join(options.outputRelativePath, name) : name;
if (path.extname(file.path) === '.js')
jade.push('script(' + renderAttributes(section[5], name.replace(path.basename(name), path.basename(file.path))) + ' )');
}
function cssRegPush(name, file) {
push(file);
name = options.outputRelativePath ? path.join(options.outputRelativePath, name) : name;
if (path.extname(file.path) === '.css')
jade.push('link(' + renderAttributes(section[5], name.replace(path.basename(name), path.basename(file.path))) + ' )');
}
function patternReplace(pattern) {
sections[i].replace(pattern, function(match, src) {
var masked = src.replace(path.extname(src), '.*' + path.extname(src));
if(options.assetsDir){
var file = finder.from(options.assetsDir).findFirst().findFiles(masked);
if(file) {
var revved = file.replace(options.assetsDir, options.outputRelativePath ? options.outputRelativePath : '');
sections[i] = sections[i].replace(src, revved);
}
}
});
}
for (var i = 0, l = sections.length; i < l; ++i) {
if (sections[i].match(startReg)) {
var section = sections[i].split(startReg);
alternatePath = section[2];
jade.push(section[0]);
var startCondLine = section[5].match(startCondReg);
var endCondLine = section[5].match(endCondReg);
if (startCondLine && endCondLine)
jade.push(startCondLine[0]);
if (section[1] !== 'remove') {
if (getBlockType(section[5]) === 'js') {
process(section[4], getFiles(section[5], jsReg), section[1], jsRegPush.bind(this, section[3]));
} else {
process(section[4], getFiles(section[5], cssReg), section[1], cssRegPush.bind(this, section[3]));
}
}
if (startCondLine && endCondLine) {
jade.push(endCondLine[0]);
}
} else {
patterns.forEach(patternReplace)
//sections[i] = sections[i].replace(/(append|prepend) scripts/gi, 'block scripts');
//sections[i] = sections[i].replace(/(append|prepend) stylesheets/gi, 'block stylesheets');
jade.push(sections[i]);
}
}
process(mainName, [createFile(mainName, jade.join(''))], 'jade', 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-jade-usemin', 'Streams are not supported!'));
callback();
}
else {
basePath = file.base;
mainPath = path.dirname(file.path);
mainName = path.basename(file.path);
processJade(String(file.contents), this.push.bind(this), callback);
}
});
};