-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·162 lines (160 loc) · 5.65 KB
/
app.js
File metadata and controls
executable file
·162 lines (160 loc) · 5.65 KB
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const esprima = require('esprima');
const escodegen = require('escodegen');
let jsFileList = [];
let readFold = (foldPath) => {
let files = fs.readdirSync(foldPath);
files.forEach((fileName) => {
let pathName = path.join(foldPath, fileName);
let stats = fs.statSync(pathName);
if (stats.isDirectory()) {
readFold(pathName);
}
if (stats.isFile() && fileName.match(/^.+\.js$/)) {
jsFileList.push(pathName);
}
});
};
let mapNode = (node) => {
if (typeof node !== "object" || !node) return;
let keys = Object.keys(node);
keys.map((key) => {
if (typeof node[key] === "object") {
mapNode(node[key]);
}
});
if (node.type === "Program" || node.type === "BlockStatement") {
parseBody(node.body, node);
}
};
let parseBody = (body, parent) => {
if (parent.bodyParsed) return;
let bodyInserted;
let removeComma = (node, key, body) => {
if (node.type === "ExpressionStatement") {
if (node.expression.type === "SequenceExpression") {
while (node.expression.expressions.length > 1) {
let new_node = {
'type': 'ExpressionStatement',
'expression': node.expression.expressions.pop()
};
body.splice(key + 1, 0, new_node);
bodyInserted++;
}
node.expression = node.expression.expressions[0];
}
}
};
let isExpression = (node) => {
return node.type.match(/^.+Expression$/) || ['Literal', 'Identifier'].indexOf(node.type) > -1
};
let deepLogicalReplace = (node) => {
if (node.type === "LogicalExpression") {
// deepLogicalReplace(node.left);
deepLogicalReplace(node.right);
// if (['Literal', 'Identifier', 'BinaryExpression'].indexOf(node.right.type) > -1) {
// // 无意义
// return;
// }
if (node.operator === "&&") {
node.test = node.left;
}
if (node.operator === "||") {
node.test = {
type: "UnaryExpression",
operator: "!",
argument: node.left,
prefix: true
};
}
if (['&&', '||'].indexOf(node.operator) > -1) {
if (isExpression(node.right)) {
node.right = {
type: "ExpressionStatement",
expression: node.right
};
}
node.consequent = {
type: "BlockStatement",
body: [node.right]
};
parseBody(node.consequent.body, node.consequent);
node.alternate = null;
node.type = "IfStatement";
delete node.operator;
delete node.left;
delete node.right;
}
}
if (node.type === "ConditionalExpression") {
deepLogicalReplace(node.consequent);
deepLogicalReplace(node.alternate);
if (isExpression(node.consequent)) {
node.consequent = {
type: "ExpressionStatement",
expression: node.consequent
};
}
if (isExpression(node.alternate)) {
node.alternate = {
type: "ExpressionStatement",
expression: node.alternate
};
}
node.consequent = {
type: "BlockStatement",
body: [node.consequent]
};
parseBody(node.consequent.body, node.consequent);
node.alternate = {
type: "BlockStatement",
body: [node.alternate]
};
parseBody(node.alternate.body, node.alternate);
node.type = "IfStatement";
}
};
let removeLogicalExp = (node, key, body) => {
if (node.type === "ExpressionStatement") {
if (node.expression.type === "LogicalExpression" || node.expression.type === "ConditionalExpression") {
deepLogicalReplace(node.expression);
body[key] = node.expression;
}
}
};
//移除逗号
bodyInserted = 0;
body.map((node, key) => {
removeComma(node, key + bodyInserted, body);
});
//移除三元、与或
body.map((node, key, body) => {
removeLogicalExp(node, key, body);
});
parent.bodyParsed = true;
};
let workDir = process.cwd();
readFold(workDir);
console.log('\033[43;30m INFO \033[40;33m Totally found ' + jsFileList.length + ' js files \033[0m');
console.log('\033[43;30m INFO \033[40;33m Dewebpack will start in 3 seconds... \033[0m');
setTimeout(()=>{
let succeeded = 0;
let failed = 0;
jsFileList.forEach((file) => {
try {
let jsData = fs.readFileSync(file, 'utf-8');
let syntax = esprima.parse(jsData);
mapNode(syntax);
let dewebpackedJs = unescape(escodegen.generate(syntax).replace(/\\/g, "%"));
esprima.parse(dewebpackedJs);
fs.writeFileSync(file, dewebpackedJs);
succeeded++;
} catch (e) {
failed++;
console.log('\033[41;30m FAIL \033[40;31m On dewebpacking [' + file.replace(path.join(workDir, '/'), '') + '] \033[0m');
}
});
console.log('\033[42;30m DONE \033[40;32m Dewebpack finished \033[0m');
},3000);