@@ -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/jsFile"
31
+ "github.com/gopherjs/gopherjs/compiler/sources"
32
+ "github.com/gopherjs/gopherjs/internal/errorList"
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
@@ -797,10 +756,11 @@ type Session struct {
797
756
// is the unresolved import path and source directory.
798
757
importPaths map [string ]map [string ]string
799
758
800
- // parsePackage is a map of parsed packages that have been built and augmented.
759
+ // sources is a map of parsed packages that have been built and augmented.
801
760
// This is keyed using resolved import paths. This is used to avoid
802
761
// rebuilding and augmenting packages that are imported by several packages.
803
- parsedPackages map [string ]* ParsedPackage
762
+ // These sources haven't been sorted nor simplified yet.
763
+ sources map [string ]* sources.Sources
804
764
805
765
// Binary archives produced during the current session and assumed to be
806
766
// up to date with input sources and dependencies. In the -w ("watch") mode
@@ -817,7 +777,7 @@ func NewSession(options *Options) (*Session, error) {
817
777
s := & Session {
818
778
options : options ,
819
779
importPaths : make (map [string ]map [string ]string ),
820
- parsedPackages : make (map [string ]* ParsedPackage ),
780
+ sources : make (map [string ]* sources. Sources ),
821
781
UpToDateArchives : make (map [string ]* compiler.Archive ),
822
782
}
823
783
s .xctx = NewBuildContext (s .InstallSuffix (), s .options .BuildTags )
@@ -935,7 +895,7 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, cwd string) erro
935
895
if err != nil {
936
896
return fmt .Errorf ("failed to stat %s: %w" , file , err )
937
897
}
938
- pkg .JSFiles = append (pkg .JSFiles , JSFile {
898
+ pkg .JSFiles = append (pkg .JSFiles , jsFile. JSFile {
939
899
Path : filepath .Join (pkg .Dir , filepath .Base (file )),
940
900
ModTime : info .ModTime (),
941
901
Content : content ,
@@ -958,13 +918,13 @@ func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) {
958
918
// ensure that runtime for gopherjs is imported
959
919
pkg .Imports = append (pkg .Imports , `runtime` )
960
920
961
- // Build the project to get the parsed packages.
962
- var parsed * ParsedPackage
921
+ // Build the project to get the sources for the parsed packages.
922
+ var srcs * sources. Sources
963
923
var err error
964
924
if pkg .IsTest {
965
- parsed , err = s .loadTestPackage (pkg )
925
+ srcs , err = s .loadTestPackage (pkg )
966
926
} else {
967
- parsed , err = s .loadPackages (pkg )
927
+ srcs , err = s .loadPackages (pkg )
968
928
}
969
929
if err != nil {
970
930
return nil , err
@@ -976,10 +936,10 @@ func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) {
976
936
// flatten, blocking, etc. information and check types to get the type info
977
937
// with all the instances for all generics in the whole project.
978
938
979
- return s .compilePackages (parsed )
939
+ return s .compilePackages (srcs )
980
940
}
981
941
982
- func (s * Session ) loadTestPackage (pkg * PackageData ) (* ParsedPackage , error ) {
942
+ func (s * Session ) loadTestPackage (pkg * PackageData ) (* sources. Sources , error ) {
983
943
_ , err := s .loadPackages (pkg .TestPackage ())
984
944
if err != nil {
985
945
return nil , err
@@ -998,30 +958,30 @@ func (s *Session) loadTestPackage(pkg *PackageData) (*ParsedPackage, error) {
998
958
return nil , fmt .Errorf ("failed to generate testmain package for %s: %w" , pkg .ImportPath , err )
999
959
}
1000
960
1001
- // Create a parsed package for the testmain package.
1002
- parsed := & ParsedPackage {
961
+ // Create the sources for parsed package for the testmain package.
962
+ srcs := & sources. Sources {
1003
963
ImportPath : mainPkg .ImportPath ,
1004
964
Dir : mainPkg .Dir ,
1005
- GoFiles : []* ast.File {mainFile },
965
+ Files : []* ast.File {mainFile },
1006
966
FileSet : fset ,
1007
967
}
1008
968
1009
969
// Import dependencies for the testmain package.
1010
- for _ , importedPkgPath := range parsed . Imports () {
970
+ for _ , importedPkgPath := range srcs . UnresolvedImports () {
1011
971
_ , _ , err := s .loadImportPathWithSrcDir (importedPkgPath , pkg .Dir )
1012
972
if err != nil {
1013
973
return nil , err
1014
974
}
1015
975
}
1016
976
1017
- return parsed , nil
977
+ return srcs , nil
1018
978
}
1019
979
1020
980
// loadImportPathWithSrcDir gets the parsed package specified by the import path.
1021
981
//
1022
982
// Relative import paths are interpreted relative to the passed srcDir.
1023
983
// If srcDir is empty, current working directory is assumed.
1024
- func (s * Session ) loadImportPathWithSrcDir (path , srcDir string ) (* PackageData , * ParsedPackage , error ) {
984
+ func (s * Session ) loadImportPathWithSrcDir (path , srcDir string ) (* PackageData , * sources. Sources , error ) {
1025
985
pkg , err := s .xctx .Import (path , srcDir , 0 )
1026
986
if s .Watcher != nil && pkg != nil { // add watch even on error
1027
987
s .Watcher .Add (pkg .Dir )
@@ -1030,13 +990,13 @@ func (s *Session) loadImportPathWithSrcDir(path, srcDir string) (*PackageData, *
1030
990
return nil , nil , err
1031
991
}
1032
992
1033
- parsed , err := s .loadPackages (pkg )
993
+ srcs , err := s .loadPackages (pkg )
1034
994
if err != nil {
1035
995
return nil , nil , err
1036
996
}
1037
997
1038
998
s .cacheImportPath (path , srcDir , pkg .ImportPath )
1039
- return pkg , parsed , nil
999
+ return pkg , srcs , nil
1040
1000
}
1041
1001
1042
1002
// cacheImportPath stores the resolved import path for the build package
@@ -1077,9 +1037,13 @@ var getExeModTime = func() func() time.Time {
1077
1037
}
1078
1038
}()
1079
1039
1080
- func (s * Session ) loadPackages (pkg * PackageData ) (* ParsedPackage , error ) {
1081
- if parsed , ok := s .parsedPackages [pkg .ImportPath ]; ok {
1082
- return parsed , nil
1040
+ // loadPackages will recursively load and parse the given package and
1041
+ // its dependencies. This will return the sources for the given package.
1042
+ // The returned source and sources for the dependencies will be added
1043
+ // to the session's sources map.
1044
+ func (s * Session ) loadPackages (pkg * PackageData ) (* sources.Sources , error ) {
1045
+ if srcs , ok := s .sources [pkg .ImportPath ]; ok {
1046
+ return srcs , nil
1083
1047
}
1084
1048
1085
1049
if exeModTime := getExeModTime (); exeModTime .After (pkg .SrcModTime ) {
@@ -1118,52 +1082,52 @@ func (s *Session) loadPackages(pkg *PackageData) (*ParsedPackage, error) {
1118
1082
files = append (files , embed )
1119
1083
}
1120
1084
1121
- parsed := & ParsedPackage {
1085
+ srcs := & sources. Sources {
1122
1086
ImportPath : pkg .ImportPath ,
1123
1087
Dir : pkg .Dir ,
1124
- GoFiles : files ,
1088
+ Files : files ,
1125
1089
FileSet : fileSet ,
1126
1090
JSFiles : append (pkg .JSFiles , overlayJsFiles ... ),
1127
1091
}
1128
- s .parsedPackages [pkg .ImportPath ] = parsed
1092
+ s .sources [pkg .ImportPath ] = srcs
1129
1093
1130
1094
// Import dependencies from the augmented files,
1131
1095
// whilst skipping any that have been already imported.
1132
- for _ , importedPkgPath := range parsed . Imports (pkg .Imports ... ) {
1096
+ for _ , importedPkgPath := range srcs . UnresolvedImports (pkg .Imports ... ) {
1133
1097
_ , _ , err := s .loadImportPathWithSrcDir (importedPkgPath , pkg .Dir )
1134
1098
if err != nil {
1135
1099
return nil , err
1136
1100
}
1137
1101
}
1138
1102
1139
- return parsed , nil
1103
+ return srcs , nil
1140
1104
}
1141
1105
1142
- func (s * Session ) compilePackages (pkg * ParsedPackage ) (* compiler.Archive , error ) {
1143
- if archive , ok := s .UpToDateArchives [pkg .ImportPath ]; ok {
1106
+ func (s * Session ) compilePackages (srcs * sources. Sources ) (* compiler.Archive , error ) {
1107
+ if archive , ok := s .UpToDateArchives [srcs .ImportPath ]; ok {
1144
1108
return archive , nil
1145
1109
}
1146
1110
1147
1111
importContext := & compiler.ImportContext {
1148
- Packages : s .Types ,
1149
- Import : s .ImportResolverFor (pkg .Dir ),
1112
+ Packages : s .Types ,
1113
+ ImportArchive : s .ImportResolverFor (srcs .Dir ),
1150
1114
}
1151
- archive , err := compiler .Compile (pkg . ImportPath , pkg . GoFiles , pkg . FileSet , importContext , s .options .Minify )
1115
+ archive , err := compiler .Compile (* srcs , importContext , s .options .Minify )
1152
1116
if err != nil {
1153
1117
return nil , err
1154
1118
}
1155
1119
1156
- for _ , jsFile := range pkg .JSFiles {
1120
+ for _ , jsFile := range srcs .JSFiles {
1157
1121
archive .IncJSCode = append (archive .IncJSCode , []byte ("\t (function() {\n " )... )
1158
1122
archive .IncJSCode = append (archive .IncJSCode , jsFile .Content ... )
1159
1123
archive .IncJSCode = append (archive .IncJSCode , []byte ("\n \t }).call($global);\n " )... )
1160
1124
}
1161
1125
1162
1126
if s .options .Verbose {
1163
- fmt .Println (pkg .ImportPath )
1127
+ fmt .Println (srcs .ImportPath )
1164
1128
}
1165
1129
1166
- s .UpToDateArchives [pkg .ImportPath ] = archive
1130
+ s .UpToDateArchives [srcs .ImportPath ] = archive
1167
1131
1168
1132
return archive , nil
1169
1133
}
@@ -1201,12 +1165,12 @@ func (s *Session) ImportResolverFor(srcDir string) func(string) (*compiler.Archi
1201
1165
return archive , nil
1202
1166
}
1203
1167
1204
- // The archive hasn't been compiled yet so compile it with the parsed package .
1205
- if parsed , ok := s .parsedPackages [importPath ]; ok {
1206
- return s .compilePackages (parsed )
1168
+ // The archive hasn't been compiled yet so compile it with the sources .
1169
+ if srcs , ok := s .sources [importPath ]; ok {
1170
+ return s .compilePackages (srcs )
1207
1171
}
1208
1172
1209
- return nil , fmt .Errorf (`parsed package for %q not found` , importPath )
1173
+ return nil , fmt .Errorf (`sources for %q not found` , importPath )
1210
1174
}
1211
1175
}
1212
1176
0 commit comments