Skip to content

Commit

Permalink
Update to use the Dart Sass binary and the new v2 protocol
Browse files Browse the repository at this point in the history
E.g. use `sass --embedded`.

Note taht this will bumb to a new major version of this library (v2).

If you need to support the old protocol/binary, use the v1 (root path) version.

It seems to be twice as fast when running in parallel, but slightly slower serialized:

```bash
name                             old time/op    new time/op    delta
Transpiler/SCSS-10                  114µs ± 0%     145µs ± 1%  +26.40%  (p=0.029 n=4+4)
Transpiler/Start_and_Execute-10    25.6ms ± 2%    26.0ms ± 3%     ~     (p=0.486 n=4+4)
Transpiler/SCSS_Parallel-10        89.0µs ± 2%    44.4µs ± 3%  -50.12%  (p=0.029 n=4+4)

name                             old alloc/op   new alloc/op   delta
Transpiler/SCSS-10                 1.33kB ± 1%    1.33kB ± 0%     ~     (p=1.000 n=4+4)
Transpiler/Start_and_Execute-10    22.4kB ± 1%    22.4kB ± 1%     ~     (p=0.886 n=4+4)
Transpiler/SCSS_Parallel-10        1.34kB ± 3%    1.33kB ± 1%     ~     (p=1.000 n=4+4)

name                             old allocs/op  new allocs/op  delta
Transpiler/SCSS-10                   18.0 ± 0%      18.0 ± 0%     ~     (all equal)
Transpiler/Start_and_Execute-10       144 ± 1%       150 ± 0%   +4.16%  (p=0.029 n=4+4)
Transpiler/SCSS_Parallel-10          18.0 ± 0%      18.0 ± 0%     ~     (all equal)
```

See gohugoio/hugo#11059
Fixes #15
  • Loading branch information
