github.com/goplusjs/gopherjs@v1.2.6-0.20211206034512-f187917453b8/compiler/gopherjspkg/fs.go (about)

     1  // +build gopherjsdev
     2  
     3  package gopherjspkg
     4  
     5  import (
     6  	"go/build"
     7  	"log"
     8  	"net/http"
     9  	"os"
    10  	pathpkg "path"
    11  	"path/filepath"
    12  
    13  	"github.com/shurcooL/httpfs/filter"
    14  )
    15  
    16  // FS is a virtual filesystem that contains core GopherJS packages.
    17  var FS = filter.Keep(
    18  	http.Dir(importPathToDir("github.com/goplusjs/gopherjs")),
    19  	func(path string, fi os.FileInfo) bool {
    20  		return path == "/" ||
    21  			path == "/js" || (pathpkg.Dir(path) == "/js" && !fi.IsDir()) ||
    22  			path == "/nosync" || (pathpkg.Dir(path) == "/nosync" && !fi.IsDir())
    23  	},
    24  )
    25  
    26  func importPathToDir(importPath string) string {
    27  	for _, src := range build.Default.SrcDirs() {
    28  		dir := filepath.Join(src, importPath)
    29  		if _, err := os.Stat(dir); err == nil {
    30  			return dir
    31  		}
    32  	}
    33  	p, err := build.Import(importPath, "", build.FindOnly)
    34  	if err != nil {
    35  		log.Fatalln(err)
    36  	}
    37  	return p.Dir
    38  }