Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions js/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions js/sources_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading