wellington / go-libsass

Go wrapper for libsass, the only Sass 3.5 compiler for Go
http://godoc.org/github.com/wellington/go-libsass
Apache License 2.0
206 stars 28 forks source link

can't generate source maps #43

Closed danchenkov closed 8 years ago

danchenkov commented 8 years ago

I don't understand why maps are not generated (neither by FileCompile nor Compile) - I get empty output. The code is below, the source maps are enabled in line 40.

package main

import (
    "net/http"
    "os"
    "path"
    "path/filepath"
    "strings"

    libsass "github.com/wellington/go-libsass"
)

type SassHandler struct {
    Root string
}

func (h *SassHandler) ServeHTTP(writer http.ResponseWriter, reader *http.Request) {

    if path.Ext(reader.URL.Path) != ".css" {
        return
    }

    request_file := filepath.Join(h.Root, "assets", reader.URL.Path)
    source_file := strings.TrimSuffix(request_file, ".css")+".scss"

    // save map
    map_file, err := os.Create(request_file + ".map")
    if err != nil {
        return
    }
    defer map_file.Close()

    writer.Header().Set("Content-Type", "text/css")

    comp, err := libsass.New(writer, nil,
        libsass.Path(source_file),
        libsass.IncludePaths([]string{filepath.Join(h.Root, "assets")}),
        // libsass.LineComments(true),
        // libsass.Comments(true),
        libsass.SourceMap(true, map_file),
        // libsass.OutputStyle(1),
    )
    if err != nil {
        log.Printf("ERROR: libsass error: %#v", err.Error())
        http.Error(rw, err.Error(), 500)
        return
    }

    if err = comp.Run(); err != nil {
        log.Printf("ERROR: libsass compile error: %#v", err.Error())
        http.Error(rw, err.Error(), 500)
        return
    }
}
drewwells commented 8 years ago

it appears libSass won't create a source map unless you ask it to embed it into the file or give libSass a path to write one. Your example is using file compiling b/c of the libsass.Path() so no source map is being embedded. The file compiler isn't currently working with sourcemaps. I'll publish a fix.

You can try removing libsass.Path() and passing a reader to libsass.New() to get source maps going in your example.

r, _ := os.Open(source_file)
libsass.new(w,r,...)
drewwells commented 8 years ago

I pushed a breaking change to the SourceMap call. libSass expects a source map embed or a source map file path in order to generate a source map. pull latest master to see the change.

This generates map files with content

package main

import (
    "fmt"
    "log"
    "net/http"
    "os"
    "path"
    "path/filepath"
    "strings"

    libsass "github.com/wellington/go-libsass"
)

func main() {
    wd, _ := os.Getwd()
    http.ListenAndServe(":1111", &SassHandler{Root: wd})
}

type SassHandler struct {
    Root string
}

func (h *SassHandler) ServeHTTP(w http.ResponseWriter, reader *http.Request) {

    if path.Ext(reader.URL.Path) != ".css" {
        return
    }

    request_file := filepath.Join(h.Root, "assets", reader.URL.Path)
    fmt.Println(request_file)
    source_file := strings.TrimSuffix(request_file, ".css") + ".scss"

    rf, _ := os.Open(source_file)
    w.Header().Set("Content-Type", "text/css")
    out, _ := os.Create(request_file)
    comp, err := libsass.New(out, rf,
        libsass.Path(source_file),
        libsass.IncludePaths([]string{filepath.Join(h.Root, "assets")}),
        // libsass.LineComments(true),
        // libsass.Comments(true),
        libsass.SourceMap(true, request_file+".map"),
        // libsass.OutputStyle(1),
    )
    if err != nil {
        log.Printf("ERROR: libsass error: %#v", err.Error())
        http.Error(w, err.Error(), 500)
        return
    }

    if err = comp.Run(); err != nil {
        log.Printf("ERROR: libsass compile error: %#v", err.Error())
        http.Error(w, err.Error(), 500)
        return
    }
}

fixed in b03355aa272f6b2d76fa1179ff872219dad34b81