bep committed Jun 8, 2023
1 parent 5d595db commit cdacd4c
Show file tree
Hide file tree
Showing 11 changed files with 680 additions and 725 deletions.
29 changes: 15 additions & 14 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ on:
pull_request:
name: Test
env:
DART_SASS_VERSION: 1.56.1
SASS_VERSION: 1.63.2
jobs:
test:
strategy:
matrix:
go-version: [~1.18, ~1.19]
go-version: [~1.19, ~1.20]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -19,24 +19,25 @@ jobs:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v3
- name: Install dart-sass-embedded Linux
- name: Install dart-sass Linux
if: matrix.os == 'ubuntu-latest'
run: |
curl -LJO "https://github.com/sass/dart-sass-embedded/releases/download/${DART_SASS_VERSION}/sass_embedded-${DART_SASS_VERSION}-linux-x64.tar.gz";
tar -xvf "sass_embedded-${DART_SASS_VERSION}-linux-x64.tar.gz";
echo "DART_SASS_EMBEDDED_BINARY=$GITHUB_WORKSPACE/sass_embedded/dart-sass-embedded" >> $GITHUB_ENV
- name: Install dart-sass-embedded MacOS
curl -LJO "https://github.com/sass/dart-sass/releases/download/${SASS_VERSION}/dart-sass-${SASS_VERSION}-linux-x64.tar.gz";
tar -xvf "dart-sass-${SASS_VERSION}-linux-x64.tar.gz";
echo "DART_SASS_BINARY=$GITHUB_WORKSPACE/dart-sass/sass" >> $GITHUB_ENV
./dart-sass/sass --version
- name: Install dart-sass MacOS
if: matrix.os == 'macos-latest'
run: |
curl -LJO "https://github.com/sass/dart-sass-embedded/releases/download/${DART_SASS_VERSION}/sass_embedded-${DART_SASS_VERSION}-macos-x64.tar.gz";
tar -xvf "sass_embedded-${DART_SASS_VERSION}-macos-x64.tar.gz";
echo "DART_SASS_EMBEDDED_BINARY=$GITHUB_WORKSPACE/sass_embedded/dart-sass-embedded" >> $GITHUB_ENV
- name: Install dart-sass-embedded Windows
curl -LJO "https://github.com/sass/dart-sass/releases/download/${SASS_VERSION}/dart-sass-${SASS_VERSION}-macos-x64.tar.gz";
tar -xvf "dart-sass-${SASS_VERSION}-macos-x64.tar.gz";
echo "DART_SASS_BINARY=$GITHUB_WORKSPACE/dart-sass/sass" >> $GITHUB_ENV
- name: Install dart-sass Windows
if: matrix.os == 'windows-latest'
run: |
curl -LJO "https://github.com/sass/dart-sass-embedded/releases/download/${env:DART_SASS_VERSION}/sass_embedded-${env:DART_SASS_VERSION}-windows-x64.zip";
Expand-Archive -Path "sass_embedded-${env:DART_SASS_VERSION}-windows-x64.zip" -DestinationPath .;
echo "DART_SASS_EMBEDDED_BINARY=$env:GITHUB_WORKSPACE/sass_embedded/dart-sass-embedded.bat" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf-8 -Append
curl -LJO "https://github.com/sass/dart-sass/releases/download/${env:SASS_VERSION}/dart-sass-${env:SASS_VERSION}-windows-x64.zip";
Expand-Archive -Path "dart-sass-${env:SASS_VERSION}-windows-x64.zip" -DestinationPath .;
echo "DART_SASS_BINARY=$env:GITHUB_WORKSPACE/dart-sass/sass.bat" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf-8 -Append
- name: Test
run: go test -race . -coverprofile=coverage.txt -covermode=atomic
- name: Upload coverage
Expand Down
17 changes: 3 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,13 @@
[![codecov](https://codecov.io/gh/bep/godartsass/branch/main/graph/badge.svg?token=OWZ9RCAYWO)](https://codecov.io/gh/bep/godartsass)
[![GoDoc](https://godoc.org/github.com/bep/godartsass?status.svg)](https://godoc.org/github.com/bep/godartsass)

This is a Go API backed by the native [Dart Sass Embedded](https://github.com/sass/dart-sass-embedded) executable.
This is a Go API backed by the native [Dart Sass](https://github.com/sass/dart-sass/releases) executable running with `sass --embedded`.

>**Note:** The `v2.x.x` of this project targets the `v2` of the Dart Sass Embedded protocol with the `sass` exexutable in releases that can be downloaeded [here](https://github.com/sass/dart-sass/releases). For `v1` you need to import `github.com/bep/godartsass` and not `github.com/bep/godartsass/v2`.
The primary motivation for this project is to provide `SCSS` support to [Hugo](https://gohugo.io/). I welcome PRs with bug fixes. I will also consider adding functionality, but please raise an issue discussing it first.

For LibSass bindings in Go, see [GoLibSass](https://github.com/bep/golibsass).

The benchmark below compares [GoLibSass](https://github.com/bep/golibsass) with this library. This is almost twice as fast when running single-threaded, but slower when running with multiple Goroutines. We're communicating with the compiler process via stdin/stdout, which becomes the serialized bottle neck here. That may be possible to improve, but for most practical applications (including Hugo), this should not matter.

```bash
Transpile/SCSS-16 770µs ± 0% 467µs ± 1% -39.36% (p=0.029 n=4+4)
Transpile/SCSS_Parallel-16 92.2µs ± 2% 362.5µs ± 1% +293.39% (p=0.029 n=4+4)

name old alloc/op new alloc/op delta
Transpile/SCSS-16 192B ± 0% 1268B ± 0% +560.42% (p=0.029 n=4+4)
Transpile/SCSS_Parallel-16 192B ± 0% 1272B ± 0% +562.37% (p=0.029 n=4+4)

name old allocs/op new allocs/op delta
Transpile/SCSS-16 2.00 ± 0% 19.00 ± 0% +850.00% (p=0.029 n=4+4)
Transpile/SCSS_Parallel-16 2.00 ± 0% 19.00 ± 0% +850.00% (p=0.029 n=4+4)
```
4 changes: 2 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c conn) Close() error {

var brokenPipeRe = regexp.MustCompile("Broken pipe|pipe is being closed")

// dart-sass-embedded ends on itself on EOF, this is just to give it some
// dart-sass ends on itself on EOF, this is just to give it some
// time to do so.
func (c conn) waitWithTimeout() error {
result := make(chan error, 1)
Expand All @@ -86,7 +86,7 @@ func (c conn) waitWithTimeout() error {
}
return err
case <-time.After(5 * time.Second):
return errors.New("timed out waiting for dart-sass-embedded to finish")
return errors.New("timed out waiting for dart-sass to finish")
}
}

Expand Down
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
module github.com/bep/godartsass
module github.com/bep/godartsass/v2

go 1.15
go 1.19

require (
github.com/cli/safeexec v1.0.0
github.com/frankban/quicktest v1.14.2
google.golang.org/protobuf v1.25.0
)

require (
github.com/google/go-cmp v0.5.7 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.6.1 // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
)
9 changes: 2 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/bep/godartsass v0.16.0 h1:nTpenrZBQjVSjLkCw3AgnYmBB2czauTJa4BLLv448qg=
github.com/bep/godartsass v0.16.0/go.mod h1:6LvK9RftsXMxGfsA0LDV12AGc4Jylnu6NgHL+Q5/pE8=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cli/safeexec v1.0.0 h1:0VngyaIyqACHdcMNWfo6+KdUYnqEr2Sg+bSP1pdF+dI=
github.com/cli/safeexec v1.0.0/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5zx3Q=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/frankban/quicktest v1.11.2 h1:mjwHjStlXWibxOohM7HYieIViKyh56mmt3+6viyhDDI=
github.com/frankban/quicktest v1.11.2/go.mod h1:K+q6oSqb0W0Ininfk863uOk1lMy69l/P6txr3mVT54s=
github.com/frankban/quicktest v1.14.2 h1:SPb1KFFmM+ybpEjPUhCCkZOM5xlovT5UbrMvWnXyBns=
github.com/frankban/quicktest v1.14.2/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand All @@ -26,17 +26,12 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
Expand Down
4 changes: 2 additions & 2 deletions internal/embeddedsass/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

* Install protobuf: https://github.com/protocolbuffers/protobuf
* Install the Go plugin: go get -u google.golang.org/protobuf/cmd/protoc-gen-go
* Download the correct version of the proto file: https://github.com/sass/embedded-protocol/blob/master/embedded_sass.proto
* Install the Go plugin: go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
* Download the correct version of the proto file: https://github.com/sass/sass/blob/main/spec/embedded_sass.proto
* protoc --go_opt=Membedded_sass.proto=github.com/bep/godartsass/internal/embeddedsass --go_opt=paths=source_relative --go_out=. embedded_sass.proto
Loading

0 comments on commit cdacd4c

Please sign in to comment.