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},