storm-lua-minify 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js ADDED
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const commander_1 = require("commander");
10
+ const luaparse_1 = __importDefault(require("luaparse"));
11
+ const ast2lua_1 = require("./ast2lua");
12
+ const program = new commander_1.Command();
13
+ program.version("0.1.0").description("A Lua minifier also outputs source map");
14
+ program.parse(process.argv);
15
+ const luaFiles = program.args;
16
+ const luaparseSetting = {
17
+ locations: true,
18
+ luaVersion: "5.3",
19
+ ranges: true,
20
+ scope: true,
21
+ };
22
+ luaFiles.forEach((fileName) => {
23
+ const includes = new Map();
24
+ const parsedFileName = path_1.default.parse(fileName);
25
+ function requireHelper(recursiveFilePath) {
26
+ const resolvePath = path_1.default.relative(parsedFileName.dir, path_1.default.join(parsedFileName.dir, recursiveFilePath.replaceAll(".", path_1.default.sep) + ".lua"));
27
+ const fullResolvePath = path_1.default.join(parsedFileName.dir, resolvePath);
28
+ if (!includes.has(resolvePath) && fs_1.default.existsSync(fullResolvePath)) {
29
+ const code = fs_1.default.readFileSync(fullResolvePath).toString();
30
+ const ast = luaparse_1.default.parse(code, luaparseSetting);
31
+ if ("globals" in ast) {
32
+ includes.set(resolvePath, code);
33
+ return new ast2lua_1.Minifier(resolvePath, ast, requireHelper).parse();
34
+ }
35
+ includes.set(resolvePath, "");
36
+ return undefined;
37
+ }
38
+ }
39
+ if (fs_1.default.existsSync(fileName)) {
40
+ const code = fs_1.default.readFileSync(fileName).toString();
41
+ const ast = luaparse_1.default.parse(code, luaparseSetting);
42
+ if ("globals" in ast) {
43
+ includes.set(parsedFileName.base, code);
44
+ const map = new ast2lua_1.Minifier(parsedFileName.base, ast, requireHelper).parse();
45
+ includes.forEach((v, k) => {
46
+ console.log(k);
47
+ map.setSourceContent(k, v);
48
+ });
49
+ const minFileName = path_1.default.format({
50
+ dir: parsedFileName.dir,
51
+ name: parsedFileName.name + ".min",
52
+ ext: ".lua",
53
+ });
54
+ const mapFileName = path_1.default.format({
55
+ dir: parsedFileName.dir,
56
+ name: parsedFileName.name,
57
+ ext: parsedFileName.ext + ".map",
58
+ });
59
+ map.add("\n--[[\n//# sourceMappingURL=" + mapFileName + "\n]]");
60
+ const sourceAndMap = map.toStringWithSourceMap();
61
+ fs_1.default.writeFileSync(minFileName, sourceAndMap.code);
62
+ fs_1.default.writeFileSync(mapFileName, JSON.stringify(sourceAndMap.map));
63
+ }
64
+ }
65
+ else {
66
+ console.error("No such file: " + fileName);
67
+ }
68
+ });
package/dist/index.js ADDED
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const fs_1 = __importDefault(require("fs"));
7
+ const path_1 = __importDefault(require("path"));
8
+ const luaparse_1 = __importDefault(require("luaparse"));
9
+ const node_process_1 = require("node:process");
10
+ const ast2lua_1 = require("./ast2lua");
11
+ const luaparseSetting = {
12
+ locations: true,
13
+ luaVersion: "5.3",
14
+ ranges: true,
15
+ scope: true,
16
+ };
17
+ const argPath = node_process_1.argv[2];
18
+ const includes = new Set();
19
+ function requireHelper(fileName) {
20
+ const resolvePath = path_1.default.join(path_1.default.dirname(argPath), fileName.replaceAll(".", path_1.default.sep) + ".lua");
21
+ if (!includes.has(resolvePath) && fs_1.default.existsSync(resolvePath)) {
22
+ const code = fs_1.default.readFileSync(resolvePath).toString();
23
+ const ast = luaparse_1.default.parse(code, luaparseSetting);
24
+ if ("globals" in ast) {
25
+ return new ast2lua_1.Minifier(resolvePath, ast, requireHelper).parse();
26
+ }
27
+ }
28
+ else {
29
+ return undefined;
30
+ }
31
+ includes.add(resolvePath);
32
+ }
33
+ if (fs_1.default.existsSync(argPath)) {
34
+ const code = fs_1.default.readFileSync(argPath).toString();
35
+ const ast = luaparse_1.default.parse(code, luaparseSetting);
36
+ if ("globals" in ast) {
37
+ const map = new ast2lua_1.Minifier(argPath, ast, requireHelper).parse();
38
+ map.add("\n--[[\n//# sourceMappingURL=" + argPath + ".map\n]]");
39
+ console.log(map.toStringWithSourceMap().code);
40
+ // toStringWithSourceMap().map の file に書き出したのちのファイル名を入れないとVSCode Extでは検索失敗する
41
+ //console.log(JSON.stringify(map.toStringWithSourceMap().map));
42
+ }
43
+ }
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "storm-lua-minify",
3
+ "description": "A Lua minifier also outputs source map",
4
+ "version": "0.1.0",
5
+ "dependencies": {
6
+ "commander": "^12.1.0",
7
+ "luamin": "^1.0.4",
8
+ "luaparse": "^0.3.1",
9
+ "source-map": "^0.7.4"
10
+ },
11
+ "devDependencies": {
12
+ "@types/node": "^20.12.8",
13
+ "@types/luaparse": "^0.2.12",
14
+ "@typescript-eslint/eslint-plugin": "^7.8.0",
15
+ "@typescript-eslint/parser": "^7.8.0",
16
+ "eslint": "^8.57.0",
17
+ "ts-node": "^10.9.2",
18
+ "typescript": "^5.4.5"
19
+ },
20
+ "bin": "dist/cli.js",
21
+ "scripts": {
22
+ "start": "node ./dist/index.js",
23
+ "build": "npx tsc"
24
+ },
25
+ "author": "Nona Takahara <nona_t_2612@yahoo.co.jp>",
26
+ "license": "MIT"
27
+ }