-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
591 lines (514 loc) · 18 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
* seajs(CMD) Module combo pulgin for gulp
* Author : [email protected]
* Date : 2018-03-29
*/
var Promise = require("promise"),
fs = require("fs"),
path = require("path"),
through = require("through2"),
gutil = require("gulp-util"),
execPlugins = require("./lib/execplugins"),
rFirstStr = /[\s\r\n\=]/,
rDefine = /define\(\s*(['"](.+?)['"],)?/,
rDeps = /(['"])(.+?)\1/g,
rAlias = /alias\s*\:([^\}]+)\}/,
rPaths = /paths\s*\:([^\}]+)\}/,
rVars = /vars\s*\:([^\}]+)\}/,
rVar = /\{([^{]+)}/g,
rSeajsConfig = /seajs\.config\([^\)]+\);?/g,
rModId = /([^\\\/?]+?)(\.(?:js))?([\?#].*)?$/,
rQueryHash = /[\?#].*$/,
rExistId = /define\(\s*['"][^\[\('"\{\r\n]+['"]\s*,?/,
rSeajsUse = /"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|\/\*[\S\s]*?\*\/|\/(?:\\\/|[^\/\r\n])+\/(?=[^\/])|\/\/.*|\.\s*seajs\.use|(?:^|[^$])\bseajs\.use\s*\((.+)/g,
rRequire = /"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|\/\*[\S\s]*?\*\/|\/(?:\\\/|[^\/\r\n])+\/(?=[^\/])|\/\/.*|\.\s*require|(?:^|[^$])\brequire\s*\(\s*(["'])(.+?)\1\s*\)/g;
const PLUGIN_NAME = "gulp-seajs-cmobo";
/*
* 过滤忽略模块
* param { Array } 忽略模块列表
* param { String } 模块名
* param { String } 模块标识
* return { Boolean } 是否在忽略列表中
*/
var filterIgnore = function(ignore, id, origId) {
return ignore.some(function(item) {
var arr;
// 含路径的模块id只过滤精确匹配的结果
if (~item.indexOf("/")) {
return item === origId;
}
// 不含路径的模块id将过滤所有匹配结果
// ui 将匹配 ../ui 和 ../../ui
else {
// 使用id过滤忽略模块时要去掉自动添加的 gulp-seajs-combo
if (~id.indexOf(PLUGIN_NAME)) {
arr = id.split("_");
id = arr.slice(0, -2).join("_");
}
return item === id;
}
});
},
/*
* 初始化插件
* param { Object } 老配置对象
* param { Object } 新忽略列表
*/
initPlugins = function(options, o) {
var name;
o.plugins = {};
options.plugins.forEach(function(item) {
item.ext.forEach(function(name) {
o.plugins[name] = item.use;
});
});
},
/*
* 提取config中的配置,会忽略包含变量的配置,只提取纯字符串
* param{ String } config字符串
* return{ Object } 提取出来的配置
*/
evalConfig = function(configStr) {
var configArr = configStr,
config = {};
configStr = configStr.replace(/\{/, "");
configArr = configStr.split(",");
configArr.forEach(function(item) {
var index, arr, key, value;
index = item.indexOf(":");
key = item.slice(0, index).replace(/['"]/g, "");
value = item.slice(index + 1);
key = key.trim();
value = value.trim();
try {
value = eval("(function(){return " + value + "})()");
config[key] = value;
} catch (_) {}
});
return config;
},
/*
* 解析config字符串,尝试提取alias、paths、vars
* param{ String } 文件内容
* return{ Object } 提取出来的配置和提取后的文件内容
*/
parseConfig = function(contents) {
var config = {};
contents = contents.replace(rSeajsConfig, function($) {
$.replace(rAlias, function(_, $1) {
config.alias = evalConfig($1);
});
$.replace(rPaths, function(_, $1) {
config.paths = evalConfig($1);
});
$.replace(rVars, function(_, $1) {
config.vars = evalConfig($1);
});
return "";
});
return {
contents: contents,
config: config
};
},
/*
* 基于base将依赖模块的相对路径转化成绝对路径
* 同时对seajs.config中的paths、alias、vars,还有options.map进行处理
* param { Object } 数据存储对象
* param { Array } 依赖模块的相对路径列表
* param { String } 基础路径
* return { Array } 依赖模块的绝对路径列表
*/
mergePath = function(options, deps, base) {
var config = options.config;
return deps.map(function(item, i) {
var origId = item.origId,
arr,
modId;
// 防止多次merge
if (item.path) {
return;
}
// 处理build.json => map
if (options.map && options.map[origId]) {
origId = options.map[origId];
}
// 处理seajs.config => vars
if (config.vars) {
if (~origId.indexOf("{")) {
origId = origId.replace(rVar, function($, $1) {
if (config.vars[$1]) {
return config.vars[$1];
}
return $;
});
}
}
// 处理seajs.config => alias
if (config.alias && config.alias[origId]) {
origId = config.alias[origId];
}
// 处理seajs.config => paths
if (config.paths) {
arr = origId.split("/");
modId = arr.splice(arr.length - 1, 1);
arr.forEach(function(_item, i) {
if (config.paths[_item]) {
arr[i] = config.paths[_item];
}
});
arr = arr.concat(modId);
origId = arr.join("/");
}
return {
id: item.id,
extName: item.extName,
path: path.resolve(base, origId),
origId: origId
};
});
},
/*
* 解析模块标识
* param { Object } 配置参数
* param { String } 模块标识
* return { Object } filePath: 过滤query和hash后的模块标识,id: 模块id,extName: 模块后缀
*/
modPathResolve = function(options, filePath) {
// 过滤query(?)和hash(#)
filePath = filePath.replace(rQueryHash, "");
var id = filePath.match(rModId)[1],
extName = path.extname(filePath);
if (extName && extName === ".js") {
id = id.replace(extName, "");
}
return {
id: id,
path: filePath,
extName: extName
};
},
/*
* 解析依赖模块列表,如果有依赖模块则开始解析依赖模块
* param { Object } 配置参数
* param { Array } 依赖模块
* param { promise }
*/
readDeps = function(options, parentDeps) {
var childDeps = [];
promiseArr = parentDeps.map(function(item) {
return new Promise(function(resolve, reject) {
var id = item.id,
extName = item.extName,
filePath = item.path,
origId = item.origId,
contents,
stream,
plugins,
deps,
isIgnore;
isIgnore = options.ignore ?
filterIgnore(options.ignore, id, origId) :
false;
// 检测该模块是否在忽略列表中
if (isIgnore) {
options.modArr.push({
id: id,
path: filePath,
contents: "",
extName: extName,
origId: origId
});
resolve();
return;
}
// 处理特殊的模块,如 tpl 模块(需额外的插件支持)
// 根据模块后缀来匹配是否使用插件
if (extName && !~extName.indexOf(".js") && fs.existsSync(filePath)) {
if (options.plugins && options.plugins[extName]) {
plugins = options.plugins[extName];
// 有插件则执行插件
stream = execPlugins(filePath, plugins);
stream.on("end", function() {
resolve();
});
stream.pipe(
through.obj(function(file, enc, _callback) {
parseDeps(options, file.contents.toString(), item);
_callback(null, file);
})
);
} else {
return reject("Can't combo unkonwn module [" + filePath + "]");
}
}
// 处理普通的js模块
else {
if (!extName.trim() || extName != ".js") {
filePath += ".js";
}
try {
contents = fs.readFileSync(filePath, options.encoding);
} catch (_) {
reject("File [" + filePath + "] not found.");
return;
}
deps = parseDeps(options, contents, item);
if (deps.length) {
childDeps = childDeps.concat(deps);
}
resolve();
}
});
});
return Promise.all(promiseArr)
.then(
function() {
if (childDeps.length) {
return readDeps(options, childDeps);
}
},
function(err) {
gutil.log(gutil.colors.red(PLUGIN_NAME + " Error: " + err));
}
)
.catch(function(err) {
gutil.log(gutil.colors.red(PLUGIN_NAME + " error: " + err.message));
console.log(err.stack);
});
},
/*
* 提取依赖模块
* param { Object } 配置参数
* param { RegExp } 提取正则
* param { Object } 文件内容
* return { Array } 依赖模块列表
*/
pullDeps = function(options, reg, contents) {
var deps = [],
matches,
origId;
reg.lastIndex = 0;
while ((matches = reg.exec(contents)) !== null) {
origId = matches[2];
if (origId && origId.slice(0, 4) !== "http") {
depPathResult = modPathResolve(options, origId);
deps.push({
id: depPathResult.id,
origId: depPathResult.path,
extName: depPathResult.extName
});
}
}
return deps;
},
/*
* 解析依赖模块
* param { Object } 配置参数
* param { String } 文件内容
* param { Object } 模块数据
* return { Array } 依赖模块数据列表
*/
parseDeps = function(options, contents, modData) {
var isSeajsUse = !!~contents.indexOf("seajs.use("),
id = modData.id,
deps = [],
configResult,
name,
base,
matches;
// 标准模块
if (!isSeajsUse) {
deps = pullDeps(options, rRequire, contents);
}
// 解析seajs.use
else {
configResult = parseConfig(contents);
contents = configResult.contents;
for (name in configResult.config) {
options.config[name] = configResult.config[name];
}
matches = contents.match(rSeajsUse);
matches.forEach(function(item) {
var _deps = [];
if (~item.indexOf("seajs.use")) {
_deps = pullDeps(options, rDeps, item);
deps = deps.concat(_deps);
}
});
}
base = path.resolve(modData.path, "..");
deps = mergePath(options, deps, base);
options.modArr.push({
id: id,
deps: deps,
path: modData.path,
contents: contents,
extName: modData.extName,
origId: modData.origId || id
});
return deps;
},
/*
* 转换模块内容
* param { Object } 配置参数
* param { Object } 模块数据
* param { Object } id映射表
* return { String } 文件内容
*/
transform = function(options, modData, idMap) {
var contents = modData.contents,
isSeajsUse = !!~contents.indexOf("seajs.use("),
origId = modData.origId,
deps = [];
// 标准模块
if (!isSeajsUse) {
contents = contents.replace(rRequire, function($, _, $2) {
var result = $,
depId,
depOrigId,
depPathResult,
firstStr;
if ($2 && $2.slice(0, 4) !== "http") {
depPathResult = modPathResolve(options, $2);
firstStr = result.charAt(0);
depOrigId = depPathResult.path;
depId = idMap[depOrigId] || depPathResult.id;
deps.push(depId);
result = "require('" + depId + "')";
if (rFirstStr.test(firstStr)) {
result = firstStr + result;
}
}
return result;
});
// 为匿名模块添加模块名,同时将依赖列表添加到头部
contents = contents.replace(rDefine, function() {
var id = idMap[origId];
return deps.length ?
"define('" + id + "',['" + deps.join("','") + "']," :
"define('" + id + "',";
});
} else {
contents = contents.replace(rSeajsUse, function($) {
var result = $;
if (~$.indexOf("seajs.use(")) {
result = $.replace(rDeps, function($, _, $2) {
var _result = $,
depPathResult,
depId;
if ($2 && $2.slice(0, 4) !== "http") {
depPathResult = modPathResolve(options, $2);
depId = depPathResult.id;
_result = "'" + depId + "'";
}
return _result;
});
}
return result;
});
}
return contents;
},
/*
* 合并模块内容
* param { Object } 配置参数
* return { String } 文件内容
*/
comboContent = function(options) {
var idUnique = {},
pathUnique = {},
contents = "",
idMap = {},
newModArr = [];
options.modArr.forEach(function(item, i) {
var obj = {},
id = item.id,
filePath = item.path;
if (!pathUnique[filePath]) {
pathUnique[filePath] = true;
newModArr.push(item);
if (idUnique[id]) {
id = id + "_" + PLUGIN_NAME + "_" + i;
} else {
idUnique[id] = true;
}
idMap[item.origId] = id;
}
});
newModArr.forEach(function(item) {
var newContents = transform(options, item, idMap);
if (newContents) {
contents = newContents + "\n" + contents;
}
if (options.verbose) {
gutil.log(
"gulp-seajs-combo:",
"✔ Module [" + filePath + "] combo success."
);
}
});
return new Buffer(contents);
},
/*
* 解析模块的内容,如果有依赖模块则开始解析依赖模块
* param { Object } 数据存储对象
* param { String } 文件内容
* param { String } 模块的绝对路径
* param { promise }
*/
parseContent = function(options, contents, filePath) {
return new Promise(function(resolve) {
var pathResult = modPathResolve(options, filePath),
deps = parseDeps(options, contents, pathResult);
if (deps.length) {
resolve(readDeps(options, deps));
} else {
resolve();
}
});
},
// 插件入口函数
createStream = function(options) {
var o = {
modArr: [],
config: {},
unique: {},
uuid: 0,
contents: "",
encoding: "UTF-8",
verbose: !!~process.argv.indexOf("--verbose")
};
if (options) {
if (options.ignore) {
o.ignore = options.ignore;
}
if (options.map) {
o.map = options.map;
}
if (options.encoding) {
o.encoding = options.encoding;
}
if (options.plugins) {
initPlugins(options, o);
}
}
return through.obj(function(file, enc, callback) {
if (file.isBuffer()) {
parseContent(o, file.contents.toString(), file.path)
.then(function() {
var contents = comboContent(o);
file.contents = contents;
callback(null, file);
})
.catch(function(err) {
gutil.log(gutil.colors.red(PLUGIN_NAME + " error: " + err.message));
console.log(err.stack);
callback(null, file);
});
} else {
callback(null, file);
}
});
};
module.exports = createStream;