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