Skip to content

Instantly share code, notes, and snippets.

@aymerick
Created September 1, 2015 15:07
Show Gist options
  • Save aymerick/c6de8a69386830828930 to your computer and use it in GitHub Desktop.
Save aymerick/c6de8a69386830828930 to your computer and use it in GitHub Desktop.
go-libsass compilation example
package main
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
libsass "github.com/wellington/go-libsass"
)
func main() {
dir, _ := os.Getwd()
sassFile := path.Join(dir, "sass", "app.scss")
outFile := path.Join(dir, "output", "app.css")
if err := compileFile(sassFile, outFile); err != nil {
panic(err)
}
fmt.Printf("Done: %s\n", outFile)
}
func compileFile(sassFile string, outFile string) error {
if sassFile == "" || outFile == "" {
panic(errors.New("Incorrect parameters"))
}
ctx := libsass.Context{
BuildDir: filepath.Dir(outFile),
MainFile: sassFile,
IncludePaths: []string{filepath.Dir(sassFile)},
}
ctx.Imports.Init()
dir := filepath.Dir(outFile)
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("Failed to create directory: %s", dir)
}
out, err := os.Create(outFile)
if err != nil {
return fmt.Errorf("Failed to create file: %s", outFile)
}
defer out.Close()
if err := ctx.FileCompile(sassFile, out); err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment