github.com/goplusjs/gopherjs@v1.2.6-0.20211206034512-f187917453b8/compiler/prelude/genmin.go (about) 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 "github.com/goplusjs/gopherjs/compiler/prelude" 16 ) 17 18 func main() { 19 if err := run(); err != nil { 20 log.Fatalln(err) 21 } 22 } 23 24 func run() error { 25 bpkg, err := build.Import("github.com/goplusjs/gopherjs", "", build.FindOnly) 26 if err != nil { 27 return fmt.Errorf("failed to locate path for github.com/gopherjs/gopherjs: %v", err) 28 } 29 30 preludeDir := filepath.Join(bpkg.Dir, "compiler", "prelude") 31 32 args := []string{ 33 filepath.Join(bpkg.Dir, "node_modules", ".bin", "uglifyjs"), 34 "--config-file", 35 filepath.Join(preludeDir, "uglifyjs_options.json"), 36 } 37 38 stderr := new(bytes.Buffer) 39 cmd := exec.Command(args[0], args[1:]...) 40 cmd.Stdin = strings.NewReader(prelude.Prelude) 41 cmd.Stderr = stderr 42 43 out, err := cmd.Output() 44 if err != nil { 45 return fmt.Errorf("failed to run %v: %v\n%s", strings.Join(args, " "), err, stderr.String()) 46 } 47 48 fn := "prelude_min.go" 49 50 outStr := fmt.Sprintf(`// Code generated by genmin; DO NOT EDIT. 51 52 package prelude 53 54 // Minified is an uglifyjs-minified version of Prelude. 55 const Minified = %q 56 `, out) 57 58 if err := ioutil.WriteFile(fn, []byte(outStr), 0644); err != nil { 59 return fmt.Errorf("failed to write to %v: %v", fn, err) 60 } 61 62 return nil 63 }