From daf920e8505232be763c60b699305bd52eee14ae Mon Sep 17 00:00:00 2001 From: visualfc Date: Sun, 21 Feb 2021 23:10:52 +0800 Subject: [PATCH 1/2] add --anylize for dump pkgs info --- build/build.go | 18 +++++++++++++++--- compiler/compiler.go | 40 +++++++++++++++++++++++++++++++++------- tool.go | 3 ++- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/build/build.go b/build/build.go index 9c4b27a6..84ebf57b 100644 --- a/build/build.go +++ b/build/build.go @@ -2,6 +2,7 @@ package build import ( "bytes" + "encoding/json" "fmt" "go/ast" "go/build" @@ -486,8 +487,9 @@ type Options struct { MapToLocalDisk bool Minify bool Color bool - BuildTags []string Rebuild bool + Analyze bool + BuildTags []string } func (o *Options) PrintError(format string, a ...interface{}) { @@ -973,7 +975,7 @@ func (s *Session) WriteCommandPackage(archive *compiler.Archive, pkgObj string) defer func() { m.WriteTo(mapFile) mapFile.Close() - fmt.Fprintf(codeFile, "//# sourceMappingURL=%s.map\n", filepath.Base(pkgObj)) + fmt.Fprintf(sourceMapFilter.Writer, "//# sourceMappingURL=%s.map\n", filepath.Base(pkgObj)) }() sourceMapFilter.MappingCallback = NewMappingCallback(m, s.options.GOROOT, s.options.GOPATH, s.options.MapToLocalDisk) @@ -989,7 +991,17 @@ func (s *Session) WriteCommandPackage(archive *compiler.Archive, pkgObj string) if err != nil { return err } - return compiler.WriteProgramCode(deps, sourceMapFilter) + infos, err := compiler.WriteProgramCode(deps, sourceMapFilter) + if s.options.Analyze { + data, err := json.MarshalIndent(&infos, "", "\t") + if err != nil { + fmt.Println("json error", err) + } else { + fmt.Printf("anylize pkg: size %v\n", sourceMapFilter.Offset()) + fmt.Println(string(data)) + } + } + return err } func NewMappingCallback(m *sourcemap.Map, goroot, gopath string, localMap bool) func(generatedLine, generatedColumn int, originalPos token.Position) { diff --git a/compiler/compiler.go b/compiler/compiler.go index dc72bcd4..c0e33d41 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -101,7 +101,7 @@ type dceInfo struct { methodFilter string } -func WriteProgramCode(pkgs []*Archive, w *SourceMapFilter) error { +func WriteProgramCode(pkgs []*Archive, w *SourceMapFilter) ([]*PkgInfo, error) { mainPkg := pkgs[len(pkgs)-1] minify := mainPkg.Minified @@ -151,31 +151,51 @@ func WriteProgramCode(pkgs []*Archive, w *SourceMapFilter) error { } if _, err := w.Write([]byte("\"use strict\";\n(function() {\n\n")); err != nil { - return err + return nil, err } preludeJS := prelude.Prelude if minify { preludeJS = prelude.Minified } if _, err := io.WriteString(w, preludeJS); err != nil { - return err + return nil, err } if _, err := w.Write([]byte("\n")); err != nil { - return err + return nil, err } // write packages + offset := w.offset + + var infos []*PkgInfo + infos = append(infos, &PkgInfo{ + Name: "$prelude", + Size: w.offset, + }) for _, pkg := range pkgs { if err := WritePkgCode(pkg, dceSelection, minify, w); err != nil { - return err + return nil, err } + infos = append(infos, &PkgInfo{ + Name: pkg.Name, + ImportPath: pkg.ImportPath, + Imports: pkg.Imports, + Size: w.offset - offset, + }) + offset = w.offset } if _, err := w.Write([]byte("$synthesizeMethods();\nvar $mainPkg = $packages[\"" + string(mainPkg.ImportPath) + "\"];\n$packages[\"runtime\"].$init();\n$go($mainPkg.$init, []);\n$flushConsole();\n\n}).call(this);\n")); err != nil { - return err + return nil, err } + return infos, nil +} - return nil +type PkgInfo struct { + Name string `json:"Name"` + ImportPath string `json:"ImportPath,omitempty"` + Imports []string `json:"Imports,omitempty"` + Size int64 `json:"Size"` } func WritePkgCode(pkg *Archive, dceSelection map[*Decl]struct{}, minify bool, w *SourceMapFilter) error { @@ -259,9 +279,14 @@ type SourceMapFilter struct { MappingCallback func(generatedLine, generatedColumn int, originalPos token.Position) line int column int + offset int64 fileSet *token.FileSet } +func (f *SourceMapFilter) Offset() int64 { + return f.offset +} + func (f *SourceMapFilter) Write(p []byte) (n int, err error) { var n2 int for { @@ -273,6 +298,7 @@ func (f *SourceMapFilter) Write(p []byte) (n int, err error) { n2, err = f.Writer.Write(w) n += n2 + f.offset += int64(n2) for { i := bytes.IndexByte(w, '\n') if i == -1 { diff --git a/tool.go b/tool.go index 13afc895..48116bc7 100644 --- a/tool.go +++ b/tool.go @@ -82,6 +82,7 @@ func main() { compilerFlags.StringVar(&tags, "tags", "", "a list of build tags to consider satisfied during the build") compilerFlags.BoolVar(&options.MapToLocalDisk, "localmap", false, "use local paths for sourcemap") compilerFlags.BoolVarP(&options.Rebuild, "force", "a", false, "force rebuilding of packages that are already up-to-date") + compilerFlags.BoolVar(&options.Analyze, "analyze", false, "analyze build package's json info") flagWatch := pflag.NewFlagSet("", 0) flagWatch.BoolVarP(&options.Watch, "watch", "w", false, "watch for changes to the source files") @@ -589,7 +590,7 @@ func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) { if err != nil { return err } - if err := compiler.WriteProgramCode(deps, sourceMapFilter); err != nil { + if _, err = compiler.WriteProgramCode(deps, sourceMapFilter); err != nil { return err } From 07b1b107e1cc027f35d46802b260afa03e3c8f41 Mon Sep 17 00:00:00 2001 From: visualfc Date: Mon, 22 Feb 2021 09:36:24 +0800 Subject: [PATCH 2/2] set --anylize for simply output, --anylize --json for dump json format --- build/build.go | 21 ++++++++++++++++----- compiler/compiler.go | 5 +++-- tool.go | 3 ++- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/build/build.go b/build/build.go index 84ebf57b..9478fec9 100644 --- a/build/build.go +++ b/build/build.go @@ -489,6 +489,7 @@ type Options struct { Color bool Rebuild bool Analyze bool + AnalyzeJson bool BuildTags []string } @@ -993,12 +994,22 @@ func (s *Session) WriteCommandPackage(archive *compiler.Archive, pkgObj string) } infos, err := compiler.WriteProgramCode(deps, sourceMapFilter) if s.options.Analyze { - data, err := json.MarshalIndent(&infos, "", "\t") - if err != nil { - fmt.Println("json error", err) - } else { - fmt.Printf("anylize pkg: size %v\n", sourceMapFilter.Offset()) + if s.options.AnalyzeJson { + data, _ := json.MarshalIndent(&infos, "", "\t") fmt.Println(string(data)) + } else { + var maxSize int64 + for _, info := range infos { + if info.Size > maxSize { + maxSize = info.Size + } + } + noff := len(strconv.FormatInt(maxSize, 10)) + format := fmt.Sprintf("%%%vv\t%%v\n", noff) + fmt.Printf(format, "", "") + for _, info := range infos { + fmt.Printf(format, info.Size, info.ImportPath) + } } } return err diff --git a/compiler/compiler.go b/compiler/compiler.go index c0e33d41..ff319f46 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -169,8 +169,9 @@ func WriteProgramCode(pkgs []*Archive, w *SourceMapFilter) ([]*PkgInfo, error) { var infos []*PkgInfo infos = append(infos, &PkgInfo{ - Name: "$prelude", - Size: w.offset, + Name: "$prelude", + ImportPath: "$prelude", + Size: w.offset, }) for _, pkg := range pkgs { if err := WritePkgCode(pkg, dceSelection, minify, w); err != nil { diff --git a/tool.go b/tool.go index 48116bc7..e0ba58de 100644 --- a/tool.go +++ b/tool.go @@ -82,7 +82,8 @@ func main() { compilerFlags.StringVar(&tags, "tags", "", "a list of build tags to consider satisfied during the build") compilerFlags.BoolVar(&options.MapToLocalDisk, "localmap", false, "use local paths for sourcemap") compilerFlags.BoolVarP(&options.Rebuild, "force", "a", false, "force rebuilding of packages that are already up-to-date") - compilerFlags.BoolVar(&options.Analyze, "analyze", false, "analyze build package's json info") + compilerFlags.BoolVar(&options.Analyze, "analyze", false, "print analyze build package's info") + compilerFlags.BoolVar(&options.AnalyzeJson, "json", false, "print analyze build package's json info") flagWatch := pflag.NewFlagSet("", 0) flagWatch.BoolVarP(&options.Watch, "watch", "w", false, "watch for changes to the source files")