Skip to content

Commit 22e0772

Browse files
committed
compiler/prelude: move prelude to separate formatted .js files
**DO NOT MERGE** _Based on #784 - this will need a rebase once #784 is merged._ See the diff this PR introduces [here](myitcv/gopherjs@regen_min...myitcv:js_file_prelude) I've been working with the prelude quite a lot recently. One of the really painful things with the current implementation is that the JavaScript for the prelude exists as a `const` string in a `.go` file, which makes properly writing and formatting the Javascript itself very difficult. This PR splits the prelude into separate `.js` files therefore and adds a `formatpreludejs.go` `go generate`-er to format those `.js` files in place.
1 parent 00efd92 commit 22e0772

16 files changed

+1984
-1696
lines changed

compiler/prelude/formatpreludejs.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// +build ignore
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"go/build"
8+
"io/ioutil"
9+
"log"
10+
"os/exec"
11+
"path/filepath"
12+
"strings"
13+
)
14+
15+
func main() {
16+
if err := run(); err != nil {
17+
log.Fatalln(err)
18+
}
19+
}
20+
21+
func run() error {
22+
bpkg, err := build.Import("github.com/gopherjs/gopherjs", "", build.FindOnly)
23+
if err != nil {
24+
return fmt.Errorf("failed to locate path for github.com/gopherjs/gopherjs/compiler/prelude: %v", err)
25+
}
26+
27+
preludeDir := filepath.Join(bpkg.Dir, "compiler", "prelude")
28+
29+
args := []string{
30+
filepath.Join(bpkg.Dir, "node_modules", ".bin", "prettier"),
31+
"--config",
32+
filepath.Join(preludeDir, "prettier_options.json"),
33+
"--write",
34+
}
35+
36+
fis, err := ioutil.ReadDir(preludeDir)
37+
if err != nil {
38+
return fmt.Errorf("failed to list contents of %v: %v", preludeDir, err)
39+
}
40+
for _, fi := range fis {
41+
fn := fi.Name()
42+
if !strings.HasSuffix(fn, ".js") || strings.HasSuffix(fn, ".min.js") {
43+
continue
44+
}
45+
args = append(args, fn)
46+
}
47+
48+
cmd := exec.Command(args[0], args[1:]...)
49+
50+
out, err := cmd.CombinedOutput()
51+
if err != nil {
52+
return fmt.Errorf("failed to run %v: %v\n%s", strings.Join(args, " "), err, string(out))
53+
}
54+
55+
return nil
56+
}

compiler/prelude/generate.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package prelude
2+
3+
//go:generate go run formatpreludejs.go
4+
//go:generate go run genprelude.go

compiler/prelude/genmin.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

compiler/prelude/genprelude.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// +build ignore
2+
3+
package main
4+
5+
import (
6+
"bytes"
7+
"fmt"
8+
"go/build"
9+
"io/ioutil"
10+
"log"
11+
"os/exec"
12+
"path/filepath"
13+
"strings"
14+
)
15+
16+
const (
17+
fn = "prelude.go"
18+
minFn = "prelude_min.go"
19+
)
20+
21+
func main() {
22+
if err := run(); err != nil {
23+
log.Fatalln(err)
24+
}
25+
}
26+
27+
func run() error {
28+
bpkg, err := build.Import("github.com/gopherjs/gopherjs", "", build.FindOnly)
29+
if err != nil {
30+
return fmt.Errorf("failed to locate path for github.com/gopherjs/gopherjs: %v", err)
31+
}
32+
33+
preludeDir := filepath.Join(bpkg.Dir, "compiler", "prelude")
34+
35+
files := []string{
36+
"prelude.js",
37+
"numeric.js",
38+
"types.js",
39+
"goroutines.js",
40+
"jsmapping.js",
41+
}
42+
43+
prelude := new(bytes.Buffer)
44+
45+
for _, f := range files {
46+
p := filepath.Join(preludeDir, f)
47+
byts, err := ioutil.ReadFile(p)
48+
if err != nil {
49+
return fmt.Errorf("failed to read from %v: %v", p, err)
50+
}
51+
if _, err := prelude.Write(byts); err != nil {
52+
return fmt.Errorf("failed to append prelude: %v", err)
53+
}
54+
}
55+
56+
args := append([]string{
57+
filepath.Join(bpkg.Dir, "node_modules", ".bin", "uglifyjs"),
58+
"--config-file",
59+
filepath.Join(preludeDir, "uglifyjs_options.json"),
60+
}, files...)
61+
62+
cmd := exec.Command(args[0], args[1:]...)
63+
64+
out, err := cmd.CombinedOutput()
65+
if err != nil {
66+
return fmt.Errorf("failed to run %v: %v\n%s", strings.Join(args, " "), err, string(out))
67+
}
68+
69+
err = writeOutput("prelude.go", ""+
70+
"// Prelude is the GopherJS JavaScript interop layer that is behind\n"+
71+
"// the github.com/gopherjs/gopherjs/js package\n"+
72+
"const Prelude = %q", prelude.Bytes())
73+
74+
if err != nil {
75+
return err
76+
}
77+
78+
err = writeOutput("prelude_min.go", ""+
79+
"// Minified is an uglifyjs-minified version of Prelude\n"+
80+
"const Minified = %q", out)
81+
82+
return err
83+
}
84+
85+
func writeOutput(fn string, format string, byts []byte) error {
86+
content := fmt.Sprintf("// Code generated by genprelude. DO NOT EDIT\n\n"+
87+
"package prelude\n\n"+format+"\n", byts)
88+
89+
if err := ioutil.WriteFile(fn, []byte(content), 0644); err != nil {
90+
return fmt.Errorf("failed to write to %v: %v", fn, err)
91+
}
92+
93+
return nil
94+
}

0 commit comments

Comments
 (0)