@@ -27,6 +27,9 @@ import (
27
27
"github.com/fsnotify/fsnotify"
28
28
"github.com/gopherjs/gopherjs/compiler"
29
29
"github.com/gopherjs/gopherjs/compiler/astutil"
30
+ "github.com/gopherjs/gopherjs/compiler/errorList"
31
+ "github.com/gopherjs/gopherjs/compiler/jsFile"
32
+ "github.com/gopherjs/gopherjs/compiler/sources"
30
33
"github.com/gopherjs/gopherjs/internal/testmain"
31
34
log "github.com/sirupsen/logrus"
32
35
@@ -163,7 +166,7 @@ type overrideInfo struct {
163
166
// - Otherwise for identifiers that exist in the original and the overrides,
164
167
// the original is removed.
165
168
// - New identifiers that don't exist in original package get added.
166
- func parseAndAugment (xctx XContext , pkg * PackageData , isTest bool , fileSet * token.FileSet ) ([]* ast.File , []JSFile , error ) {
169
+ func parseAndAugment (xctx XContext , pkg * PackageData , isTest bool , fileSet * token.FileSet ) ([]* ast.File , []jsFile. JSFile , error ) {
167
170
jsFiles , overlayFiles := parseOverlayFiles (xctx , pkg , isTest , fileSet )
168
171
169
172
originalFiles , err := parserOriginalFiles (pkg , fileSet )
@@ -192,7 +195,7 @@ func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *toke
192
195
193
196
// parseOverlayFiles loads and parses overlay files
194
197
// to augment the original files with.
195
- func parseOverlayFiles (xctx XContext , pkg * PackageData , isTest bool , fileSet * token.FileSet ) ([]JSFile , []* ast.File ) {
198
+ func parseOverlayFiles (xctx XContext , pkg * PackageData , isTest bool , fileSet * token.FileSet ) ([]jsFile. JSFile , []* ast.File ) {
196
199
isXTest := strings .HasSuffix (pkg .ImportPath , "_test" )
197
200
importPath := pkg .ImportPath
198
201
if isXTest {
@@ -238,7 +241,7 @@ func parseOverlayFiles(xctx XContext, pkg *PackageData, isTest bool, fileSet *to
238
241
// parserOriginalFiles loads and parses the original files to augment.
239
242
func parserOriginalFiles (pkg * PackageData , fileSet * token.FileSet ) ([]* ast.File , error ) {
240
243
var files []* ast.File
241
- var errList compiler .ErrorList
244
+ var errList errorList .ErrorList
242
245
for _ , name := range pkg .GoFiles {
243
246
if ! filepath .IsAbs (name ) { // name might be absolute if specified directly. E.g., `gopherjs build /abs/file.go`.
244
247
name = filepath .Join (pkg .Dir , name )
@@ -622,18 +625,11 @@ func (o *Options) PrintSuccess(format string, a ...interface{}) {
622
625
fmt .Fprintf (os .Stderr , format , a ... )
623
626
}
624
627
625
- // JSFile represents a *.inc.js file metadata and content.
626
- type JSFile struct {
627
- Path string // Full file path for the build context the file came from.
628
- ModTime time.Time
629
- Content []byte
630
- }
631
-
632
628
// PackageData is an extension of go/build.Package with additional metadata
633
629
// GopherJS requires.
634
630
type PackageData struct {
635
631
* build.Package
636
- JSFiles []JSFile
632
+ JSFiles []jsFile. JSFile
637
633
// IsTest is true if the package is being built for running tests.
638
634
IsTest bool
639
635
SrcModTime time.Time
@@ -743,43 +739,6 @@ func (p *PackageData) InstallPath() string {
743
739
return p .PkgObj
744
740
}
745
741
746
- // ParsedPackage is the results from building a package before it is compiled.
747
- type ParsedPackage struct {
748
- ImportPath string // import path of package ("" if unknown)
749
- Dir string // directory containing package sources
750
-
751
- // GoFiles is the parsed and augmented Go AST files for the package.
752
- GoFiles []* ast.File
753
- FileSet * token.FileSet
754
- JSFiles []JSFile
755
- }
756
-
757
- // Imports calculates the import paths of the package's dependencies
758
- // based on all the imports in the augmented files.
759
- //
760
- // The given skip paths will not be returned in the results.
761
- // This will not return any `*_test` packages in the results.
762
- func (p * ParsedPackage ) Imports (skip ... string ) []string {
763
- seen := make (map [string ]struct {})
764
- for _ , s := range skip {
765
- seen [s ] = struct {}{}
766
- }
767
- imports := []string {}
768
- for _ , file := range p .GoFiles {
769
- for _ , imp := range file .Imports {
770
- path := strings .Trim (imp .Path .Value , `"` )
771
- if _ , ok := seen [path ]; ! ok {
772
- if ! strings .HasSuffix (path , "_test" ) {
773
- imports = append (imports , path )
774
- }
775
- seen [path ] = struct {}{}
776
- }
777
- }
778
- }
779
- sort .Strings (imports )
780
- return imports
781
- }
782
-
783
742
// Session manages internal state GopherJS requires to perform a build.
784
743
//
785
744
// This is the main interface to GopherJS build system. Session lifetime is
@@ -793,10 +752,11 @@ type Session struct {
793
752
// compilation when we're looking up parsed packages.
794
753
importPaths map [string ]map [string ]string
795
754
796
- // parsePackage is a map of parsed packages that have been built and augmented.
755
+ // sources is a map of parsed packages that have been built and augmented.
797
756
// This is keyed using resolved import paths. This is used to avoid
798
757
// rebuilding and augmenting packages that are imported by several packages.
799
- parsedPackages map [string ]* ParsedPackage
758
+ // These sources haven't been sorted nor simplified yet.
759
+ sources map [string ]* sources.Sources
800
760
801
761
// Binary archives produced during the current session and assumed to be
802
762
// up to date with input sources and dependencies. In the -w ("watch") mode
@@ -813,7 +773,7 @@ func NewSession(options *Options) (*Session, error) {
813
773
s := & Session {
814
774
options : options ,
815
775
importPaths : make (map [string ]map [string ]string ),
816
- parsedPackages : make (map [string ]* ParsedPackage ),
776
+ sources : make (map [string ]* sources. Sources ),
817
777
UpToDateArchives : make (map [string ]* compiler.Archive ),
818
778
}
819
779
s .xctx = NewBuildContext (s .InstallSuffix (), s .options .BuildTags )
@@ -931,7 +891,7 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, cwd string) erro
931
891
if err != nil {
932
892
return fmt .Errorf ("failed to stat %s: %w" , file , err )
933
893
}
934
- pkg .JSFiles = append (pkg .JSFiles , JSFile {
894
+ pkg .JSFiles = append (pkg .JSFiles , jsFile. JSFile {
935
895
Path : filepath .Join (pkg .Dir , filepath .Base (file )),
936
896
ModTime : info .ModTime (),
937
897
Content : content ,
@@ -954,13 +914,13 @@ func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) {
954
914
// ensure that runtime for gopherjs is imported
955
915
pkg .Imports = append (pkg .Imports , `runtime` )
956
916
957
- // Build the project to get the parsed packages.
958
- var parsed * ParsedPackage
917
+ // Build the project to get the sources for the parsed packages.
918
+ var srcs * sources. Sources
959
919
var err error
960
920
if pkg .IsTest {
961
- parsed , err = s .buildTestPackage (pkg )
921
+ srcs , err = s .buildTestPackage (pkg )
962
922
} else {
963
- parsed , err = s .buildPackages (pkg )
923
+ srcs , err = s .buildPackages (pkg )
964
924
}
965
925
if err != nil {
966
926
return nil , err
@@ -972,10 +932,10 @@ func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) {
972
932
// flatten, blocking, etc. information and check types to get the type info
973
933
// with all the instances for all generics in the whole project.
974
934
975
- return s .compilePackages (parsed )
935
+ return s .compilePackages (srcs )
976
936
}
977
937
978
- func (s * Session ) buildTestPackage (pkg * PackageData ) (* ParsedPackage , error ) {
938
+ func (s * Session ) buildTestPackage (pkg * PackageData ) (* sources. Sources , error ) {
979
939
_ , err := s .buildPackages (pkg .TestPackage ())
980
940
if err != nil {
981
941
return nil , err
@@ -994,30 +954,30 @@ func (s *Session) buildTestPackage(pkg *PackageData) (*ParsedPackage, error) {
994
954
return nil , fmt .Errorf ("failed to generate testmain package for %s: %w" , pkg .ImportPath , err )
995
955
}
996
956
997
- // Create a parsed package for the testmain package.
998
- parsed := & ParsedPackage {
957
+ // Create the sources for parsed package for the testmain package.
958
+ srcs := & sources. Sources {
999
959
ImportPath : mainPkg .ImportPath ,
1000
960
Dir : mainPkg .Dir ,
1001
- GoFiles : []* ast.File {mainFile },
961
+ Files : []* ast.File {mainFile },
1002
962
FileSet : fset ,
1003
963
}
1004
964
1005
965
// Import dependencies for the testmain package.
1006
- for _ , importedPkgPath := range parsed .Imports () {
966
+ for _ , importedPkgPath := range srcs .Imports () {
1007
967
_ , _ , err := s .buildImportPathWithSrcDir (importedPkgPath , pkg .Dir )
1008
968
if err != nil {
1009
969
return nil , err
1010
970
}
1011
971
}
1012
972
1013
- return parsed , nil
973
+ return srcs , nil
1014
974
}
1015
975
1016
- // buildImportPathWithSrcDir builds the parsed package specified by the import path.
976
+ // buildImportPathWithSrcDir builds the sources for a package specified by the import path.
1017
977
//
1018
978
// Relative import paths are interpreted relative to the passed srcDir. If
1019
979
// srcDir is empty, current working directory is assumed.
1020
- func (s * Session ) buildImportPathWithSrcDir (path , srcDir string ) (* PackageData , * ParsedPackage , error ) {
980
+ func (s * Session ) buildImportPathWithSrcDir (path , srcDir string ) (* PackageData , * sources. Sources , error ) {
1021
981
pkg , err := s .xctx .Import (path , srcDir , 0 )
1022
982
if s .Watcher != nil && pkg != nil { // add watch even on error
1023
983
s .Watcher .Add (pkg .Dir )
@@ -1026,13 +986,13 @@ func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData,
1026
986
return nil , nil , err
1027
987
}
1028
988
1029
- parsed , err := s .buildPackages (pkg )
989
+ srcs , err := s .buildPackages (pkg )
1030
990
if err != nil {
1031
991
return nil , nil , err
1032
992
}
1033
993
1034
994
s .cacheImportPath (path , srcDir , pkg .ImportPath )
1035
- return pkg , parsed , nil
995
+ return pkg , srcs , nil
1036
996
}
1037
997
1038
998
// cacheImportPath stores the import path for the build package so we can look
@@ -1073,9 +1033,9 @@ var getExeModTime = func() func() time.Time {
1073
1033
}
1074
1034
}()
1075
1035
1076
- func (s * Session ) buildPackages (pkg * PackageData ) (* ParsedPackage , error ) {
1077
- if parsed , ok := s .parsedPackages [pkg .ImportPath ]; ok {
1078
- return parsed , nil
1036
+ func (s * Session ) buildPackages (pkg * PackageData ) (* sources. Sources , error ) {
1037
+ if srcs , ok := s .sources [pkg .ImportPath ]; ok {
1038
+ return srcs , nil
1079
1039
}
1080
1040
1081
1041
if exeModTime := getExeModTime (); exeModTime .After (pkg .SrcModTime ) {
@@ -1114,52 +1074,52 @@ func (s *Session) buildPackages(pkg *PackageData) (*ParsedPackage, error) {
1114
1074
files = append (files , embed )
1115
1075
}
1116
1076
1117
- parsed := & ParsedPackage {
1077
+ srcs := & sources. Sources {
1118
1078
ImportPath : pkg .ImportPath ,
1119
1079
Dir : pkg .Dir ,
1120
- GoFiles : files ,
1080
+ Files : files ,
1121
1081
FileSet : fileSet ,
1122
1082
JSFiles : append (pkg .JSFiles , overlayJsFiles ... ),
1123
1083
}
1124
- s .parsedPackages [pkg .ImportPath ] = parsed
1084
+ s .sources [pkg .ImportPath ] = srcs
1125
1085
1126
1086
// Import dependencies from the augmented files,
1127
1087
// whilst skipping any that have been already imported.
1128
- for _ , importedPkgPath := range parsed .Imports (pkg .Imports ... ) {
1088
+ for _ , importedPkgPath := range srcs .Imports (pkg .Imports ... ) {
1129
1089
_ , _ , err := s .buildImportPathWithSrcDir (importedPkgPath , pkg .Dir )
1130
1090
if err != nil {
1131
1091
return nil , err
1132
1092
}
1133
1093
}
1134
1094
1135
- return parsed , nil
1095
+ return srcs , nil
1136
1096
}
1137
1097
1138
- func (s * Session ) compilePackages (pkg * ParsedPackage ) (* compiler.Archive , error ) {
1139
- if archive , ok := s .UpToDateArchives [pkg .ImportPath ]; ok {
1098
+ func (s * Session ) compilePackages (srcs * sources. Sources ) (* compiler.Archive , error ) {
1099
+ if archive , ok := s .UpToDateArchives [srcs .ImportPath ]; ok {
1140
1100
return archive , nil
1141
1101
}
1142
1102
1143
1103
importContext := & compiler.ImportContext {
1144
- Packages : s .Types ,
1145
- Import : s .ImportResolverFor (pkg .Dir ),
1104
+ Packages : s .Types ,
1105
+ ImportArchive : s .ImportResolverFor (srcs .Dir ),
1146
1106
}
1147
- archive , err := compiler .Compile (pkg . ImportPath , pkg . GoFiles , pkg . FileSet , importContext , s .options .Minify )
1107
+ archive , err := compiler .Compile (* srcs , importContext , s .options .Minify )
1148
1108
if err != nil {
1149
1109
return nil , err
1150
1110
}
1151
1111
1152
- for _ , jsFile := range pkg .JSFiles {
1112
+ for _ , jsFile := range srcs .JSFiles {
1153
1113
archive .IncJSCode = append (archive .IncJSCode , []byte ("\t (function() {\n " )... )
1154
1114
archive .IncJSCode = append (archive .IncJSCode , jsFile .Content ... )
1155
1115
archive .IncJSCode = append (archive .IncJSCode , []byte ("\n \t }).call($global);\n " )... )
1156
1116
}
1157
1117
1158
1118
if s .options .Verbose {
1159
- fmt .Println (pkg .ImportPath )
1119
+ fmt .Println (srcs .ImportPath )
1160
1120
}
1161
1121
1162
- s .UpToDateArchives [pkg .ImportPath ] = archive
1122
+ s .UpToDateArchives [srcs .ImportPath ] = archive
1163
1123
1164
1124
return archive , nil
1165
1125
}
@@ -1197,12 +1157,12 @@ func (s *Session) ImportResolverFor(srcDir string) func(string) (*compiler.Archi
1197
1157
return archive , nil
1198
1158
}
1199
1159
1200
- // The archive hasn't been compiled yet so compile it with the parsed package .
1201
- if parsed , ok := s .parsedPackages [importPath ]; ok {
1202
- return s .compilePackages (parsed )
1160
+ // The archive hasn't been compiled yet so compile it with the sources .
1161
+ if srcs , ok := s .sources [importPath ]; ok {
1162
+ return s .compilePackages (srcs )
1203
1163
}
1204
1164
1205
- return nil , fmt .Errorf (`parsed package for %q not found` , importPath )
1165
+ return nil , fmt .Errorf (`sources for %q not found` , importPath )
1206
1166
}
1207
1167
}
1208
1168
0 commit comments