-
Notifications
You must be signed in to change notification settings - Fork 2
/
unwebpack.js
executable file
·59 lines (50 loc) · 1.83 KB
/
unwebpack.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
#!/usr/bin/env node
/*
* This Source Code is subject to the terms of the Mozilla Public License
* version 2.0 (the "License"). You can obtain a copy of the License at
* http://mozilla.org/MPL/2.0/.
*/
"use strict";
import path from "path";
import {program} from "commander";
import {readScript, saveScript} from "./lib/io.js";
import generateVariableNames from "./lib/generateVariableNames.js";
import rewriteCode from "./lib/rewriteCode.js";
import deduceVariableNames from "./lib/deduceVariableNames.js";
import {parseModules} from "./lib/bundles.js";
program.arguments("<script> <target-dir>");
program.action((script, targetDir) =>
{
program.script = script;
program.targetDir = targetDir;
});
program.option("-n, --no-mods", "Disable all modifications, reformat only");
program.option("-c, --no-code", "Disable code rewriting");
program.option("-v, --no-vars", "Disable variable name modification");
program.parse(process.argv);
if (typeof program.script == "undefined" || typeof program.targetDir == "undefined")
{
program.outputHelp();
process.exit(1);
}
let options = program.opts();
let modules = parseModules(readScript(program.script));
for (let {name, node, scope} of modules)
{
name = name.replace(/\/$/, "/index.js");
name = name.replace(/^\/+/, "");
name = path.join(program.targetDir, ...name.split("/"));
if (!path.basename(name).includes("."))
name += ".js";
if (path.relative(program.targetDir, name).startsWith(".." + path.sep))
throw new Error(`Unexpected module output path outside of target directory: ${name}`);
if (node.type == "BlockStatement")
node.type = "Program";
if (options.mods && options.vars)
generateVariableNames(node, scope);
if (options.mods && options.code)
rewriteCode(node);
if (options.mods && options.vars)
deduceVariableNames(node);
saveScript(node, name);
}