From 59875158f8d2a03a3020d62afe84745286acd908 Mon Sep 17 00:00:00 2001 From: Scott Fleckenstein Date: Thu, 29 Sep 2016 17:21:26 -0700 Subject: [PATCH] Add support for source maps that use the local disk path of go source (#521) --- build/build.go | 28 +++++++++++++++++----------- tool.go | 10 +++++++++- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/build/build.go b/build/build.go index b8c6a4682..8cce949c5 100644 --- a/build/build.go +++ b/build/build.go @@ -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{}) { @@ -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) { @@ -672,14 +673,18 @@ 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): @@ -687,6 +692,7 @@ func NewMappingCallback(m *sourcemap.Map, goroot, gopath string) func(generatedL default: file = filepath.Base(file) } + m.AddMapping(&sourcemap.Mapping{GeneratedLine: generatedLine, GeneratedColumn: generatedColumn, OriginalFile: file, OriginalLine: originalPos.Line, OriginalColumn: originalPos.Column}) } } diff --git a/tool.go b/tool.go index bdd457273..4be0b375e 100644 --- a/tool.go +++ b/tool.go @@ -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", @@ -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 { @@ -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 { @@ -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{ @@ -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 { @@ -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) { @@ -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 {