-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (44 loc) · 1.59 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
const through = require('through2');
const PluginError = require('plugin-error');
const PLUGIN_NAME = 'gulp-uncomment-code';
/**
* 在开发环境下, uncomment用于测试目的的代码块
*
* @param options
* @param options.startComment [uncomment-in-develop-start] 代码块开始标识
* @param options.endComment [uncomment-in-develop-end] 代码块结束标识
*
**/
module.exports = function(options) {
const defaultOptions = {
startComment: 'uncomment-in-development-start',
endComment: 'uncomment-in-development-end',
};
const opts = Object.assign({}, defaultOptions, options);
const {
startComment,
endComment,
} = opts;
const reg = new RegExp(`(?:[\\t\\ ]*)(?:\\/\\/|\\/\\*)(?:[\\t\\ ]*)${startComment}(?:[\\t\\ ]*)(?:\\*\\/)*\n([\\s\\S]*?)(?:[\\t\\ ]*)(?:\\/\\/|\\/\\*)*(?:[\\t\\ ]*)${endComment}(?:[\\t\\ ]*)(?:\\*\\/)*\\n?`, 'g');
return through.obj(function (file, encoding, callback) {
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams not supported!'));
callback();
} else if (file.isBuffer()) {
const contents = file.contents.toString();
let contentsProcessed = file.contents.toString();
let match;
let processed = false;
while (match = reg.exec(contents)) {
const uncommentCode = match[1].replace(/\/\/|\\\*|\*\//g, '');
contentsProcessed = contentsProcessed.replace(match[0], uncommentCode);
processed = true;
}
if (processed) {
file.contents = Buffer.from(contentsProcessed);
}
this.push(file);
callback();
}
});
}