-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
325 lines (314 loc) · 10.4 KB
/
mod.ts
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
import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.4/command/mod.ts";
import { load as loadSwc, transform as transformSwc } from "~/minifier/swc.ts";
import {
load as loadTerser,
transform as transformTerser,
} from "~/minifier/terser.ts";
import {
load as loadBabel,
transform as transformBabel,
} from "~/minifier/babel.ts";
import type { Config } from "https://esm.sh/v135/@swc/types@0.1.6";
import { minifyCheck } from "~/minifyCheck/minifyCheck.ts";
import { findTriggeredOptionsCommand } from "~/minifyCheck/findTriggeredOption.ts";
interface MinifierOptions {
version: string; // semver
file?: boolean;
diff: boolean;
}
// valid target values for swc
// todo: refactor to get dynamically
const validJscTargetSwc: String[] = [
"es3",
"es5",
"es2015",
"es2016",
"es2017",
"es2018",
"es2019",
"es2020",
"es2021",
"es2022",
"esnext",
];
await new Command()
.name("minify-runner")
.option("-v, --version <name@semver:string>", "minifier name with semver", {
default: "swc@1.4.6",
})
.option("-f, --file", "Code is given by filename instead of string")
.option("-d, --diff", "Show difference between original and minified code", {
default: false,
})
.option(
// note that target is temporarily implemented only for swc
"-t, --target <target:string>",
"target ES version",
{
default: null, // if null, it follows the default .swcrc files
},
)
.option("-n, --notcompress", "Do not compress the code", {
// note that notcompress is temporarily implemented only for swc
default: false, // false is compress
})
.arguments("<codeOrFilePath:string>")
.action(
async (
options: Record<string, any>, // to be refactored
codeOrFilePath: string,
) => {
const { version, file, diff } = options as MinifierOptions; // to be refactored
const [name, semver] = version.split("@");
const code = file
? await Deno.readTextFile(codeOrFilePath)
: codeOrFilePath;
switch (name) {
case "swc": {
const swc = await loadSwc(semver);
const config: Config = JSON.parse(
await Deno.readTextFile(new URL(".swcrc", import.meta.url)),
) as Config;
if (options.target) {
const target = options.target.toLowerCase().trim();
if (validJscTargetSwc.includes(target)) {
config.jsc ??= {};
config.jsc.target = target;
} else {
throw new Error(
`Invalid target value. Valid values are ${
validJscTargetSwc.join(
", ",
)
}`,
);
}
}
if (options.notcompress) {
config.jsc ??= {};
config.jsc.minify = {};
}
const { code: output } = transformSwc({
code,
config,
filename: "tmp.js",
swc,
});
console.log(output.trim());
if (diff) {
const config = JSON.parse(
await Deno.readTextFile(new URL(".acornrc", import.meta.url)),
);
console.log("======================================");
console.log(await minifyCheck(code, output, config));
}
break;
}
case "terser": {
const terser = await loadTerser(semver);
const config = JSON.parse(
await Deno.readTextFile(new URL(".terserrc", import.meta.url)),
);
const output = await transformTerser({
code,
config,
terser,
});
console.log(output.trim());
if (diff) {
const config = JSON.parse(
await Deno.readTextFile(new URL(".acornrc", import.meta.url)),
);
console.log("======================================");
console.log(await minifyCheck(code, output, config));
}
break;
}
case "babel": {
const babel = await loadBabel(semver);
const config = JSON.parse(
await Deno.readTextFile(new URL(".babelrc", import.meta.url)),
);
const output = await transformBabel({
code,
config,
babel,
});
console.log(output.trim());
if (diff) {
const config = JSON.parse(
await Deno.readTextFile(new URL(".acornrc", import.meta.url)),
);
console.log("======================================");
console.log(await minifyCheck(code, output, config));
}
break;
}
default:
throw "invalid minifier name";
}
},
)
.command("find-triggered-options", findTriggeredOptionsCommand)
.command(
"test-es2015",
new Command()
.description("Test ES2015+ features by transpiling them to ES2015")
.action(async () => {
const dirPath = "./test-es2015";
let successcase = 0;
let failcase = 0;
for await (const entry of Deno.readDir(dirPath)) {
if (entry.isFile && entry.name.endsWith(".js")) {
const filePath = `${dirPath}/${entry.name}`;
const originalCode = await Deno.readTextFile(filePath);
const babel = await loadBabel("7.19.1");
const config = JSON.parse(
await Deno.readTextFile(new URL(".babelrc", import.meta.url)),
);
const transpiledCode = await transformBabel({
code: originalCode,
config,
babel,
});
if (
originalCode.replace(/\s+/g, "") !==
transpiledCode.replace(/\s+/g, "")
) {
console.log(`Differences found in ${entry.name}:`);
console.log("Original Code:");
console.log(originalCode);
console.log("Transpiled Code:");
console.log(transpiledCode);
failcase++;
} else {
console.log(`No differences found in ${entry.name}.`);
successcase++;
}
}
}
// result phase
console.log("Test finished.");
console.log("Success cases:", successcase);
console.log("Fail cases:", failcase);
}),
)
.command(
"get-transpilable-rate",
new Command()
.description("Get transpilable rate of ES2015+ features to ES2015")
.option(
"-v, --version <name@semver:string>",
"minifier name with semver",
{
default: "babel@7.19.1",
},
)
.arguments("<dirPath:string>")
.action(async (
{ version }: { version: string },
dirPath: string,
) => {
const [name, semver] = version.split("@");
let successcase = 0;
let failcase = 0;
const checkTranspilable = async (
originalCode: string,
name: string,
) => {
const configAcorn = JSON.parse(
await Deno.readTextFile(new URL(".acornrc", import.meta.url)),
);
switch (name) {
case "swc":
const swc = await loadSwc(semver);
let configSwc: Config = JSON.parse(
await Deno.readTextFile(new URL(".swcrc", import.meta.url)),
) as Config;
// set default target to es2015 and don't compress
configSwc.jsc ??= {};
configSwc.jsc.target = "es2015";
configSwc.jsc ??= {};
configSwc.jsc.minify = {};
try {
const { code: outputSwc } = transformSwc({
code: originalCode,
config: configSwc,
filename: "tmp.js",
swc,
});
if (await minifyCheck(originalCode, outputSwc, configAcorn)) {
return true;
} else {
return false;
}
} catch {
return false;
}
case "terser":
const terser = await loadTerser(semver);
const configTerser = JSON.parse(
await Deno.readTextFile(new URL(".terserrc", import.meta.url)),
);
try {
const outputTerser = await transformTerser({
code: originalCode,
config: configTerser,
terser,
});
if (
await minifyCheck(originalCode, outputTerser, configAcorn)
) {
return true;
} else {
return false;
}
} catch {
return false;
}
case "babel":
const babel = await loadBabel(semver);
const configBabel = JSON.parse(
await Deno.readTextFile(new URL(".babelrc", import.meta.url)),
);
try {
const outputBabel = await transformBabel({
code: originalCode,
config: configBabel,
babel,
});
if (await minifyCheck(originalCode, outputBabel, configAcorn)) {
return true;
} else {
return false;
}
} catch {
return false;
}
default:
throw "invalid minifier name";
}
};
for await (const entry of Deno.readDir(dirPath)) {
if (entry.isFile && entry.name.endsWith(".js")) {
const filePath = `${dirPath}/${entry.name}`;
const originalCode = await Deno.readTextFile(filePath);
if (await checkTranspilable(originalCode, name)) {
successcase++;
} else {
failcase++;
}
}
if ((successcase + failcase) % 2000 == 0) {
console.log(`current: ${successcase + failcase}`);
console.log(`success: ${successcase}`);
console.log(`rate: ${successcase / (successcase + failcase)}`);
}
}
console.log(`finished`);
console.log(`current: ${successcase + failcase}`);
console.log(`success: ${successcase}`);
console.log(`rate: ${successcase / (successcase + failcase)}`);
}),
)
.parse(Deno.args);