@data-vegle/rbxts-go 1.0.1 → 1.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/README.md +7 -0
- package/cli/cli.go +6 -0
- package/compile/compile.go +21 -3
- package/compile/obfuscate.go +99 -0
- package/package.json +44 -44
- package/plugins/fallback.go +1 -1
- package/project/shared/constants.go +2 -0
package/README.md
CHANGED
|
@@ -144,6 +144,13 @@ byte-identical to rbxtsgo's, and on rbxtsgo's `upstream-tests` corpus the printe
|
|
|
144
144
|
|
|
145
145
|
Also new: `--memprofile <file>` writes a heap profile, next to the existing `--cpuprofile`.
|
|
146
146
|
|
|
147
|
+
Also new: `--obfuscate` runs [darklua](https://darklua.com) on the compiled files, and needs `darklua` on the PATH.
|
|
148
|
+
The compiled Luau goes to a temporary folder, and one darklua run writes the obfuscated files to the output folder, so
|
|
149
|
+
each output file is written once. Without a `.darklua.json` or `.darklua.json5` in the project folder, it uses the
|
|
150
|
+
rule list in `compile/obfuscate.go` (remove comments, spaces, types and unused code, rename locals, and more); with
|
|
151
|
+
one, that config decides. Only the files the compiler emits
|
|
152
|
+
are obfuscated: not the runtime library, and not copied `.lua` files.
|
|
153
|
+
|
|
147
154
|
Also new: **native plugins**. A transformer plugin written in Go, as its own module, can be compiled into the compiler
|
|
148
155
|
and then runs in place of the JavaScript transformer of the same name in `compilerOptions.plugins`, so the build no
|
|
149
156
|
longer goes to the JS compiler. [docs/plugin-api.md](docs/plugin-api.md) is the contract for plugin authors, and
|
package/cli/cli.go
CHANGED
|
@@ -81,6 +81,8 @@ func Run(args []string) int {
|
|
|
81
81
|
memProfile := flags.String("memprofile", "", "write a heap profile to this file when the build ends")
|
|
82
82
|
var fixEmit tristate
|
|
83
83
|
flags.Var(&fixEmit, "fix-emit", "turn on fixes for output that is wrong in roblox-ts 3.0.0, which makes the output differ from it where it was wrong. Also tsconfig \"rbxtsgo\": { \"fixEmit\": true }")
|
|
84
|
+
var obfuscate tristate
|
|
85
|
+
flags.Var(&obfuscate, "obfuscate", "obfuscate the compiled files with darklua, which must be on the PATH. Uses .darklua.json or .darklua.json5 of the project when there is one")
|
|
84
86
|
var setget tristate
|
|
85
87
|
flags.Var(&setget, "setget", "turn getters and setters into methods, as rbxts-transformer-setget does")
|
|
86
88
|
setgetPrefix := flags.String("setgetPrefix", "", "prefix of the methods setget makes (default __)")
|
|
@@ -138,6 +140,7 @@ func Run(args []string) int {
|
|
|
138
140
|
setBool(allowCommentDirectives, &options.AllowCommentDirectives)
|
|
139
141
|
setBool(luauFlag, &options.Luau)
|
|
140
142
|
setBool(fixEmit, &options.FixEmit)
|
|
143
|
+
setBool(obfuscate, &options.Obfuscate)
|
|
141
144
|
if *projectType != "" {
|
|
142
145
|
options.Type = shared.ProjectType(*projectType)
|
|
143
146
|
}
|
|
@@ -289,6 +292,9 @@ func build(projectPath string, applyFlags func(*shared.ProjectOptions), checkers
|
|
|
289
292
|
if options.FixEmit {
|
|
290
293
|
return errors.New(decision.Reason + ", and the JS compiler doesn't have --fix-emit.\nTo build this project, turn fixEmit off, or remove the plugin from compilerOptions.plugins.")
|
|
291
294
|
}
|
|
295
|
+
if options.Obfuscate {
|
|
296
|
+
return errors.New(decision.Reason + ", and the JS compiler doesn't have --obfuscate.")
|
|
297
|
+
}
|
|
292
298
|
code, err := plugins.Delegate(decision.Reason, tsConfigPath, commandLine, os.Stderr)
|
|
293
299
|
exitCode = code
|
|
294
300
|
return err
|
package/compile/compile.go
CHANGED
|
@@ -332,18 +332,31 @@ func CompileFiles(ctx context.Context, c *Context, files []*ast.SourceFile) (*Em
|
|
|
332
332
|
// WriteResults writes each rendered file to its output path, as the emit loop of compileFiles does. The files are
|
|
333
333
|
// independent, so several goroutines write them at once. The returned paths keep the order of the results, and the
|
|
334
334
|
// error returned is the first one in that order, with the paths before it, as a sequential loop would give.
|
|
335
|
+
//
|
|
336
|
+
// With --obfuscate the files go to a temporary folder, and darklua writes them to the output folder. See obfuscate.go.
|
|
335
337
|
func WriteResults(c *Context, results []*FileResult) ([]string, error) {
|
|
338
|
+
var obfuscate *obfuscation
|
|
339
|
+
if c.Data.ProjectOptions.Obfuscate {
|
|
340
|
+
var err error
|
|
341
|
+
if obfuscate, err = newObfuscation(); err != nil {
|
|
342
|
+
return nil, err
|
|
343
|
+
}
|
|
344
|
+
defer obfuscate.close()
|
|
345
|
+
}
|
|
336
346
|
writeOne := func(result *FileResult) (string, error) {
|
|
337
347
|
outPath := c.Translator.GetOutputPath(nodepath.Native.Normalize(result.File.FileName().AsString()))
|
|
338
|
-
|
|
348
|
+
target := outPath
|
|
349
|
+
if obfuscate != nil {
|
|
350
|
+
target = obfuscate.path(c, outPath)
|
|
351
|
+
} else if c.Data.ProjectOptions.WriteOnlyChanged {
|
|
339
352
|
if current, err := os.ReadFile(outPath); err == nil && string(current) == result.Source {
|
|
340
353
|
return "", nil
|
|
341
354
|
}
|
|
342
355
|
}
|
|
343
|
-
if err := os.MkdirAll(nodepath.Native.Dirname(
|
|
356
|
+
if err := os.MkdirAll(nodepath.Native.Dirname(target), 0o777); err != nil {
|
|
344
357
|
return "", err
|
|
345
358
|
}
|
|
346
|
-
if err := os.WriteFile(
|
|
359
|
+
if err := os.WriteFile(target, []byte(result.Source), 0o666); err != nil {
|
|
347
360
|
return "", err
|
|
348
361
|
}
|
|
349
362
|
return outPath, nil
|
|
@@ -380,6 +393,11 @@ func WriteResults(c *Context, results []*FileResult) ([]string, error) {
|
|
|
380
393
|
emitted = append(emitted, paths[i])
|
|
381
394
|
}
|
|
382
395
|
}
|
|
396
|
+
if obfuscate != nil && len(emitted) > 0 {
|
|
397
|
+
if err := obfuscate.run(c); err != nil {
|
|
398
|
+
return nil, err
|
|
399
|
+
}
|
|
400
|
+
}
|
|
383
401
|
return emitted, nil
|
|
384
402
|
}
|
|
385
403
|
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
package compile
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"errors"
|
|
5
|
+
"fmt"
|
|
6
|
+
"os"
|
|
7
|
+
"os/exec"
|
|
8
|
+
"strings"
|
|
9
|
+
|
|
10
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/nodepath"
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
// defaultDarkluaConfig is used when the project has no darklua config of its own. The generator keeps the tokens, so
|
|
14
|
+
// remove_spaces has something to do and one leading directive comment (--!native, --!strict, --!optimize) survives.
|
|
15
|
+
// "$roblox" keeps rename_variables from giving a local the name of a Roblox global.
|
|
16
|
+
const defaultDarkluaConfig = `{
|
|
17
|
+
"generator": "retain_lines",
|
|
18
|
+
"rules": [
|
|
19
|
+
"remove_spaces",
|
|
20
|
+
{ "rule": "remove_comments", "except": ["^--!"] },
|
|
21
|
+
"remove_assertions",
|
|
22
|
+
"remove_debug_profiling",
|
|
23
|
+
"compute_expression",
|
|
24
|
+
"remove_unused_if_branch",
|
|
25
|
+
"remove_unused_while",
|
|
26
|
+
"filter_after_early_return",
|
|
27
|
+
"remove_empty_do",
|
|
28
|
+
"remove_unused_variable",
|
|
29
|
+
"remove_method_definition",
|
|
30
|
+
"convert_index_to_field",
|
|
31
|
+
"remove_nil_declaration",
|
|
32
|
+
"remove_interpolated_string",
|
|
33
|
+
"remove_function_call_parens",
|
|
34
|
+
"remove_types",
|
|
35
|
+
"remove_if_expression",
|
|
36
|
+
{ "rule": "rename_variables", "globals": ["$default", "$roblox"], "include_functions": true },
|
|
37
|
+
"group_local_assignment",
|
|
38
|
+
"convert_square_root_call",
|
|
39
|
+
"convert_luau_number"
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
`
|
|
43
|
+
|
|
44
|
+
// darkluaConfigNames are the files darklua itself looks for, in the project folder.
|
|
45
|
+
var darkluaConfigNames = []string{".darklua.json", ".darklua.json5"}
|
|
46
|
+
|
|
47
|
+
// obfuscation is --obfuscate for one WriteResults call. darklua reads files and writes files, so the compiled Luau
|
|
48
|
+
// is written to a temporary folder that mirrors the output folder, and one darklua run writes the obfuscated files
|
|
49
|
+
// to the output folder. Each output file is then written once, already obfuscated.
|
|
50
|
+
type obfuscation struct {
|
|
51
|
+
root string // the temporary folder
|
|
52
|
+
staging string // root/out, the mirror of the output folder
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
func newObfuscation() (*obfuscation, error) {
|
|
56
|
+
root, err := os.MkdirTemp("", "rbxts-go-obfuscate-")
|
|
57
|
+
if err != nil {
|
|
58
|
+
return nil, err
|
|
59
|
+
}
|
|
60
|
+
return &obfuscation{root: root, staging: nodepath.Native.Join(root, "out")}, nil
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// path is where the compiled text of an output file goes before darklua runs.
|
|
64
|
+
func (o *obfuscation) path(c *Context, outPath string) string {
|
|
65
|
+
return nodepath.Native.Join(o.staging, nodepath.Native.Relative(c.Translator.OutDir, outPath))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// run has darklua write every staged file, obfuscated, to the output folder.
|
|
69
|
+
func (o *obfuscation) run(c *Context) error {
|
|
70
|
+
config := ""
|
|
71
|
+
for _, name := range darkluaConfigNames {
|
|
72
|
+
candidate := nodepath.Native.Join(c.Data.ProjectPath, name)
|
|
73
|
+
if _, err := os.Stat(candidate); err == nil {
|
|
74
|
+
config = candidate
|
|
75
|
+
break
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if config == "" {
|
|
79
|
+
config = nodepath.Native.Join(o.root, "darklua.json")
|
|
80
|
+
if err := os.WriteFile(config, []byte(defaultDarkluaConfig), 0o666); err != nil {
|
|
81
|
+
return err
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
command := exec.Command("darklua", "process", o.staging, c.Translator.OutDir, "--config", config)
|
|
85
|
+
// The project folder, so that a tool manager (rokit, aftman) picks the project's darklua.
|
|
86
|
+
command.Dir = c.Data.ProjectPath
|
|
87
|
+
output, err := command.CombinedOutput()
|
|
88
|
+
if errors.Is(err, exec.ErrNotFound) {
|
|
89
|
+
return errors.New("--obfuscate needs darklua, and it was not found on the PATH")
|
|
90
|
+
}
|
|
91
|
+
if err != nil {
|
|
92
|
+
return fmt.Errorf("darklua failed: %w\n%s", err, strings.TrimSpace(string(output)))
|
|
93
|
+
}
|
|
94
|
+
return nil
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
func (o *obfuscation) close() {
|
|
98
|
+
_ = os.RemoveAll(o.root)
|
|
99
|
+
}
|
package/package.json
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
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
|
-
|
|
2
|
+
"name": "@data-vegle/rbxts-go",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "The roblox-ts compiler in Go, on TypeScript 7, with native plugins. One dev dependency brings the compiler launcher, the roblox-ts types and TypeScript 7.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/Vegle-Data-Apps/rbxts-go.git"
|
|
9
|
+
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"rbxts-go": "npm/rbxts-go.sh",
|
|
12
|
+
"rbxtsc": "npm/rbxts-go.sh"
|
|
13
|
+
},
|
|
14
|
+
"types": "npm/index.d.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"npm",
|
|
17
|
+
"scripts/build-with-plugins.sh",
|
|
18
|
+
"scripts/setup-third-party.sh",
|
|
19
|
+
"patches",
|
|
20
|
+
"docs/plugin-api.md",
|
|
21
|
+
"go.mod",
|
|
22
|
+
"go.sum",
|
|
23
|
+
"cli",
|
|
24
|
+
"compile",
|
|
25
|
+
"internal",
|
|
26
|
+
"luau",
|
|
27
|
+
"pluginapi",
|
|
28
|
+
"plugins",
|
|
29
|
+
"project",
|
|
30
|
+
"transformer",
|
|
31
|
+
"tsbridge",
|
|
32
|
+
"watch",
|
|
33
|
+
"!**/*_test.go",
|
|
34
|
+
"!**/testdata",
|
|
35
|
+
"THIRD_PARTY_NOTICES.md",
|
|
36
|
+
"README.rbxtsgo.md"
|
|
37
|
+
],
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@rbxts/compiler-types": "3.0.0-types.0",
|
|
43
|
+
"@rbxts/types": "^1.0.915",
|
|
44
|
+
"typescript": "7.1.0-beta"
|
|
45
|
+
}
|
|
46
46
|
}
|
package/plugins/fallback.go
CHANGED
|
@@ -16,7 +16,7 @@ const jsCompilerPath = "node_modules/roblox-ts/out/CLI/cli.js"
|
|
|
16
16
|
// flagsOfOurs are the flags only rbxtsgo has that take a value, and boolFlagsOfOurs the ones that don't.
|
|
17
17
|
var (
|
|
18
18
|
flagsOfOurs = []string{"checkers", "cpuprofile", "setgetPrefix"}
|
|
19
|
-
boolFlagsOfOurs = []string{"setget", "no-setget", "fix-emit", "no-fix-emit"}
|
|
19
|
+
boolFlagsOfOurs = []string{"setget", "no-setget", "fix-emit", "no-fix-emit", "obfuscate", "no-obfuscate"}
|
|
20
20
|
)
|
|
21
21
|
|
|
22
22
|
// passAlongArgs removes our own flags from the command line, because yargs in the JS compiler rejects unknown ones.
|
|
@@ -63,6 +63,8 @@ type ProjectOptions struct {
|
|
|
63
63
|
// FixEmit is rbxtsgo's own. It turns on the output fixes that differ from JS 3.0.0 for code that compiles there. See
|
|
64
64
|
// docs/known-differences.md. BuildKey hashes these options, so changing it forces a rebuild.
|
|
65
65
|
FixEmit bool
|
|
66
|
+
// Obfuscate is rbxts-go's own: darklua obfuscates the compiled files as they are written. See compile/obfuscate.go.
|
|
67
|
+
Obfuscate bool
|
|
66
68
|
}
|
|
67
69
|
|
|
68
70
|
// DefaultProjectOptions is DEFAULT_PROJECT_OPTIONS.
|