Skip to content

Commit

Permalink
Make Hugo work with tailwindcss-jit
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Mar 18, 2021
1 parent 24c716c commit 10a68ca
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
6 changes: 6 additions & 0 deletions common/hugo/hugo.go
Expand Up @@ -89,6 +89,12 @@ func GetExecEnviron(workDir string, cfg config.Provider, fs afero.Fs) []string {
if np := os.Getenv("NODE_PATH"); np != "" {
nodepath = workDir + string(os.PathListSeparator) + np
}

// For tailwindcss-jit -- we must make sure that we don't start their watcher
// as that currently will not work.
// See https://github.com/tailwindlabs/tailwindcss-jit#getting-started
config.SetEnvVars(&env, "TAILWIND_MODE", "build")

config.SetEnvVars(&env, "NODE_PATH", nodepath)
config.SetEnvVars(&env, "PWD", workDir)
config.SetEnvVars(&env, "HUGO_ENVIRONMENT", cfg.GetString("environment"))
Expand Down
5 changes: 5 additions & 0 deletions common/loggers/loggers.go
Expand Up @@ -327,3 +327,8 @@ func newLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, logHandle
errors: errorBuff,
}
}

func TimeTrack(start time.Time, name string) {
elapsed := time.Since(start)
fmt.Printf("%s took %s\n", name, elapsed)
}
2 changes: 1 addition & 1 deletion resources/resource_transformers/babel/babel.go
Expand Up @@ -172,7 +172,7 @@ func (t *babelTransformation) Transform(ctx *resources.ResourceTransformationCtx
// Create compile into a real temp file:
// 1. separate stdout/stderr messages from babel (https://github.com/gohugoio/hugo/issues/8136)
// 2. allow generation and retrieval of external source map.
compileOutput, err := ioutil.TempFile("", "compileOut-*.js")
compileOutput, err := ioutil.TempFile("", "babel-out-*.js")
if err != nil {
return err
}
Expand Down
26 changes: 15 additions & 11 deletions resources/resource_transformers/postcss/postcss.go
Expand Up @@ -19,11 +19,13 @@ import (
"encoding/hex"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"

"github.com/cli/safeexec"

Expand Down Expand Up @@ -141,6 +143,7 @@ func (t *postcssTransformation) Key() internal.ResourceTransformationKey {
// npm install -g postcss-cli
// npm install -g autoprefixer
func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationCtx) error {
defer loggers.TimeTrack(time.Now(), "PostCSS")
const localPostCSSPath = "node_modules/.bin/"
const binaryName = "postcss"

Expand Down Expand Up @@ -178,11 +181,18 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
}
}

var cmdArgs []string
// tailwindcss-jit currently does not support reading from stdin.
inFile, err := ioutil.TempFile("", "postcss-in-*.css")
if err != nil {
return err
}
defer os.Remove(inFile.Name())

cmdArgs := []string{inFile.Name()}

if configFile != "" {
logger.Infoln("postcss: use config file", configFile)
cmdArgs = []string{"--config", configFile}
cmdArgs = append(cmdArgs, "--config", configFile)
}

if optArgs := t.options.toArgs(); len(optArgs) > 0 {
Expand All @@ -202,11 +212,6 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC

cmd.Env = hugo.GetExecEnviron(t.rs.WorkingDir, t.rs.Cfg, t.rs.BaseFs.Assets.Fs)

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

src := ctx.From

imp := newImportResolver(
Expand All @@ -223,10 +228,9 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
}
}

go func() {
defer stdin.Close()
io.Copy(stdin, src)
}()
if _, err := io.Copy(inFile, src); err != nil {
return err
}

err = cmd.Run()
if err != nil {
Expand Down

0 comments on commit 10a68ca

Please sign in to comment.