Skip to content

Commit 3c74d21

Browse files
authored
Merge pull request #75 from gopherjs/go1.18
Upgrade Playground to GopherJS 1.18.0-beta1
2 parents 9480d85 + 302f27f commit 3c74d21

File tree

220 files changed

+293
-165
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

220 files changed

+293
-165
lines changed

playground/README.md

Lines changed: 9 additions & 0 deletions

playground/gen.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
//go:generate ./update.sh
1+
//go:generate go run ./internal/cmd/precompile
2+
//go:generate go install github.com/gopherjs/gopherjs
3+
//go:generate gopherjs build -m .
24

35
package main

playground/go.mod

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@ module github.com/gopherjs/gopherjs.github.io/playground
33
go 1.17
44

55
require (
6-
github.com/gopherjs/gopherjs v0.0.0-20211108205335-ed9a9b14a747
6+
github.com/gopherjs/gopherjs v1.18.0-beta1
77
github.com/neelance/go-angularjs v0.0.0-20170205214111-8c6312cca6e2
8-
golang.org/x/tools v0.1.7
8+
github.com/sirupsen/logrus v1.8.1
9+
golang.org/x/tools v0.1.12
910
honnef.co/go/js/dom v0.0.0-20210725211120-f030747120f2
1011
honnef.co/go/js/xhr v0.0.0-20150307031022-00e3346113ae
1112
)
1213

1314
require (
15+
github.com/fsnotify/fsnotify v1.5.1 // indirect
1416
github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86 // indirect
17+
github.com/neelance/sourcemap v0.0.0-20200213170602-2833bce08e4c // indirect
18+
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
19+
github.com/stretchr/testify v1.7.0 // indirect
20+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
21+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
1522
honnef.co/go/js/util v0.0.0-20150216223935-96b8dd9d1621 // indirect
1623
)

playground/go.sum

Lines changed: 40 additions & 13 deletions
Large diffs are not rendered by default.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Program precompile updates pre-built standard library packages for the
2+
// playground.
3+
//
4+
// This script performs the following sequence of steps:
5+
//
6+
// - Enumerate all standard packages that should be available in the playground.
7+
// - Precompile them, including transitive dependencies.
8+
// - Delete all old precompiled archive.
9+
// - Write all new precompiled archive in their place.
10+
//
11+
// This will use the same GopherJS version as specified in the playground gm.mod
12+
// to ensure consistency. The script uses GopherJS compiler API directly, so
13+
// it doesn't require the GopherJS tool to be installed.
14+
package main
15+
16+
import (
17+
"flag"
18+
"fmt"
19+
gobuild "go/build"
20+
"os"
21+
"path/filepath"
22+
"strings"
23+
24+
"github.com/gopherjs/gopherjs/build"
25+
"github.com/gopherjs/gopherjs/compiler"
26+
log "github.com/sirupsen/logrus"
27+
)
28+
29+
type logLevelFlag struct{ log.Level }
30+
31+
func (l *logLevelFlag) Set(raw string) error { return l.UnmarshalText([]byte(raw)) }
32+
33+
var (
34+
logLevel logLevelFlag = logLevelFlag{Level: log.ErrorLevel}
35+
)
36+
37+
func init() {
38+
flag.Var(&logLevel, "log_level", "Default logging level.")
39+
}
40+
41+
func run() error {
42+
s, err := build.NewSession(&build.Options{
43+
Verbose: true,
44+
Minify: true,
45+
NoCache: true,
46+
})
47+
if err != nil {
48+
return fmt.Errorf("failed to create a build session: %w", err)
49+
}
50+
51+
packages, err := s.XContext().Match([]string{"std"})
52+
if err != nil {
53+
return fmt.Errorf("failed to enumerate standard library packages")
54+
}
55+
packages = importable(packages)
56+
packages = append(packages, "github.com/gopherjs/gopherjs/js", "github.com/gopherjs/gopherjs/nosync")
57+
58+
for _, pkg := range packages {
59+
_, err := s.BuildImportPath(pkg)
60+
if err != nil {
61+
return fmt.Errorf("failed to precompile package %q: %w", pkg, err)
62+
}
63+
}
64+
65+
target, err := targetDir(s)
66+
if err := os.RemoveAll(target); err != nil {
67+
return fmt.Errorf("failed to clean out old precompiled archives: %w", err)
68+
}
69+
70+
for _, archive := range s.UpToDateArchives {
71+
if err := writeArchive(target, archive); err != nil {
72+
return fmt.Errorf("failed to write package %q archive: %w", archive.ImportPath, err)
73+
}
74+
}
75+
76+
return nil
77+
}
78+
79+
func writeArchive(target string, archive *compiler.Archive) error {
80+
path := filepath.Join(target, filepath.FromSlash(archive.ImportPath)+".a.js")
81+
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
82+
return fmt.Errorf("failed to create precompiled package directory %q: %w", filepath.Dir(path), err)
83+
}
84+
f, err := os.Create(path)
85+
if err != nil {
86+
return fmt.Errorf("failed to create precompiled archive %q: %w", path, err)
87+
}
88+
defer f.Close()
89+
90+
return compiler.WriteArchive(archive, f)
91+
}
92+
93+
// targetDir returns path to the directory where precompiled packages must be
94+
// stored.
95+
func targetDir(s *build.Session) (string, error) {
96+
pkg, err := s.XContext().Import("github.com/gopherjs/gopherjs.github.io/playground", "", gobuild.FindOnly)
97+
if err != nil {
98+
return "", fmt.Errorf("failed to find playground package directory: %w", err)
99+
}
100+
target := filepath.Join(pkg.Dir, "pkg")
101+
if _, err := os.Stat(target); os.IsNotExist(err) {
102+
return "", fmt.Errorf("target directory %q not found", target)
103+
}
104+
return target, nil
105+
}
106+
107+
// importable excludes packages that are incompatible with GopherJS or can't be
108+
// directly imported by the user code. The remaining packages will be used as a
109+
// starting points for precompilation.
110+
func importable(all []string) []string {
111+
result := []string{}
112+
for _, pkg := range all {
113+
switch {
114+
case strings.HasPrefix(pkg, "vendor/"),
115+
strings.Contains(pkg, "internal"),
116+
strings.Contains(pkg, "pprof"),
117+
strings.Contains(pkg, "plugin"):
118+
continue
119+
default:
120+
result = append(result, pkg)
121+
}
122+
}
123+
return result
124+
}
125+
126+
func main() {
127+
flag.Parse()
128+
log.SetLevel(logLevel.Level)
129+
if err := run(); err != nil {
130+
log.Fatalf("Precompilation failed: %v", err)
131+
}
132+
}

playground/pkg/archive/tar.a.js

-119 KB
Binary file not shown.

playground/pkg/archive/zip.a.js

-72.7 KB
Binary file not shown.

playground/pkg/bufio.a.js

-29.1 KB
Binary file not shown.

playground/pkg/bytes.a.js

-33.1 KB
Binary file not shown.

playground/pkg/compress/bzip2.a.js

-22.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)