Permalink
Browse files

Add support for source maps that use the local disk path of go source (

  • Loading branch information...
1 parent a49e8ac commit 59875158f8d2a03a3020d62afe84745286acd908 @nullstyle nullstyle committed with neelance Sep 30, 2016
Showing with 26 additions and 12 deletions.
  1. +17 −11 build/build.go
  2. +9 −1 tool.go
View
@@ -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,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})
}
}
View
@@ -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 {

0 comments on commit 5987515

Please sign in to comment.