Skip to content

compiler: automate regeneration of prelude #784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 20, 2018
3 changes: 3 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ dependencies:
post:
- mv ./gopherjs $HOME/bin
- npm install --global node-gyp
- npm install # install our (dev) dependencies from package.json
- cd node-syscall && node-gyp rebuild && mkdir -p ~/.node_libraries/ && cp build/Release/syscall.node ~/.node_libraries/syscall.node

test:
override:
- go generate github.com/gopherjs/gopherjs/compiler/prelude
- diff -u <(echo -n) <(git status --porcelain)
- diff -u <(echo -n) <(gofmt -d .)
- go tool vet *.go # Go package in root directory.
- for d in */; do echo $d; done | grep -v tests/ | grep -v third_party/ | xargs go tool vet # All subdirectories except "tests", "third_party".
Expand Down
63 changes: 63 additions & 0 deletions compiler/prelude/genmin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// +build ignore

package main

import (
"bytes"
"fmt"
"go/build"
"io/ioutil"
"log"
"os/exec"
"path/filepath"
"strings"

"github.com/gopherjs/gopherjs/compiler/prelude"
)

func main() {
if err := run(); err != nil {
log.Fatalln(err)
}
}

func run() error {
bpkg, err := build.Import("github.com/gopherjs/gopherjs", "", build.FindOnly)
if err != nil {
return fmt.Errorf("failed to locate path for github.com/gopherjs/gopherjs: %v", err)
}

preludeDir := filepath.Join(bpkg.Dir, "compiler", "prelude")

args := []string{
filepath.Join(bpkg.Dir, "node_modules", ".bin", "uglifyjs"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bpkg.Dir is not a very readable name here. I would've copied the importPathToDir helper from:

func importPathToDir(importPath string) string {
p, err := build.Import(importPath, "", build.FindOnly)
if err != nil {
log.Fatalln(err)
}
return p.Dir
}

Then code above can be gopherjsRoot := importPathToDir("github.com/gopherjs/gopherjs"), and this line can be filepath.Join(gopherjsRoot, "node_modules", ".bin", "uglifyjs").

In theory, you could also just resolve both dirs directly via importPathToDir("github.com/gopherjs/gopherjs/compiler/prelude") and importPathToDir("github.com/gopherjs/gopherjs/node_modules/.bin"). importPathToDir can resolve any existing directory in a GOPATH workspace, it doesn't need to be a valid Go package.

Not a big deal, just pointing it out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd tend to disagree; using go/build a lot, it feels very idiomatic to do exactly this sort of resolution. Wrapping exactly the same behaviour in a function just adds to the non-exported API surface and therefore cognitive overhead to my mind (i.e. I have to remember that importPathToDir exists and to then use it). So I'll leave as is if that's ok with you.

"--config-file",
filepath.Join(preludeDir, "uglifyjs_options.json"),
}

stderr := new(bytes.Buffer)
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdin = strings.NewReader(prelude.Prelude)
cmd.Stderr = stderr

out, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to run %v: %v\n%s", strings.Join(args, " "), err, stderr.String())
}

fn := "prelude_min.go"

outStr := fmt.Sprintf(`// Code generated by genmin; DO NOT EDIT.

package prelude

// Minified is an uglifyjs-minified version of Prelude.
const Minified = %q
`, out)

if err := ioutil.WriteFile(fn, []byte(outStr), 0644); err != nil {
return fmt.Errorf("failed to write to %v: %v", fn, err)
}

return nil
}
7 changes: 3 additions & 4 deletions compiler/prelude/prelude.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions compiler/prelude/prelude_min.go

Large diffs are not rendered by default.

98 changes: 98 additions & 0 deletions compiler/prelude/uglifyjs_options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"compress": {
"arrows": true,
"booleans": true,
"collapse_vars": true,
"comparisons": true,
"computed_props": true,
"conditionals": true,
"dead_code": true,
"drop_console": false,
"drop_debugger": true,
"ecma": 5,
"evaluate": true,
"expression": false,
"global_defs": {},
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"ie8": false,
"if_return": true,
"inline": true,
"join_vars": true,
"keep_classnames": false,
"keep_fargs": true,
"keep_fnames": false,
"keep_infinity": false,
"loops": true,
"negate_iife": true,
"passes": 1,
"properties": true,
"pure_funcs": null,
"pure_getters": "strict",
"reduce_funcs": true,
"reduce_vars": true,
"sequences": true,
"side_effects": true,
"switches": true,
"top_retain": null,
"toplevel": false,
"typeofs": true,
"unsafe": false,
"unsafe_Function": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_math": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": true,
"warnings": false
},
"mangle": {
"eval": false,
"ie8": false,
"keep_classnames": false,
"keep_fnames": false,
"properties": false,
"reserved": [],
"safari10": false,
"toplevel": false
},
"output": {
"ascii_only": false,
"beautify": false,
"bracketize": false,
"comments": "/@license|@preserve|^!/",
"ecma": 5,
"ie8": false,
"indent_level": 4,
"indent_start": 0,
"inline_script": true,
"keep_quoted_props": false,
"max_line_len": false,
"preamble": null,
"preserve_line": false,
"quote_keys": false,
"quote_style": 0,
"safari10": false,
"semicolons": true,
"shebang": true,
"source_map": null,
"webkit": false,
"width": 80,
"wrap_iife": false
},
"parse": {
"bare_returns": false,
"ecma": 8,
"expression": false,
"filename": null,
"html5_comments": true,
"shebang": true,
"strict": false,
"toplevel": null
},
"wrap": false
}
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "gopherjs",
"devDependencies": {
"uglify-es": "3.3.9"
}
}