-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
127 lines (112 loc) · 4.65 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
/**
* webpack plugin for vConsole
*
* @author diamont1001<diamont1001@163.com>
* @sea http://webpack.github.io/docs/plugins.html
*/
'use strict';
const path = require('path');
const fs = require('fs');
function vConsolePlugin(options) {
this.options = Object.assign({
filter: [],
enable: false // 插件开关,默认“关”
}, options);
if (typeof this.options.filter === 'string') {
this.options.filter = [this.options.filter];
}
}
vConsolePlugin.prototype.apply = function(compiler) {
const enable = this.options.enable;
let pathVconsole = 'vconsole-webpack-plugin/src/vconsole.js';
const _root = module.parent.paths.find(item => { // eslint-disable-line no-unused-vars
let tmpPathVconsole = path.join(item, 'vconsole-webpack-plugin/src/vconsole.js');
if (fs.existsSync(item) && fs.existsSync(tmpPathVconsole)) {
pathVconsole = tmpPathVconsole;
return true;
}
return false;
});
const that = this;
const pluginFunction = (local, entry) => {
if (enable) {
if (typeof entry === 'string') {
if (!checkFilter([entry], that.options.filter)) {
// TODO: entry 为 string 时,修改不了,只有 object 才可以修改
entry = [pathVconsole, entry];
console.warn('[vconsole-webpack-plugin] 暂不支持 entry 为 string 类型的情况\n');
}
} else if (Object.prototype.toString.call(entry) === '[object Array]') {
if (!checkFilter(entry, that.options.filter)) {
entry.unshift(pathVconsole);
}
} else if (typeof entry === 'object') {
for (let key in entry) {
if (that.options.filter.indexOf(key) < 0) {
if (Object.prototype.toString.call(entry[key]) === '[object Array]') {
if (!checkFilter(entry[key], that.options.filter)) { // 记住这句不能提升到上层 if,因为有其他类型情况,导致检测文件夹时出错
entry[key].unshift(pathVconsole);
}
} else if (typeof entry[key] === 'string') {
if (!checkFilter([entry[key]], that.options.filter)) {
entry[key] = [pathVconsole, entry[key]];
}
} else if (Object.prototype.toString.call(entry[key]) === '[object Object]') {
if (!checkFilter([entry[key]], that.options.filter)) {
// 兼容webpack 5 增加import参数
if (entry[key].import && Object.prototype.toString.call(entry[key].import) === '[object Array]') {
entry[key].import.unshift(pathVconsole);
}
}
}
}
}
}
// console.log(entry);
}
};
if (compiler.hooks) {
// console.log('it is webpack 4');
compiler.hooks.entryOption.tap({ name: 'vConsolePlugin' }, pluginFunction);
} else {
// console.log('it is not webpack 4');
compiler.plugin('entry-option', pluginFunction);
}
};
function checkFilter(entries, filter) {
for (var i = 0; i < entries.length; i++) {
// 去重,避免两次初始化 vconsole
if (!fs.existsSync(entries[i])) { // 处理 webpack-dev-server 开启的情况
continue;
}
if (fs.statSync(entries[i]).isDirectory()) {
return checkFilter(fs.readdirSync(entries[i]), filter);
}
let data = '';
try {
data = codeClean((fs.readFileSync(entries[i]) || '').toString());
} catch (e) {}
if (data.toLowerCase().indexOf('new vconsole(') >= 0
|| data.indexOf('new require(\'vconsole') >= 0
|| data.indexOf('new require("vconsole') >= 0
) {
return true;
}
// 过滤黑名单
for (var j = 0; j < filter.length; j++) {
if (filter[j] === entries[i]) {
return true;
}
}
}
return false;
}
// 去除注释
function codeClean(str) {
var reg = /("([^\\\"]*(\\.)?)*")|('([^\\\']*(\\.)?)*')|(\/{2,}.*?(\r|\n))|(\/\*(\n|.)*?\*\/)/g;
// console.log(str.match(reg));
return str.replace(reg, function(word) { // 去除注释后的文本
return /^\/{2,}/.test(word) || /^\/\*/.test(word) ? '' : word;
});
}
module.exports = vConsolePlugin;