From 8de52b4cf209b405ee554ff0eed3ca43d2de8955 Mon Sep 17 00:00:00 2001 From: Christian Stewart Date: Sun, 15 Nov 2026 05:48:39 -0800 Subject: [PATCH] js: embed the whole library source in Sources Sources held only the three modules that import nothing, so a Go module that vendors Modlock kept the HUD tokens, the hero table and the text helpers but not the library itself. Embedding all of src gives a vendoring module the TypeScript library at the version it pins: its type checker can resolve modlock to the vendored src/index.ts, with no npm package or project setup step. The protobuf messages the source imports are already vendored with their own packages. A test checks that Sources holds the entry module and every module its source imports from inside src. The vendored tree grows by 2,576,353 bytes in 34 files, most of it entities.ts. Nothing in Modlock reads Sources, so no binary grows. Signed-off-by: Christian Stewart --- js/library.go | 11 ++++++----- js/sources_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 js/sources_test.go diff --git a/js/library.go b/js/library.go index 58dd3ef5..b6f86b46 100644 --- a/js/library.go +++ b/js/library.go @@ -47,12 +47,13 @@ const Built = "built" //go:embed all:built var built embed.FS -// Sources holds the library modules that import nothing, so a Go module that -// vendors Modlock keeps them and its browser pages can import them beside its -// own TypeScript: the HUD kit's tokens, the hero table and the HUD text -// helpers, which a page drawn in the game's look shares with its mods. +// Sources holds the library's whole TypeScript source, so a Go module that +// vendors Modlock keeps the library at the version it pins: its type checker +// can resolve modlock to src/index.ts, and its browser pages can import a +// module such as the HUD kit's tokens beside their own TypeScript. The +// protobuf messages the source imports are vendored with their own packages. // -//go:embed src/theme.ts src/heroes.ts src/format.ts +//go:embed src var Sources embed.FS // Renderer is the built renderer's file name, which a client content package diff --git a/js/sources_test.go b/js/sources_test.go new file mode 100644 index 00000000..14c89407 --- /dev/null +++ b/js/sources_test.go @@ -0,0 +1,44 @@ +package js + +import ( + "io/fs" + "path" + "regexp" + "strings" + "testing" +) + +// relativeImport matches a TypeScript import or export from a relative path. +var relativeImport = regexp.MustCompile(`from '(\.{1,2}/[^']+)\.js'`) + +// TestSourcesCloseOverImports checks that Sources holds the library's entry +// module and every module its source imports from inside src, so a vendored +// copy type checks. Imports that leave src, such as the protobuf messages, +// are vendored with their own packages. +func TestSourcesCloseOverImports(t *testing.T) { + if _, err := fs.Stat(Sources, "src/index.ts"); err != nil { + t.Fatal("Sources does not embed the entry module", err) + } + err := fs.WalkDir(Sources, ".", func(name string, entry fs.DirEntry, err error) error { + if err != nil || entry.IsDir() || !strings.HasSuffix(name, ".ts") { + return err + } + source, err := fs.ReadFile(Sources, name) + if err != nil { + return err + } + for _, match := range relativeImport.FindAllStringSubmatch(string(source), -1) { + target := path.Join(path.Dir(name), match[1]) + ".ts" + if !strings.HasPrefix(target, "src/") { + continue + } + if _, err := fs.Stat(Sources, target); err != nil { + t.Errorf("%s imports %s, which Sources does not embed", name, target) + } + } + return nil + }) + if err != nil { + t.Fatal(err) + } +}