Skip to content

Commit

Permalink
Fix import resolver for non-SCSS files
Browse files Browse the repository at this point in the history
Fixes #14
  • Loading branch information
bep committed Jun 8, 2023
1 parent 3eec1b9 commit e584a3c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (c conn) waitWithTimeout() error {
}
}
return err
case <-time.After(5 * time.Second):
case <-time.After(10 * time.Second):
return errors.New("timed out waiting for dart-sass to finish")
}
}
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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=
Expand Down
10 changes: 9 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ func (opts *Options) init() error {
// Load loads the canonicalized URL's content.
type ImportResolver interface {
CanonicalizeURL(url string) (string, error)
Load(canonicalizedURL string) (string, error)
Load(canonicalizedURL string) (Import, error)
}

type Import struct {
// The content of the imported file.
Content string

// The syntax of the imported file.
SourceSyntax SourceSyntax
}

// Args holds the arguments to Execute.
Expand Down
6 changes: 4 additions & 2 deletions transpiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ func (t *Transpiler) input() {
case *embeddedsass.OutboundMessage_ImportRequest_:
call := t.getCall(compilationID)
url := c.ImportRequest.GetUrl()
contents, loadErr := call.importResolver.Load(url)
imp, loadErr := call.importResolver.Load(url)
sourceSyntax := embeddedsass.Syntax_value[string(imp.SourceSyntax)]

var response *embeddedsass.InboundMessage_ImportResponse
var sourceMapURL string
Expand All @@ -385,8 +386,9 @@ func (t *Transpiler) input() {
Id: c.ImportRequest.GetId(),
Result: &embeddedsass.InboundMessage_ImportResponse_Success{
Success: &embeddedsass.InboundMessage_ImportResponse_ImportSuccess{
Contents: contents,
Contents: imp.Content,
SourceMapUrl: &sourceMapURL,
Syntax: embeddedsass.Syntax(sourceSyntax),
},
},
}
Expand Down
22 changes: 17 additions & 5 deletions transpiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ const (
)

type testImportResolver struct {
name string
content string
name string
content string
sourceSyntax godartsass.SourceSyntax

failOnCanonicalizeURL bool
failOnLoad bool
Expand All @@ -54,14 +55,15 @@ func (t testImportResolver) CanonicalizeURL(url string) (string, error) {
return "file:/my" + t.name + "/scss/" + url + "_myfile.scss", nil
}

func (t testImportResolver) Load(url string) (string, error) {
func (t testImportResolver) Load(url string) (godartsass.Import, error) {
if t.failOnLoad {
return "", errors.New("failed")
return godartsass.Import{}, errors.New("failed")
}
if !strings.Contains(url, t.name) {
panic("protocol error")
}
return t.content, nil
return godartsass.Import{Content: t.content, SourceSyntax: t.sourceSyntax}, nil

}

func TestTranspilerVariants(t *testing.T) {
Expand All @@ -72,6 +74,15 @@ func TestTranspilerVariants(t *testing.T) {
content: `$white: #ffff`,
}

resolverIndented := testImportResolver{
name: "main",
content: `
#main
color: blue
`,
sourceSyntax: godartsass.SourceSyntaxSASS,
}

for _, test := range []struct {
name string
opts godartsass.Options
Expand All @@ -93,6 +104,7 @@ body
SourceSyntax: godartsass.SourceSyntaxSASS,
}, godartsass.Result{CSS: "body{font:100% Helvetica,sans-serif;color:#333}"}},
{"Import resolver with source map", godartsass.Options{}, godartsass.Args{Source: "@import \"colors\";\ndiv { p { color: $white; } }", EnableSourceMap: true, ImportResolver: colorsResolver}, godartsass.Result{CSS: "div p {\n color: white;\n}", SourceMap: "{\"version\":3,\"sourceRoot\":\"\",\"sources\":[\"data:;charset=utf-8,@import%20%22colors%22;%0Adiv%20%7B%20p%20%7B%20color:%20$white;%20%7D%20%7D\",\"file:///mycolors/scss/colors_myfile.scss\"],\"names\":[],\"mappings\":\"AACM;EAAI,OCDC\"}"}},
{"Import resolver with indented source syntax", godartsass.Options{}, godartsass.Args{Source: "@import \"main\";\n", ImportResolver: resolverIndented}, godartsass.Result{CSS: "#main {\n color: blue;\n}"}},

// Error cases
{"Invalid syntax", godartsass.Options{}, godartsass.Args{Source: "div { color: $white; }"}, false},
Expand Down

0 comments on commit e584a3c

Please sign in to comment.