Navigation Menu

Skip to content

Commit

Permalink
Add support for source maps that use the local disk path of go source (
Browse files Browse the repository at this point in the history
  • Loading branch information
nullstyle authored and neelance committed Sep 30, 2016
1 parent a49e8ac commit 5987515
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
28 changes: 17 additions & 11 deletions build/build.go
Expand Up @@ -322,15 +322,16 @@ func parseAndAugment(pkg *build.Package, isTest bool, fileSet *token.FileSet) ([
}

type Options struct {
GOROOT string
GOPATH string
Verbose bool
Quiet bool
Watch bool
CreateMapFile bool
Minify bool
Color bool
BuildTags []string
GOROOT string
GOPATH string
Verbose bool
Quiet bool
Watch bool
CreateMapFile bool
MapToLocalDisk bool
Minify bool
Color bool
BuildTags []string
}

func (o *Options) PrintError(format string, a ...interface{}) {
Expand Down Expand Up @@ -656,7 +657,7 @@ func (s *Session) WriteCommandPackage(archive *compiler.Archive, pkgObj string)
fmt.Fprintf(codeFile, "//# sourceMappingURL=%s.map\n", filepath.Base(pkgObj))
}()

sourceMapFilter.MappingCallback = NewMappingCallback(m, s.options.GOROOT, s.options.GOPATH)
sourceMapFilter.MappingCallback = NewMappingCallback(m, s.options.GOROOT, s.options.GOPATH, s.options.MapToLocalDisk)
}

deps, err := compiler.ImportDependencies(archive, func(path string) (*compiler.Archive, error) {
Expand All @@ -672,21 +673,26 @@ func (s *Session) WriteCommandPackage(archive *compiler.Archive, pkgObj string)
return compiler.WriteProgramCode(deps, sourceMapFilter)
}

func NewMappingCallback(m *sourcemap.Map, goroot, gopath string) func(generatedLine, generatedColumn int, originalPos token.Position) {
func NewMappingCallback(m *sourcemap.Map, goroot, gopath string, localMap bool) func(generatedLine, generatedColumn int, originalPos token.Position) {
return func(generatedLine, generatedColumn int, originalPos token.Position) {
if !originalPos.IsValid() {
m.AddMapping(&sourcemap.Mapping{GeneratedLine: generatedLine, GeneratedColumn: generatedColumn})
return
}

file := originalPos.Filename

switch hasGopathPrefix, prefixLen := hasGopathPrefix(file, gopath); {
case localMap:
// no-op: keep file as-is
case hasGopathPrefix:
file = filepath.ToSlash(file[prefixLen+4:])
case strings.HasPrefix(file, goroot):
file = filepath.ToSlash(file[len(goroot)+4:])
default:
file = filepath.Base(file)
}

m.AddMapping(&sourcemap.Mapping{GeneratedLine: generatedLine, GeneratedColumn: generatedColumn, OriginalFile: file, OriginalLine: originalPos.Line, OriginalColumn: originalPos.Column})
}
}
Expand Down
10 changes: 9 additions & 1 deletion tool.go
Expand Up @@ -75,6 +75,9 @@ func main() {
tags := pflag.String("tags", "", "a list of build tags to consider satisfied during the build")
flagTags := pflag.Lookup("tags")

pflag.BoolVar(&options.MapToLocalDisk, "localmap", false, "use local paths for sourcemap")
flagLocalMap := pflag.Lookup("localmap")

cmdBuild := &cobra.Command{
Use: "build [packages]",
Short: "compile packages and dependencies",
Expand All @@ -86,6 +89,7 @@ func main() {
cmdBuild.Flags().AddFlag(flagMinify)
cmdBuild.Flags().AddFlag(flagColor)
cmdBuild.Flags().AddFlag(flagTags)
cmdBuild.Flags().AddFlag(flagLocalMap)
cmdBuild.Run = func(cmd *cobra.Command, args []string) {
options.BuildTags = strings.Fields(*tags)
for {
Expand Down Expand Up @@ -162,6 +166,7 @@ func main() {
cmdInstall.Flags().AddFlag(flagMinify)
cmdInstall.Flags().AddFlag(flagColor)
cmdInstall.Flags().AddFlag(flagTags)
cmdInstall.Flags().AddFlag(flagLocalMap)
cmdInstall.Run = func(cmd *cobra.Command, args []string) {
options.BuildTags = strings.Fields(*tags)
for {
Expand Down Expand Up @@ -234,6 +239,7 @@ func main() {
cmdGet.Flags().AddFlag(flagMinify)
cmdGet.Flags().AddFlag(flagColor)
cmdGet.Flags().AddFlag(flagTags)
cmdGet.Flags().AddFlag(flagLocalMap)
cmdGet.Run = cmdInstall.Run

cmdRun := &cobra.Command{
Expand Down Expand Up @@ -289,6 +295,7 @@ func main() {
cmdTest.Flags().AddFlag(flagMinify)
cmdTest.Flags().AddFlag(flagColor)
cmdTest.Flags().AddFlag(flagTags)
cmdTest.Flags().AddFlag(flagLocalMap)
cmdTest.Run = func(cmd *cobra.Command, args []string) {
options.BuildTags = strings.Fields(*tags)
os.Exit(handleError(func() error {
Expand Down Expand Up @@ -475,6 +482,7 @@ func main() {
cmdServe.Flags().AddFlag(flagMinify)
cmdServe.Flags().AddFlag(flagColor)
cmdServe.Flags().AddFlag(flagTags)
cmdServe.Flags().AddFlag(flagLocalMap)
var addr string
cmdServe.Flags().StringVarP(&addr, "http", "", ":8080", "HTTP bind address to serve")
cmdServe.Run = func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -592,7 +600,7 @@ func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) {

sourceMapFilter := &compiler.SourceMapFilter{Writer: buf}
m := &sourcemap.Map{File: base + ".js"}
sourceMapFilter.MappingCallback = gbuild.NewMappingCallback(m, fs.options.GOROOT, fs.options.GOPATH)
sourceMapFilter.MappingCallback = gbuild.NewMappingCallback(m, fs.options.GOROOT, fs.options.GOPATH, fs.options.MapToLocalDisk)

deps, err := compiler.ImportDependencies(archive, s.BuildImportPath)
if err != nil {
Expand Down

0 comments on commit 5987515

Please sign in to comment.