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

     1  // Copyright 2018 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  //go:build extended
    15  // +build extended
    16  
    17  package scss
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"path"
    23  
    24  	"path/filepath"
    25  	"strings"
    26  
    27  	"github.com/bep/golibsass/libsass"
    28  	"github.com/bep/golibsass/libsass/libsasserrors"
    29  	"github.com/gohugoio/hugo/common/herrors"
    30  	"github.com/gohugoio/hugo/helpers"
    31  	"github.com/gohugoio/hugo/hugofs"
    32  	"github.com/gohugoio/hugo/media"
    33  	"github.com/gohugoio/hugo/resources"
    34  	"github.com/gohugoio/hugo/resources/resource_transformers/tocss/internal/sass"
    35  )
    36  
    37  // Used in tests. This feature requires Hugo to be built with the extended tag.
    38  func Supports() bool {
    39  	return true
    40  }
    41  
    42  func (t *toCSSTransformation) Transform(ctx *resources.ResourceTransformationCtx) error {
    43  	ctx.OutMediaType = media.CSSType
    44  
    45  	var outName string
    46  	if t.options.from.TargetPath != "" {
    47  		ctx.OutPath = t.options.from.TargetPath
    48  	} else {
    49  		ctx.ReplaceOutPathExtension(".css")
    50  	}
    51  
    52  	outName = path.Base(ctx.OutPath)
    53  
    54  	options := t.options
    55  	baseDir := path.Dir(ctx.SourcePath)
    56  	options.to.IncludePaths = t.c.sfs.RealDirs(baseDir)
    57  
    58  	// Append any workDir relative include paths
    59  	for _, ip := range options.from.IncludePaths {
    60  		info, err := t.c.workFs.Stat(filepath.Clean(ip))
    61  		if err == nil {
    62  			filename := info.(hugofs.FileMetaInfo).Meta().Filename
    63  			options.to.IncludePaths = append(options.to.IncludePaths, filename)
    64  		}
    65  	}
    66  
    67  	varsStylesheet := sass.CreateVarsStyleSheet(options.from.Vars)
    68  
    69  	// To allow for overrides of SCSS files anywhere in the project/theme hierarchy, we need
    70  	// to help libsass revolve the filename by looking in the composite filesystem first.
    71  	// We add the entry directories for both project and themes to the include paths list, but
    72  	// that only work for overrides on the top level.
    73  	options.to.ImportResolver = func(url string, prev string) (newUrl string, body string, resolved bool) {
    74  		if url == sass.HugoVarsNamespace {
    75  			return url, varsStylesheet, true
    76  		}
    77  
    78  		// We get URL paths from LibSASS, but we need file paths.
    79  		url = filepath.FromSlash(url)
    80  		prev = filepath.FromSlash(prev)
    81  
    82  		var basePath string
    83  		urlDir := filepath.Dir(url)
    84  		var prevDir string
    85  
    86  		if prev == "stdin" {
    87  			prevDir = baseDir
    88  		} else {
    89  			prevDir, _ = t.c.sfs.MakePathRelative(filepath.Dir(prev))
    90  
    91  			if prevDir == "" {
    92  				// Not a member of this filesystem. Let LibSASS handle it.
    93  				return "", "", false
    94  			}
    95  		}
    96  
    97  		basePath = filepath.Join(prevDir, urlDir)
    98  		name := filepath.Base(url)
    99  
   100  		// Libsass throws an error in cases where you have several possible candidates.
   101  		// We make this simpler and pick the first match.
   102  		var namePatterns []string
   103  		if strings.Contains(name, ".") {
   104  			namePatterns = []string{"_%s", "%s"}
   105  		} else if strings.HasPrefix(name, "_") {
   106  			namePatterns = []string{"_%s.scss", "_%s.sass"}
   107  		} else {
   108  			namePatterns = []string{"_%s.scss", "%s.scss", "_%s.sass", "%s.sass"}
   109  		}
   110  
   111  		name = strings.TrimPrefix(name, "_")
   112  
   113  		for _, namePattern := range namePatterns {
   114  			filenameToCheck := filepath.Join(basePath, fmt.Sprintf(namePattern, name))
   115  			fi, err := t.c.sfs.Fs.Stat(filenameToCheck)
   116  			if err == nil {
   117  				if fim, ok := fi.(hugofs.FileMetaInfo); ok {
   118  					return fim.Meta().Filename, "", true
   119  				}
   120  			}
   121  		}
   122  
   123  		// Not found, let LibSASS handle it
   124  		return "", "", false
   125  	}
   126  
   127  	if ctx.InMediaType.SubType == media.SASSType.SubType {
   128  		options.to.SassSyntax = true
   129  	}
   130  
   131  	if options.from.EnableSourceMap {
   132  
   133  		options.to.SourceMapOptions.Filename = outName + ".map"
   134  		options.to.SourceMapOptions.Root = t.c.rs.WorkingDir
   135  
   136  		// Setting this to the relative input filename will get the source map
   137  		// more correct for the main entry path (main.scss typically), but
   138  		// it will mess up the import mappings. As a workaround, we do a replacement
   139  		// in the source map itself (see below).
   140  		// options.InputPath = inputPath
   141  		options.to.SourceMapOptions.OutputPath = outName
   142  		options.to.SourceMapOptions.Contents = true
   143  		options.to.SourceMapOptions.OmitURL = false
   144  		options.to.SourceMapOptions.EnableEmbedded = false
   145  	}
   146  
   147  	res, err := t.c.toCSS(options.to, ctx.To, ctx.From)
   148  	if err != nil {
   149  		if sasserr, ok := err.(libsasserrors.Error); ok {
   150  			if sasserr.File == "stdin" && ctx.SourcePath != "" {
   151  				sasserr.File = t.c.sfs.RealFilename(ctx.SourcePath)
   152  				err = sasserr
   153  			}
   154  		}
   155  		return herrors.NewFileErrorFromFileInErr(err, hugofs.Os, nil)
   156  
   157  	}
   158  
   159  	if options.from.EnableSourceMap && res.SourceMapContent != "" {
   160  		sourcePath := t.c.sfs.RealFilename(ctx.SourcePath)
   161  
   162  		if strings.HasPrefix(sourcePath, t.c.rs.WorkingDir) {
   163  			sourcePath = strings.TrimPrefix(sourcePath, t.c.rs.WorkingDir+helpers.FilePathSeparator)
   164  		}
   165  
   166  		// This needs to be Unix-style slashes, even on Windows.
   167  		// See https://github.com/gohugoio/hugo/issues/4968
   168  		sourcePath = filepath.ToSlash(sourcePath)
   169  
   170  		// This is a workaround for what looks like a bug in Libsass. But
   171  		// getting this resolution correct in tools like Chrome Workspaces
   172  		// is important enough to go this extra mile.
   173  		mapContent := strings.Replace(res.SourceMapContent, `stdin"`, fmt.Sprintf("%s\"", sourcePath), 1)
   174  
   175  		return ctx.PublishSourceMap(mapContent)
   176  	}
   177  	return nil
   178  }
   179  
   180  func (c *Client) toCSS(options libsass.Options, dst io.Writer, src io.Reader) (libsass.Result, error) {
   181  	var res libsass.Result
   182  
   183  	transpiler, err := libsass.New(options)
   184  	if err != nil {
   185  		return res, err
   186  	}
   187  
   188  	in := helpers.ReaderToString(src)
   189  
   190  	// See https://github.com/gohugoio/hugo/issues/7059
   191  	// We need to preserve the regular CSS imports. This is by far
   192  	// a perfect solution, and only works for the main entry file, but
   193  	// that should cover many use cases, e.g. using SCSS as a preprocessor
   194  	// for Tailwind.
   195  	var importsReplaced bool
   196  	in, importsReplaced = replaceRegularImportsIn(in)
   197  
   198  	res, err = transpiler.Execute(in)
   199  	if err != nil {
   200  		return res, err
   201  	}
   202  
   203  	out := res.CSS
   204  	if importsReplaced {
   205  		out = replaceRegularImportsOut(out)
   206  	}
   207  
   208  	_, err = io.WriteString(dst, out)
   209  
   210  	return res, err
   211  }