diff --git a/js/library.go b/js/library.go index 58dd3ef..b6f86b4 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 0000000..14c8940 --- /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) + } +}