Skip to content

Commit

Permalink
Add css.TailwindCSS
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jun 23, 2024
1 parent eed0eff commit 60e19c3
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
123 changes: 123 additions & 0 deletions resources/resource_transformers/postcss/tailwindcss.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2024 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 postcss

import (
"bytes"
"io"
"os"
"path/filepath"

"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/hexec"
"github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/resources"
"github.com/gohugoio/hugo/resources/internal"
"github.com/gohugoio/hugo/resources/resource"
)

// NewTailwindCSSClient creates a new TailwindCSSClient with the given specification.
func NewTailwindCSSClient(rs *resources.Spec) *TailwindCSSClient {
return &TailwindCSSClient{rs: rs}
}

// Client is the client used to do TailwindCSS transformations.
type TailwindCSSClient struct {
rs *resources.Spec
}

// Process transforms the given Resource with the PostCSS processor.
func (c *TailwindCSSClient) Process(res resources.ResourceTransformer, options map[string]any) (resource.Resource, error) {
return res.Transform(&tailwindcssTransformation{rs: c.rs, optionsm: options})
}

type tailwindcssTransformation struct {
optionsm map[string]any
rs *resources.Spec
}

func (t *tailwindcssTransformation) Key() internal.ResourceTransformationKey {
return internal.NewResourceTransformationKey("tailwindcss", t.optionsm)
}

type TailwindCSSOptions struct{}

func (t *tailwindcssTransformation) Transform(ctx *resources.ResourceTransformationCtx) error {
const binaryName = "tailwindcss"

infol := t.rs.Logger.InfoCommand(binaryName)
infow := loggers.LevelLoggerToWriter(infol)

ex := t.rs.ExecHelper

// The tailwindcss CLI does not support streaming, so we need to write to temporary files.
tempDir := os.TempDir()
defer os.RemoveAll(tempDir)

outFilename := filepath.Join(tempDir, "hugo-tailwind-out.css")
workingDir := t.rs.Cfg.BaseConfig().WorkingDir

var cmdArgs []any = []any{
//"--input", inFile.Name(),
"--output", outFilename,
"--cwd", workingDir,
}

var errBuf bytes.Buffer

stderr := io.MultiWriter(infow, &errBuf)
cmdArgs = append(cmdArgs, hexec.WithStderr(stderr))
cmdArgs = append(cmdArgs, hexec.WithEnviron(hugo.GetExecEnviron(workingDir, t.rs.Cfg, t.rs.BaseFs.Assets.Fs)))

cmd, err := ex.Npx(binaryName, cmdArgs...)
if err != nil {
if hexec.IsNotFound(err) {
// This may be on a CI server etc. Will fall back to pre-built assets.
return &herrors.FeatureNotAvailableError{Cause: err}
}
return err
}

stdin, err := cmd.StdinPipe()
if err != nil {
return err
}

src := ctx.From

go func() {
defer stdin.Close()
io.Copy(stdin, src)
}()

err = cmd.Run()
if err != nil {
return err
}

// Copy outFilename to ctx.To
outFile, err := os.Open(outFilename)
if err != nil {
return err
}
defer outFile.Close()

_, err = io.Copy(ctx.To, outFile)
if err != nil {
return err
}

return nil
}
16 changes: 16 additions & 0 deletions tpl/css/css.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Namespace struct {
d *deps.Deps
scssClientLibSass *scss.Client
postcssClient *postcss.Client
tailwindcssClient *postcss.TailwindCSSClient
babelClient *babel.Client

// The Dart Client requires a os/exec process, so only
Expand Down Expand Up @@ -63,6 +64,20 @@ func (ns *Namespace) PostCSS(args ...any) (resource.Resource, error) {
return ns.postcssClient.Process(r, m)
}

// TailwindCSS processes the given Resource with tailwindcss.
func (ns *Namespace) TailwindCSS(args ...any) (resource.Resource, error) {
if len(args) > 2 {
return nil, errors.New("must not provide more arguments than resource object and options")
}

r, m, err := resourcehelpers.ResolveArgs(args)
if err != nil {
return nil, err
}

return ns.tailwindcssClient.Process(r, m)
}

// SASS processes the given Resource with SASS.
func (ns *Namespace) SASS(args ...any) (resource.Resource, error) {
if len(args) > 2 {
Expand Down Expand Up @@ -145,6 +160,7 @@ func init() {
d: d,
scssClientLibSass: scssClient,
postcssClient: postcss.New(d.ResourceSpec),
tailwindcssClient: postcss.NewTailwindCSSClient(d.ResourceSpec),
babelClient: babel.New(d.ResourceSpec),
}

Expand Down

0 comments on commit 60e19c3

Please sign in to comment.