diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..74d24bd --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule ".lune/terracotta"] + path = .lune/terracotta + url = https://github.com/0x5eal/terracotta.git diff --git a/.lune/bundle.luau b/.lune/bundle.luau new file mode 100644 index 0000000..e4fbf78 --- /dev/null +++ b/.lune/bundle.luau @@ -0,0 +1,66 @@ +local fs = require("@lune/fs") +local stdio = require("@lune/stdio") +local task = require("@lune/task") + +local terracotta = require("terracotta") + +local function map(tbl: { [k]: v }, callback: (k: k, v: v) -> v) + for k, v in tbl do + local ok, val = pcall(function() + return callback(k, v) + end) + + if not ok then + tbl[k] = nil + else + tbl[k] = val + end + end + + return tbl +end + +print("\n") + +local dirChildren = fs.readDir("out/rbxts") +local ENTYRPOINTS = map(dirChildren, function(k, path) + if path ~= "rbxts" and path:find("%.lua$") then + print(`{k}/{#dirChildren} include`, path) + return "out/rbxts/" .. path + end + + print(`{k}/{#dirChildren} ignore`, path) + error("not a lua file") +end) + +function main() + local bundleStart = os.clock() + + local out = terracotta.Build({ + entryPoints = ENTYRPOINTS, + bundle = true, + rules = { + "remove_spaces", + "remove_unused_while", + "remove_unused_if_branch", + "remove_empty_do", + }, + }) + + print(`\nbundled {#ENTYRPOINTS} files in {os.clock() - bundleStart}s`) + + local fsStart = os.clock() + for pos, path in ENTYRPOINTS do + local bundle = out[path] + + path = "out/" .. path:split("/")[3] + stdio.write(`\x1B[2K\r{pos}/{#ENTYRPOINTS} write {path}`) + task.wait(0.1) + + fs.writeFile(path, bundle) + end + + stdio.write(`\x1B[2K\rwrote {#ENTYRPOINTS} files in {os.clock() - fsStart}s`) +end + +return main() diff --git a/.lune/terracotta b/.lune/terracotta new file mode 160000 index 0000000..c27e384 --- /dev/null +++ b/.lune/terracotta @@ -0,0 +1 @@ +Subproject commit c27e3849e3180b8b15a1116a08cef0e73faeeb8d diff --git a/package.json b/package.json index 4215567..fa54fce 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "A lua implementation of the wireguard keygen algorithm.", "main": "out/init.lua", "scripts": { - "build": "rbxtsc --verbose", + "build": "rbxtsc --verbose && ./lunew bundle", "watch": "rbxtsc -w", "lint": "eslint src", "check_fmt": "prettier -c src/", diff --git a/src/base64.ts b/src/base64.ts index bff6e48..8c976c1 100644 --- a/src/base64.ts +++ b/src/base64.ts @@ -1,6 +1,6 @@ const { slice } = require<{ slice: (arr: T[], start: number, stop?: number) => T[]; -}>("util"); +}>("./util.lua"); function stringToBytes(str: string) { const result = []; diff --git a/src/index.ts b/src/index.ts index 1b57ca8..158edc0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,8 @@ const { generatePrivateKey, generatePublicKey } = require<{ generatePrivateKey: () => number[]; generatePublicKey: (privateKey: number[]) => number[]; -}>("wg"); -const { atob } = require<{ atob: (buf: number[]) => string }>("base64"); +}>("./wg.lua"); +const { atob } = require<{ atob: (buf: number[]) => string }>("./base64.lua"); export interface Keypair { publicKey: string; diff --git a/tsconfig.json b/tsconfig.json index fbca70e..2c5b30d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,7 +19,7 @@ // configurable "rootDir": "src", - "outDir": "out", + "outDir": "out/rbxts", "incremental": true, "tsBuildInfoFile": "out/tsconfig.tsbuildinfo", "declaration": true