github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/resources/resource_transformers/tocss/dartsass/client.go (about)

     1  // Copyright 2020 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // Package godartsass integrates with the Dass Sass Embedded protocol to transpile
    15  // SCSS/SASS.
    16  package dartsass
    17  
    18  import (
    19  	"fmt"
    20  	"io"
    21  	"strings"
    22  
    23  	"github.com/gohugoio/hugo/common/herrors"
    24  	"github.com/gohugoio/hugo/helpers"
    25  	"github.com/gohugoio/hugo/hugofs"
    26  	"github.com/gohugoio/hugo/hugolib/filesystems"
    27  	"github.com/gohugoio/hugo/resources"
    28  	"github.com/gohugoio/hugo/resources/resource"
    29  	"github.com/spf13/afero"
    30  
    31  	"github.com/bep/godartsass"
    32  	"github.com/mitchellh/mapstructure"
    33  )
    34  
    35  // used as part of the cache key.
    36  const transformationName = "tocss-dart"
    37  
    38  // See https://github.com/sass/dart-sass-embedded/issues/24
    39  // Note: This prefix must be all lower case.
    40  const dartSassStdinPrefix = "hugostdin:"
    41  
    42  func New(fs *filesystems.SourceFilesystem, rs *resources.Spec) (*Client, error) {
    43  	if !Supports() {
    44  		return &Client{dartSassNotAvailable: true}, nil
    45  	}
    46  
    47  	if err := rs.ExecHelper.Sec().CheckAllowedExec(dartSassEmbeddedBinaryName); err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	transpiler, err := godartsass.Start(godartsass.Options{
    52  		LogEventHandler: func(event godartsass.LogEvent) {
    53  			message := strings.ReplaceAll(event.Message, dartSassStdinPrefix, "")
    54  			switch event.Type {
    55  			case godartsass.LogEventTypeDebug:
    56  				// Log as Info for now, we may adjust this if it gets too chatty.
    57  				rs.Logger.Infof("Dart Sass: %s", message)
    58  			default:
    59  				// The rest are either deprecations or @warn statements.
    60  				rs.Logger.Warnf("Dart Sass: %s", message)
    61  			}
    62  		},
    63  	})
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	return &Client{sfs: fs, workFs: rs.BaseFs.Work, rs: rs, transpiler: transpiler}, nil
    68  }
    69  
    70  type Client struct {
    71  	dartSassNotAvailable bool
    72  	rs                   *resources.Spec
    73  	sfs                  *filesystems.SourceFilesystem
    74  	workFs               afero.Fs
    75  	transpiler           *godartsass.Transpiler
    76  }
    77  
    78  func (c *Client) ToCSS(res resources.ResourceTransformer, args map[string]any) (resource.Resource, error) {
    79  	if c.dartSassNotAvailable {
    80  		return res.Transform(resources.NewFeatureNotAvailableTransformer(transformationName, args))
    81  	}
    82  	return res.Transform(&transform{c: c, optsm: args})
    83  }
    84  
    85  func (c *Client) Close() error {
    86  	if c.transpiler == nil {
    87  		return nil
    88  	}
    89  	return c.transpiler.Close()
    90  }
    91  
    92  func (c *Client) toCSS(args godartsass.Args, src io.Reader) (godartsass.Result, error) {
    93  	var res godartsass.Result
    94  
    95  	in := helpers.ReaderToString(src)
    96  
    97  	args.Source = in
    98  
    99  	res, err := c.transpiler.Execute(args)
   100  	if err != nil {
   101  		if err.Error() == "unexpected EOF" {
   102  			return res, fmt.Errorf("got unexpected EOF when executing %q. The user running hugo must have read and execute permissions on this program. With execute permissions only, this error is thrown.", dartSassEmbeddedBinaryName)
   103  		}
   104  		return res, herrors.NewFileErrorFromFileInErr(err, hugofs.Os, herrors.OffsetMatcher)
   105  	}
   106  
   107  	return res, err
   108  }
   109  
   110  type Options struct {
   111  
   112  	// Hugo, will by default, just replace the extension of the source
   113  	// to .css, e.g. "scss/main.scss" becomes "scss/main.css". You can
   114  	// control this by setting this, e.g. "styles/main.css" will create
   115  	// a Resource with that as a base for RelPermalink etc.
   116  	TargetPath string
   117  
   118  	// Hugo automatically adds the entry directories (where the main.scss lives)
   119  	// for project and themes to the list of include paths sent to LibSASS.
   120  	// Any paths set in this setting will be appended. Note that these will be
   121  	// treated as relative to the working dir, i.e. no include paths outside the
   122  	// project/themes.
   123  	IncludePaths []string
   124  
   125  	// Default is nested.
   126  	// One of nested, expanded, compact, compressed.
   127  	OutputStyle string
   128  
   129  	// When enabled, Hugo will generate a source map.
   130  	EnableSourceMap bool
   131  
   132  	// If enabled, sources will be embedded in the generated source map.
   133  	SourceMapIncludeSources bool
   134  
   135  	// Vars will be available in 'hugo:vars', e.g:
   136  	//     @use "hugo:vars";
   137  	//     $color: vars.$color;
   138  	Vars map[string]any
   139  }
   140  
   141  func decodeOptions(m map[string]any) (opts Options, err error) {
   142  	if m == nil {
   143  		return
   144  	}
   145  	err = mapstructure.WeakDecode(m, &opts)
   146  
   147  	if opts.TargetPath != "" {
   148  		opts.TargetPath = helpers.ToSlashTrimLeading(opts.TargetPath)
   149  	}
   150  
   151  	return
   152  }