2020-12-23 03:26:23 -05:00
|
|
|
// Copyright 2020 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package dartsass
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2021-12-12 06:11:11 -05:00
|
|
|
"github.com/gohugoio/hugo/common/hexec"
|
2022-05-15 05:40:34 -04:00
|
|
|
"github.com/gohugoio/hugo/common/paths"
|
2020-12-23 03:26:23 -05:00
|
|
|
"github.com/gohugoio/hugo/htesting"
|
|
|
|
"github.com/gohugoio/hugo/media"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/resources"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/resources/internal"
|
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
|
|
|
|
|
|
|
"github.com/bep/godartsass"
|
|
|
|
)
|
|
|
|
|
2021-12-12 06:11:11 -05:00
|
|
|
const (
|
|
|
|
dartSassEmbeddedBinaryName = "dart-sass-embedded"
|
|
|
|
)
|
2020-12-23 03:26:23 -05:00
|
|
|
|
|
|
|
// Supports returns whether dart-sass-embedded is found in $PATH.
|
|
|
|
func Supports() bool {
|
|
|
|
if htesting.SupportsAll() {
|
|
|
|
return true
|
|
|
|
}
|
2021-12-12 06:11:11 -05:00
|
|
|
return hexec.InPath(dartSassEmbeddedBinaryName)
|
2020-12-23 03:26:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type transform struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
optsm map[string]any
|
2020-12-23 03:26:23 -05:00
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *transform) Key() internal.ResourceTransformationKey {
|
|
|
|
return internal.NewResourceTransformationKey(transformationName, t.optsm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *transform) Transform(ctx *resources.ResourceTransformationCtx) error {
|
|
|
|
ctx.OutMediaType = media.CSSType
|
|
|
|
|
|
|
|
opts, err := decodeOptions(t.optsm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.TargetPath != "" {
|
|
|
|
ctx.OutPath = opts.TargetPath
|
|
|
|
} else {
|
|
|
|
ctx.ReplaceOutPathExtension(".css")
|
|
|
|
}
|
|
|
|
|
|
|
|
baseDir := path.Dir(ctx.SourcePath)
|
2022-05-15 05:40:34 -04:00
|
|
|
filename := dartSassStdinPrefix
|
2022-03-17 12:22:34 -04:00
|
|
|
|
|
|
|
if ctx.SourcePath != "" {
|
|
|
|
filename += t.c.sfs.RealFilename(ctx.SourcePath)
|
|
|
|
}
|
2020-12-23 03:26:23 -05:00
|
|
|
|
|
|
|
args := godartsass.Args{
|
2022-03-17 12:22:34 -04:00
|
|
|
URL: filename,
|
2020-12-23 03:26:23 -05:00
|
|
|
IncludePaths: t.c.sfs.RealDirs(baseDir),
|
|
|
|
ImportResolver: importResolver{
|
|
|
|
baseDir: baseDir,
|
|
|
|
c: t.c,
|
|
|
|
},
|
2020-12-31 04:49:39 -05:00
|
|
|
OutputStyle: godartsass.ParseOutputStyle(opts.OutputStyle),
|
2020-12-23 03:26:23 -05:00
|
|
|
EnableSourceMap: opts.EnableSourceMap,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append any workDir relative include paths
|
|
|
|
for _, ip := range opts.IncludePaths {
|
|
|
|
info, err := t.c.workFs.Stat(filepath.Clean(ip))
|
|
|
|
if err == nil {
|
2021-07-13 05:41:02 -04:00
|
|
|
filename := info.(hugofs.FileMetaInfo).Meta().Filename
|
2020-12-23 03:26:23 -05:00
|
|
|
args.IncludePaths = append(args.IncludePaths, filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.InMediaType.SubType == media.SASSType.SubType {
|
|
|
|
args.SourceSyntax = godartsass.SourceSyntaxSASS
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := t.c.toCSS(args, ctx.From)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
out := res.CSS
|
|
|
|
|
|
|
|
_, err = io.WriteString(ctx.To, out)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.EnableSourceMap && res.SourceMap != "" {
|
|
|
|
if err := ctx.PublishSourceMap(res.SourceMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = fmt.Fprintf(ctx.To, "\n\n/*# sourceMappingURL=%s */", path.Base(ctx.OutPath)+".map")
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type importResolver struct {
|
|
|
|
baseDir string
|
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t importResolver) CanonicalizeURL(url string) (string, error) {
|
2022-05-15 05:40:34 -04:00
|
|
|
filePath, isURL := paths.UrlToFilename(url)
|
2020-12-23 03:26:23 -05:00
|
|
|
var prevDir string
|
|
|
|
var pathDir string
|
|
|
|
if isURL {
|
|
|
|
var found bool
|
|
|
|
prevDir, found = t.c.sfs.MakePathRelative(filepath.Dir(filePath))
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
// Not a member of this filesystem, let Dart Sass handle it.
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prevDir = t.baseDir
|
|
|
|
pathDir = path.Dir(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
basePath := filepath.Join(prevDir, pathDir)
|
|
|
|
name := filepath.Base(filePath)
|
|
|
|
|
|
|
|
// Pick the first match.
|
|
|
|
var namePatterns []string
|
|
|
|
if strings.Contains(name, ".") {
|
|
|
|
namePatterns = []string{"_%s", "%s"}
|
|
|
|
} else if strings.HasPrefix(name, "_") {
|
|
|
|
namePatterns = []string{"_%s.scss", "_%s.sass"}
|
|
|
|
} else {
|
|
|
|
namePatterns = []string{"_%s.scss", "%s.scss", "_%s.sass", "%s.sass"}
|
|
|
|
}
|
|
|
|
|
|
|
|
name = strings.TrimPrefix(name, "_")
|
|
|
|
|
|
|
|
for _, namePattern := range namePatterns {
|
|
|
|
filenameToCheck := filepath.Join(basePath, fmt.Sprintf(namePattern, name))
|
|
|
|
fi, err := t.c.sfs.Fs.Stat(filenameToCheck)
|
|
|
|
if err == nil {
|
|
|
|
if fim, ok := fi.(hugofs.FileMetaInfo); ok {
|
2021-07-13 05:41:02 -04:00
|
|
|
return "file://" + filepath.ToSlash(fim.Meta().Filename), nil
|
2020-12-23 03:26:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not found, let Dart Dass handle it
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t importResolver) Load(url string) (string, error) {
|
2022-05-15 05:40:34 -04:00
|
|
|
filename, _ := paths.UrlToFilename(url)
|
2020-12-23 03:26:23 -05:00
|
|
|
b, err := afero.ReadFile(hugofs.Os, filename)
|
|
|
|
return string(b), err
|
|
|
|
}
|