@@ -458,36 +458,25 @@ type serveCommandFileSystem struct {
458
458
}
459
459
460
460
func (fs serveCommandFileSystem ) Open (name string ) (http.File , error ) {
461
- for _ , d := range fs .dirs {
462
- file , err := http .Dir (filepath .Join (d , "src" )).Open (name )
463
- if err == nil {
464
- return file , nil
465
- }
466
- }
467
-
468
- dir , _ := path .Split (name )
461
+ dir , file := path .Split (name )
469
462
base := path .Base (dir ) // base is parent folder name, which becomes the output file name.
470
463
471
- if strings .HasSuffix (name , "/" + base + ".js.map" ) {
472
- if content , ok := fs .sourceMaps [name ]; ok {
473
- return newFakeFile (base + ".js.map" , content ), nil
474
- }
475
- }
464
+ isPkg := file == base + ".js"
465
+ isMap := file == base + ".js.map"
466
+ isIndex := file == "index.html"
476
467
477
- isIndex := strings .HasSuffix (name , "/index.html" )
478
- isPkg := strings .HasSuffix (name , "/" + base + ".js" )
479
- if isIndex || isPkg {
468
+ if isPkg || isMap || isIndex {
469
+ // If we're going to be serving our special files, make sure there's a Go command in this folder.
480
470
s := gbuild .NewSession (fs .options )
481
471
buildPkg , err := gbuild .Import (path .Dir (name [1 :]), 0 , s .InstallSuffix (), fs .options .BuildTags )
482
472
if err != nil || buildPkg .Name != "main" {
483
- return nil , os .ErrNotExist
484
- }
485
-
486
- if isIndex {
487
- return newFakeFile ("index.html" , []byte (`<html><head><meta charset="utf-8"><script src="` + base + `.js"></script></head></html>` )), nil
473
+ isPkg = false
474
+ isMap = false
475
+ isIndex = false
488
476
}
489
477
490
- if isPkg {
478
+ switch {
479
+ case isPkg :
491
480
buf := bytes .NewBuffer (nil )
492
481
browserErrors := bytes .NewBuffer (nil )
493
482
exitCode := handleError (func () error {
@@ -519,9 +508,26 @@ func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
519
508
buf = browserErrors
520
509
}
521
510
return newFakeFile (base + ".js" , buf .Bytes ()), nil
511
+
512
+ case isMap :
513
+ if content , ok := fs .sourceMaps [name ]; ok {
514
+ return newFakeFile (base + ".js.map" , content ), nil
515
+ }
522
516
}
523
517
}
524
518
519
+ for _ , d := range fs .dirs {
520
+ f , err := http .Dir (filepath .Join (d , "src" )).Open (name )
521
+ if err == nil {
522
+ return f , nil
523
+ }
524
+ }
525
+
526
+ if isIndex {
527
+ // If there was no index.html file in any dirs, supply our own.
528
+ return newFakeFile ("index.html" , []byte (`<html><head><meta charset="utf-8"><script src="` + base + `.js"></script></head></html>` )), nil
529
+ }
530
+
525
531
return nil , os .ErrNotExist
526
532
}
527
533
0 commit comments