From e584a3c20a1c12e8bfe7f88e55b001f3892520e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 8 Jun 2023 12:37:46 +0200 Subject: [PATCH] Fix import resolver for non-SCSS files Fixes #14 --- conn.go | 2 +- go.sum | 2 -- options.go | 10 +++++++++- transpiler.go | 6 ++++-- transpiler_test.go | 22 +++++++++++++++++----- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/conn.go b/conn.go index 65bf0c5..9143a22 100644 --- a/conn.go +++ b/conn.go @@ -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") } } diff --git a/go.sum b/go.sum index 112c2ef..d7f9409 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/options.go b/options.go index 2e4f1a4..9bed3e0 100644 --- a/options.go +++ b/options.go @@ -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. diff --git a/transpiler.go b/transpiler.go index fdd8cdb..0f14959 100644 --- a/transpiler.go +++ b/transpiler.go @@ -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 @@ -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), }, }, } diff --git a/transpiler_test.go b/transpiler_test.go index f91415e..999cc12 100644 --- a/transpiler_test.go +++ b/transpiler_test.go @@ -36,8 +36,9 @@ const ( ) type testImportResolver struct { - name string - content string + name string + content string + sourceSyntax godartsass.SourceSyntax failOnCanonicalizeURL bool failOnLoad bool @@ -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) { @@ -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 @@ -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},