mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
parent
b1106f8715
commit
df298558a5
11 changed files with 362 additions and 118 deletions
|
@ -722,9 +722,6 @@ func (c *commandeer) handleBuildErr(err error, msg string) {
|
||||||
|
|
||||||
func (c *commandeer) rebuildSites(events []fsnotify.Event) error {
|
func (c *commandeer) rebuildSites(events []fsnotify.Event) error {
|
||||||
defer c.timeTrack(time.Now(), "Total")
|
defer c.timeTrack(time.Now(), "Total")
|
||||||
defer func() {
|
|
||||||
c.wasError = false
|
|
||||||
}()
|
|
||||||
|
|
||||||
c.buildErr = nil
|
c.buildErr = nil
|
||||||
visited := c.visitedURLs.PeekAllSet()
|
visited := c.visitedURLs.PeekAllSet()
|
||||||
|
@ -886,6 +883,10 @@ func (c *commandeer) handleEvents(watcher *watcher.Batcher,
|
||||||
evs []fsnotify.Event,
|
evs []fsnotify.Event,
|
||||||
configSet map[string]bool) {
|
configSet map[string]bool) {
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
c.wasError = false
|
||||||
|
}()
|
||||||
|
|
||||||
var isHandled bool
|
var isHandled bool
|
||||||
|
|
||||||
for _, ev := range evs {
|
for _, ev := range evs {
|
||||||
|
@ -1080,10 +1081,11 @@ func (c *commandeer) handleEvents(watcher *watcher.Batcher,
|
||||||
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized
|
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized
|
||||||
|
|
||||||
// force refresh when more than one file
|
// force refresh when more than one file
|
||||||
if len(staticEvents) == 1 {
|
if !c.wasError && len(staticEvents) == 1 {
|
||||||
ev := staticEvents[0]
|
ev := staticEvents[0]
|
||||||
path := c.hugo().BaseFs.SourceFilesystems.MakeStaticPathRelative(ev.Name)
|
path := c.hugo().BaseFs.SourceFilesystems.MakeStaticPathRelative(ev.Name)
|
||||||
path = c.firstPathSpec().RelURL(helpers.ToSlashTrimLeading(path), false)
|
path = c.firstPathSpec().RelURL(helpers.ToSlashTrimLeading(path), false)
|
||||||
|
|
||||||
livereload.RefreshPath(path)
|
livereload.RefreshPath(path)
|
||||||
} else {
|
} else {
|
||||||
livereload.ForceRefresh()
|
livereload.ForceRefresh()
|
||||||
|
@ -1107,6 +1109,10 @@ func (c *commandeer) handleEvents(watcher *watcher.Batcher,
|
||||||
|
|
||||||
if doLiveReload {
|
if doLiveReload {
|
||||||
if len(partitionedEvents.ContentEvents) == 0 && len(partitionedEvents.AssetEvents) > 0 {
|
if len(partitionedEvents.ContentEvents) == 0 && len(partitionedEvents.AssetEvents) > 0 {
|
||||||
|
if c.wasError {
|
||||||
|
livereload.ForceRefresh()
|
||||||
|
return
|
||||||
|
}
|
||||||
changed := c.changeDetector.changed()
|
changed := c.changeDetector.changed()
|
||||||
if c.changeDetector != nil && len(changed) == 0 {
|
if c.changeDetector != nil && len(changed) == 0 {
|
||||||
// Nothing has changed.
|
// Nothing has changed.
|
||||||
|
|
|
@ -340,6 +340,7 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, erro
|
||||||
if err != nil {
|
if err != nil {
|
||||||
f.c.logger.ERROR.Println(err)
|
f.c.logger.ERROR.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
port = 1313
|
port = 1313
|
||||||
if !f.c.paused {
|
if !f.c.paused {
|
||||||
port = f.c.Cfg.GetInt("liveReloadPort")
|
port = f.c.Cfg.GetInt("liveReloadPort")
|
||||||
|
|
|
@ -30,8 +30,7 @@ var buildErrorTemplate = `<!doctype html>
|
||||||
body {
|
body {
|
||||||
font-family: "Muli",avenir, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
|
font-family: "Muli",avenir, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
background-color: black;
|
background-color: #2f1e2e;
|
||||||
color: rgba(255, 255, 255, 0.9);
|
|
||||||
}
|
}
|
||||||
main {
|
main {
|
||||||
margin: auto;
|
margin: auto;
|
||||||
|
@ -43,7 +42,7 @@ var buildErrorTemplate = `<!doctype html>
|
||||||
padding: 1rem 0;
|
padding: 1rem 0;
|
||||||
}
|
}
|
||||||
.stack {
|
.stack {
|
||||||
margin-top: 6rem;
|
margin-top: 4rem;
|
||||||
}
|
}
|
||||||
pre {
|
pre {
|
||||||
white-space: pre-wrap;
|
white-space: pre-wrap;
|
||||||
|
@ -54,10 +53,7 @@ var buildErrorTemplate = `<!doctype html>
|
||||||
}
|
}
|
||||||
.highlight {
|
.highlight {
|
||||||
overflow-x: auto;
|
overflow-x: auto;
|
||||||
padding: 0.75rem;
|
|
||||||
margin-bottom: 1rem;
|
margin-bottom: 1rem;
|
||||||
background-color: #272822;
|
|
||||||
border: 1px solid black;
|
|
||||||
}
|
}
|
||||||
a {
|
a {
|
||||||
color: #0594cb;
|
color: #0594cb;
|
||||||
|
@ -70,14 +66,14 @@ var buildErrorTemplate = `<!doctype html>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<main>
|
<main>
|
||||||
{{ highlight .Error "apl" "noclasses=true,style=monokai" }}
|
{{ highlight .Error "apl" "linenos=false,noclasses=true,style=paraiso-dark" }}
|
||||||
{{ with .File }}
|
{{ with .File }}
|
||||||
{{ $params := printf "noclasses=true,style=monokai,linenos=table,hl_lines=%d,linenostart=%d" (add .LinesPos 1) (sub .Position.LineNumber .LinesPos) }}
|
{{ $params := printf "noclasses=true,style=paraiso-dark,linenos=table,hl_lines=%d,linenostart=%d" (add .LinesPos 1) (sub .Position.LineNumber .LinesPos) }}
|
||||||
{{ $lexer := .ChromaLexer | default "go-html-template" }}
|
{{ $lexer := .ChromaLexer | default "go-html-template" }}
|
||||||
{{ highlight (delimit .Lines "\n") $lexer $params }}
|
{{ highlight (delimit .Lines "\n") $lexer $params }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ with .StackTrace }}
|
{{ with .StackTrace }}
|
||||||
{{ highlight . "apl" "noclasses=true,style=monokai" }}
|
{{ highlight . "apl" "noclasses=true,style=paraiso-dark" }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
<p class="version">{{ .Version }}</p>
|
<p class="version">{{ .Version }}</p>
|
||||||
<a href="">Reload Page</a>
|
<a href="">Reload Page</a>
|
||||||
|
|
8
deps/deps.go
vendored
8
deps/deps.go
vendored
|
@ -235,7 +235,9 @@ func New(cfg DepsCfg) (*Deps, error) {
|
||||||
return nil, errors.WithMessage(err, "failed to create file caches from configuration")
|
return nil, errors.WithMessage(err, "failed to create file caches from configuration")
|
||||||
}
|
}
|
||||||
|
|
||||||
resourceSpec, err := resources.NewSpec(ps, fileCaches, logger, cfg.OutputFormats, cfg.MediaTypes)
|
errorHandler := &globalErrHandler{}
|
||||||
|
|
||||||
|
resourceSpec, err := resources.NewSpec(ps, fileCaches, logger, errorHandler, cfg.OutputFormats, cfg.MediaTypes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -275,7 +277,7 @@ func New(cfg DepsCfg) (*Deps, error) {
|
||||||
BuildStartListeners: &Listeners{},
|
BuildStartListeners: &Listeners{},
|
||||||
BuildFlags: &BuildFlags{},
|
BuildFlags: &BuildFlags{},
|
||||||
Timeout: time.Duration(timeoutms) * time.Millisecond,
|
Timeout: time.Duration(timeoutms) * time.Millisecond,
|
||||||
globalErrHandler: &globalErrHandler{},
|
globalErrHandler: errorHandler,
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Cfg.GetBool("templateMetrics") {
|
if cfg.Cfg.GetBool("templateMetrics") {
|
||||||
|
@ -306,7 +308,7 @@ func (d Deps) ForLanguage(cfg DepsCfg, onCreated func(d *Deps) error) (*Deps, er
|
||||||
// The resource cache is global so reuse.
|
// The resource cache is global so reuse.
|
||||||
// TODO(bep) clean up these inits.
|
// TODO(bep) clean up these inits.
|
||||||
resourceCache := d.ResourceSpec.ResourceCache
|
resourceCache := d.ResourceSpec.ResourceCache
|
||||||
d.ResourceSpec, err = resources.NewSpec(d.PathSpec, d.ResourceSpec.FileCaches, d.Log, cfg.OutputFormats, cfg.MediaTypes)
|
d.ResourceSpec, err = resources.NewSpec(d.PathSpec, d.ResourceSpec.FileCaches, d.Log, d.globalErrHandler, cfg.OutputFormats, cfg.MediaTypes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gohugoio/hugo/common/herrors"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/htesting"
|
"github.com/gohugoio/hugo/htesting"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
@ -717,11 +719,10 @@ func TestResourceChainPostCSS(t *testing.T) {
|
||||||
|
|
||||||
packageJSON := `{
|
packageJSON := `{
|
||||||
"scripts": {},
|
"scripts": {},
|
||||||
"dependencies": {
|
|
||||||
"tailwindcss": "^1.2"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"postcss-cli": "^7.1.0"
|
"postcss-cli": "^7.1.0",
|
||||||
|
"tailwindcss": "^1.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
@ -826,7 +827,7 @@ Styles Content: Len: 770878|
|
||||||
|
|
||||||
assertCss(b)
|
assertCss(b)
|
||||||
|
|
||||||
build := func(s string, shouldFail bool) {
|
build := func(s string, shouldFail bool) error {
|
||||||
b.Assert(os.RemoveAll(filepath.Join(workDir, "public")), qt.IsNil)
|
b.Assert(os.RemoveAll(filepath.Join(workDir, "public")), qt.IsNil)
|
||||||
|
|
||||||
v := viper.New()
|
v := viper.New()
|
||||||
|
@ -837,19 +838,37 @@ Styles Content: Len: 770878|
|
||||||
b = newTestBuilder(v)
|
b = newTestBuilder(v)
|
||||||
|
|
||||||
b.Assert(os.RemoveAll(filepath.Join(workDir, "public")), qt.IsNil)
|
b.Assert(os.RemoveAll(filepath.Join(workDir, "public")), qt.IsNil)
|
||||||
b.Assert(os.RemoveAll(filepath.Join(workDir, "node_modules")), qt.IsNil)
|
|
||||||
|
|
||||||
|
err := b.BuildE(BuildCfg{})
|
||||||
if shouldFail {
|
if shouldFail {
|
||||||
b.BuildFail(BuildCfg{})
|
b.Assert(err, qt.Not(qt.IsNil))
|
||||||
} else {
|
} else {
|
||||||
b.Build(BuildCfg{})
|
b.Assert(err, qt.IsNil)
|
||||||
assertCss(b)
|
assertCss(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
build("always", false)
|
build("always", false)
|
||||||
build("fallback", false)
|
build("fallback", false)
|
||||||
|
|
||||||
|
// Introduce a syntax error in an import
|
||||||
|
b.WithSourceFile("assets/css/components/b.css", `@import "a.css";
|
||||||
|
|
||||||
|
class-in-b {
|
||||||
|
@apply asdf;
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
|
||||||
|
err = build("newer", true)
|
||||||
|
|
||||||
|
err = herrors.UnwrapErrorWithFileContext(err)
|
||||||
|
fe, ok := err.(*herrors.ErrorWithFileContext)
|
||||||
|
b.Assert(ok, qt.Equals, true)
|
||||||
|
b.Assert(fe.Position().LineNumber, qt.Equals, 4)
|
||||||
|
b.Assert(fe.Error(), qt.Contains, filepath.Join(workDir, "assets/css/components/b.css:4:1"))
|
||||||
|
|
||||||
// Remove PostCSS
|
// Remove PostCSS
|
||||||
b.Assert(os.RemoveAll(filepath.Join(workDir, "node_modules")), qt.IsNil)
|
b.Assert(os.RemoveAll(filepath.Join(workDir, "node_modules")), qt.IsNil)
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,8 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gohugoio/hugo/common/herrors"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/config"
|
"github.com/gohugoio/hugo/config"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/hugofs"
|
"github.com/gohugoio/hugo/hugofs"
|
||||||
|
@ -43,6 +45,7 @@ func NewSpec(
|
||||||
s *helpers.PathSpec,
|
s *helpers.PathSpec,
|
||||||
fileCaches filecache.Caches,
|
fileCaches filecache.Caches,
|
||||||
logger *loggers.Logger,
|
logger *loggers.Logger,
|
||||||
|
errorHandler herrors.ErrorSender,
|
||||||
outputFormats output.Formats,
|
outputFormats output.Formats,
|
||||||
mimeTypes media.Types) (*Spec, error) {
|
mimeTypes media.Types) (*Spec, error) {
|
||||||
|
|
||||||
|
@ -67,6 +70,7 @@ func NewSpec(
|
||||||
|
|
||||||
rs := &Spec{PathSpec: s,
|
rs := &Spec{PathSpec: s,
|
||||||
Logger: logger,
|
Logger: logger,
|
||||||
|
ErrorSender: errorHandler,
|
||||||
imaging: imaging,
|
imaging: imaging,
|
||||||
MediaTypes: mimeTypes,
|
MediaTypes: mimeTypes,
|
||||||
OutputFormats: outputFormats,
|
OutputFormats: outputFormats,
|
||||||
|
@ -92,6 +96,7 @@ type Spec struct {
|
||||||
OutputFormats output.Formats
|
OutputFormats output.Formats
|
||||||
|
|
||||||
Logger *loggers.Logger
|
Logger *loggers.Logger
|
||||||
|
ErrorSender herrors.ErrorSender
|
||||||
|
|
||||||
TextTemplates tpl.TemplateParseFinder
|
TextTemplates tpl.TemplateParseFinder
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ func NewTestResourceSpec() (*resources.Spec, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
spec, err := resources.NewSpec(s, filecaches, nil, output.DefaultFormats, media.DefaultTypes)
|
spec, err := resources.NewSpec(s, filecaches, nil, nil, output.DefaultFormats, media.DefaultTypes)
|
||||||
return spec, err
|
return spec, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
package postcss
|
package postcss
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"io"
|
"io"
|
||||||
|
@ -21,13 +22,15 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gohugoio/hugo/common/loggers"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/config"
|
"github.com/gohugoio/hugo/config"
|
||||||
|
|
||||||
"github.com/spf13/afero"
|
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/resources/internal"
|
"github.com/gohugoio/hugo/resources/internal"
|
||||||
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/hugofs"
|
"github.com/gohugoio/hugo/hugofs"
|
||||||
|
@ -45,6 +48,41 @@ import (
|
||||||
|
|
||||||
const importIdentifier = "@import"
|
const importIdentifier = "@import"
|
||||||
|
|
||||||
|
var cssSyntaxErrorRe = regexp.MustCompile(`> (\d+) \|`)
|
||||||
|
|
||||||
|
var shouldImportRe = regexp.MustCompile(`^@import ["'].*["'];?\s*(/\*.*\*/)?$`)
|
||||||
|
|
||||||
|
// New creates a new Client with the given specification.
|
||||||
|
func New(rs *resources.Spec) *Client {
|
||||||
|
return &Client{rs: rs}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DecodeOptions(m map[string]interface{}) (opts Options, err error) {
|
||||||
|
if m == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = mapstructure.WeakDecode(m, &opts)
|
||||||
|
|
||||||
|
if !opts.NoMap {
|
||||||
|
// There was for a long time a discrepancy between documentation and
|
||||||
|
// implementation for the noMap property, so we need to support both
|
||||||
|
// camel and snake case.
|
||||||
|
opts.NoMap = cast.ToBool(m["no-map"])
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Client is the client used to do PostCSS transformations.
|
||||||
|
type Client struct {
|
||||||
|
rs *resources.Spec
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process transforms the given Resource with the PostCSS processor.
|
||||||
|
func (c *Client) Process(res resources.ResourceTransformer, options Options) (resource.Resource, error) {
|
||||||
|
return res.Transform(&postcssTransformation{rs: c.rs, options: options})
|
||||||
|
}
|
||||||
|
|
||||||
// Some of the options from https://github.com/postcss/postcss-cli
|
// Some of the options from https://github.com/postcss/postcss-cli
|
||||||
type Options struct {
|
type Options struct {
|
||||||
|
|
||||||
|
@ -68,22 +106,6 @@ type Options struct {
|
||||||
Syntax string // Custom postcss syntax
|
Syntax string // Custom postcss syntax
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeOptions(m map[string]interface{}) (opts Options, err error) {
|
|
||||||
if m == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = mapstructure.WeakDecode(m, &opts)
|
|
||||||
|
|
||||||
if !opts.NoMap {
|
|
||||||
// There was for a long time a discrepancy between documentation and
|
|
||||||
// implementation for the noMap property, so we need to support both
|
|
||||||
// camel and snake case.
|
|
||||||
opts.NoMap = cast.ToBool(m["no-map"])
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (opts Options) toArgs() []string {
|
func (opts Options) toArgs() []string {
|
||||||
var args []string
|
var args []string
|
||||||
if opts.NoMap {
|
if opts.NoMap {
|
||||||
|
@ -104,16 +126,6 @@ func (opts Options) toArgs() []string {
|
||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client is the client used to do PostCSS transformations.
|
|
||||||
type Client struct {
|
|
||||||
rs *resources.Spec
|
|
||||||
}
|
|
||||||
|
|
||||||
// New creates a new Client with the given specification.
|
|
||||||
func New(rs *resources.Spec) *Client {
|
|
||||||
return &Client{rs: rs}
|
|
||||||
}
|
|
||||||
|
|
||||||
type postcssTransformation struct {
|
type postcssTransformation struct {
|
||||||
options Options
|
options Options
|
||||||
rs *resources.Spec
|
rs *resources.Spec
|
||||||
|
@ -186,8 +198,10 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
|
||||||
|
|
||||||
cmd := exec.Command(binary, cmdArgs...)
|
cmd := exec.Command(binary, cmdArgs...)
|
||||||
|
|
||||||
|
var errBuf bytes.Buffer
|
||||||
|
|
||||||
cmd.Stdout = ctx.To
|
cmd.Stdout = ctx.To
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = io.MultiWriter(os.Stderr, &errBuf)
|
||||||
// TODO(bep) somehow generalize this to other external helpers that may need this.
|
// TODO(bep) somehow generalize this to other external helpers that may need this.
|
||||||
env := os.Environ()
|
env := os.Environ()
|
||||||
config.SetEnvVars(&env, "HUGO_ENVIRONMENT", t.rs.Cfg.GetString("environment"))
|
config.SetEnvVars(&env, "HUGO_ENVIRONMENT", t.rs.Cfg.GetString("environment"))
|
||||||
|
@ -199,9 +213,16 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
|
||||||
}
|
}
|
||||||
|
|
||||||
src := ctx.From
|
src := ctx.From
|
||||||
|
|
||||||
|
imp := newImportResolver(
|
||||||
|
ctx.From,
|
||||||
|
ctx.InPath,
|
||||||
|
t.rs.Assets.Fs, t.rs.Logger,
|
||||||
|
)
|
||||||
|
|
||||||
if t.options.InlineImports {
|
if t.options.InlineImports {
|
||||||
var err error
|
var err error
|
||||||
src, err = t.inlineImports(ctx)
|
src, err = imp.resolve()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -214,69 +235,99 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
|
||||||
|
|
||||||
err = cmd.Run()
|
err = cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return imp.toFileError(errBuf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *postcssTransformation) inlineImports(ctx *resources.ResourceTransformationCtx) (io.Reader, error) {
|
type fileOffset struct {
|
||||||
|
Filename string
|
||||||
|
Offset int
|
||||||
|
}
|
||||||
|
|
||||||
const importIdentifier = "@import"
|
type importResolver struct {
|
||||||
|
r io.Reader
|
||||||
|
inPath string
|
||||||
|
|
||||||
// Set of content hashes.
|
contentSeen map[string]bool
|
||||||
contentSeen := make(map[string]bool)
|
linemap map[int]fileOffset
|
||||||
|
fs afero.Fs
|
||||||
|
logger *loggers.Logger
|
||||||
|
}
|
||||||
|
|
||||||
content, err := ioutil.ReadAll(ctx.From)
|
func newImportResolver(r io.Reader, inPath string, fs afero.Fs, logger *loggers.Logger) *importResolver {
|
||||||
|
return &importResolver{
|
||||||
|
r: r,
|
||||||
|
inPath: inPath,
|
||||||
|
fs: fs, logger: logger,
|
||||||
|
linemap: make(map[int]fileOffset), contentSeen: make(map[string]bool),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (imp *importResolver) contentHash(filename string) ([]byte, string) {
|
||||||
|
b, err := afero.ReadFile(imp.fs, filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, ""
|
||||||
|
}
|
||||||
|
h := sha256.New()
|
||||||
|
h.Write(b)
|
||||||
|
return b, hex.EncodeToString(h.Sum(nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
contents := string(content)
|
func (imp *importResolver) importRecursive(
|
||||||
|
lineNum int,
|
||||||
newContent, err := t.importRecursive(contentSeen, contents, ctx.InPath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.NewReader(newContent), nil
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *postcssTransformation) importRecursive(
|
|
||||||
contentSeen map[string]bool,
|
|
||||||
content string,
|
content string,
|
||||||
inPath string) (string, error) {
|
inPath string) (int, string, error) {
|
||||||
|
|
||||||
basePath := path.Dir(inPath)
|
basePath := path.Dir(inPath)
|
||||||
|
|
||||||
var replacements []string
|
var replacements []string
|
||||||
lines := strings.Split(content, "\n")
|
lines := strings.Split(content, "\n")
|
||||||
|
|
||||||
for _, line := range lines {
|
trackLine := func(i, offset int, line string) {
|
||||||
|
// TODO(bep) this is not very efficient.
|
||||||
|
imp.linemap[i+lineNum] = fileOffset{Filename: inPath, Offset: offset}
|
||||||
|
}
|
||||||
|
|
||||||
|
i := 0
|
||||||
|
for offset, line := range lines {
|
||||||
|
i++
|
||||||
line = strings.TrimSpace(line)
|
line = strings.TrimSpace(line)
|
||||||
if shouldImport(line) {
|
|
||||||
|
if !imp.shouldImport(line) {
|
||||||
|
trackLine(i, offset, line)
|
||||||
|
} else {
|
||||||
|
i--
|
||||||
path := strings.Trim(strings.TrimPrefix(line, importIdentifier), " \"';")
|
path := strings.Trim(strings.TrimPrefix(line, importIdentifier), " \"';")
|
||||||
filename := filepath.Join(basePath, path)
|
filename := filepath.Join(basePath, path)
|
||||||
importContent, hash := t.contentHash(filename)
|
importContent, hash := imp.contentHash(filename)
|
||||||
if importContent == nil {
|
if importContent == nil {
|
||||||
t.rs.Logger.WARN.Printf("postcss: Failed to resolve CSS @import in %q for path %q", inPath, filename)
|
trackLine(i, offset, "ERROR")
|
||||||
|
imp.logger.WARN.Printf("postcss: Failed to resolve CSS @import in %q for path %q", inPath, filename)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if contentSeen[hash] {
|
if imp.contentSeen[hash] {
|
||||||
|
i++
|
||||||
// Just replace the line with an empty string.
|
// Just replace the line with an empty string.
|
||||||
replacements = append(replacements, []string{line, ""}...)
|
replacements = append(replacements, []string{line, ""}...)
|
||||||
|
trackLine(i, offset, "IMPORT")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
contentSeen[hash] = true
|
imp.contentSeen[hash] = true
|
||||||
|
|
||||||
// Handle recursive imports.
|
// Handle recursive imports.
|
||||||
nested, err := t.importRecursive(contentSeen, string(importContent), filepath.ToSlash(filename))
|
l, nested, err := imp.importRecursive(i+lineNum, string(importContent), filepath.ToSlash(filename))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return 0, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trackLine(i, offset, line)
|
||||||
|
|
||||||
|
i += l
|
||||||
|
|
||||||
importContent = []byte(nested)
|
importContent = []byte(nested)
|
||||||
|
|
||||||
replacements = append(replacements, []string{line, string(importContent)}...)
|
replacements = append(replacements, []string{line, string(importContent)}...)
|
||||||
|
@ -288,25 +339,27 @@ func (t *postcssTransformation) importRecursive(
|
||||||
content = repl.Replace(content)
|
content = repl.Replace(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
return content, nil
|
return i, content, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *postcssTransformation) contentHash(filename string) ([]byte, string) {
|
func (imp *importResolver) resolve() (io.Reader, error) {
|
||||||
b, err := afero.ReadFile(t.rs.Assets.Fs, filename)
|
const importIdentifier = "@import"
|
||||||
|
|
||||||
|
content, err := ioutil.ReadAll(imp.r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ""
|
return nil, err
|
||||||
}
|
|
||||||
h := sha256.New()
|
|
||||||
h.Write(b)
|
|
||||||
return b, hex.EncodeToString(h.Sum(nil))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process transforms the given Resource with the PostCSS processor.
|
contents := string(content)
|
||||||
func (c *Client) Process(res resources.ResourceTransformer, options Options) (resource.Resource, error) {
|
|
||||||
return res.Transform(&postcssTransformation{rs: c.rs, options: options})
|
_, newContent, err := imp.importRecursive(0, contents, imp.inPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var shouldImportRe = regexp.MustCompile(`^@import ["'].*["'];?\s*(/\*.*\*/)?$`)
|
return strings.NewReader(newContent), nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// See https://www.w3schools.com/cssref/pr_import_rule.asp
|
// See https://www.w3schools.com/cssref/pr_import_rule.asp
|
||||||
// We currently only support simple file imports, no urls, no media queries.
|
// We currently only support simple file imports, no urls, no media queries.
|
||||||
|
@ -315,7 +368,7 @@ var shouldImportRe = regexp.MustCompile(`^@import ["'].*["'];?\s*(/\*.*\*/)?$`)
|
||||||
// This is not:
|
// This is not:
|
||||||
// @import url("navigation.css");
|
// @import url("navigation.css");
|
||||||
// @import "mobstyle.css" screen and (max-width: 768px);
|
// @import "mobstyle.css" screen and (max-width: 768px);
|
||||||
func shouldImport(s string) bool {
|
func (imp *importResolver) shouldImport(s string) bool {
|
||||||
if !strings.HasPrefix(s, importIdentifier) {
|
if !strings.HasPrefix(s, importIdentifier) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -325,3 +378,38 @@ func shouldImport(s string) bool {
|
||||||
|
|
||||||
return shouldImportRe.MatchString(s)
|
return shouldImportRe.MatchString(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (imp *importResolver) toFileError(output string) error {
|
||||||
|
inErr := errors.New(strings.TrimSpace(output))
|
||||||
|
|
||||||
|
match := cssSyntaxErrorRe.FindStringSubmatch(output)
|
||||||
|
if match == nil {
|
||||||
|
return inErr
|
||||||
|
}
|
||||||
|
|
||||||
|
lineNum, err := strconv.Atoi(match[1])
|
||||||
|
if err != nil {
|
||||||
|
return inErr
|
||||||
|
}
|
||||||
|
|
||||||
|
file, ok := imp.linemap[lineNum]
|
||||||
|
if !ok {
|
||||||
|
return inErr
|
||||||
|
}
|
||||||
|
|
||||||
|
fi, err := imp.fs.Stat(file.Filename)
|
||||||
|
if err != nil {
|
||||||
|
return inErr
|
||||||
|
}
|
||||||
|
realFilename := fi.(hugofs.FileMetaInfo).Meta().Filename()
|
||||||
|
|
||||||
|
ferr := herrors.NewFileError("css", -1, file.Offset+1, 1, inErr)
|
||||||
|
|
||||||
|
werr, ok := herrors.WithFileContextForFile(ferr, realFilename, file.Filename, imp.fs, herrors.SimpleLineMatcher)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return ferr
|
||||||
|
}
|
||||||
|
|
||||||
|
return werr
|
||||||
|
}
|
||||||
|
|
|
@ -14,8 +14,17 @@
|
||||||
package postcss
|
package postcss
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gohugoio/hugo/htesting/hqt"
|
||||||
|
|
||||||
|
"github.com/gohugoio/hugo/common/loggers"
|
||||||
|
"github.com/gohugoio/hugo/helpers"
|
||||||
|
|
||||||
|
"github.com/spf13/afero"
|
||||||
|
|
||||||
qt "github.com/frankban/quicktest"
|
qt "github.com/frankban/quicktest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -40,6 +49,7 @@ func TestDecodeOptions(t *testing.T) {
|
||||||
|
|
||||||
func TestShouldImport(t *testing.T) {
|
func TestShouldImport(t *testing.T) {
|
||||||
c := qt.New(t)
|
c := qt.New(t)
|
||||||
|
var imp *importResolver
|
||||||
|
|
||||||
for _, test := range []struct {
|
for _, test := range []struct {
|
||||||
input string
|
input string
|
||||||
|
@ -52,6 +62,106 @@ func TestShouldImport(t *testing.T) {
|
||||||
{input: `@import url("navigation.css");`, expect: false},
|
{input: `@import url("navigation.css");`, expect: false},
|
||||||
{input: `@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,400i,800,800i&display=swap');`, expect: false},
|
{input: `@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,400i,800,800i&display=swap');`, expect: false},
|
||||||
} {
|
} {
|
||||||
c.Assert(shouldImport(test.input), qt.Equals, test.expect)
|
c.Assert(imp.shouldImport(test.input), qt.Equals, test.expect)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestImportResolver(t *testing.T) {
|
||||||
|
c := qt.New(t)
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
|
||||||
|
writeFile := func(name, content string) {
|
||||||
|
c.Assert(afero.WriteFile(fs, name, []byte(content), 0777), qt.IsNil)
|
||||||
|
}
|
||||||
|
|
||||||
|
writeFile("a.css", `@import "b.css";
|
||||||
|
@import "c.css";
|
||||||
|
A_STYLE1
|
||||||
|
A_STYLE2
|
||||||
|
`)
|
||||||
|
|
||||||
|
writeFile("b.css", `B_STYLE`)
|
||||||
|
writeFile("c.css", "@import \"d.css\"\nC_STYLE")
|
||||||
|
writeFile("d.css", "@import \"a.css\"\n\nD_STYLE")
|
||||||
|
writeFile("e.css", "E_STYLE")
|
||||||
|
|
||||||
|
mainStyles := strings.NewReader(`@import "a.css";
|
||||||
|
@import "b.css";
|
||||||
|
LOCAL_STYLE
|
||||||
|
@import "c.css";
|
||||||
|
@import "e.css";
|
||||||
|
@import "missing.css";`)
|
||||||
|
|
||||||
|
imp := newImportResolver(
|
||||||
|
mainStyles,
|
||||||
|
"styles.css",
|
||||||
|
fs, loggers.NewErrorLogger(),
|
||||||
|
)
|
||||||
|
|
||||||
|
r, err := imp.resolve()
|
||||||
|
c.Assert(err, qt.IsNil)
|
||||||
|
rs := helpers.ReaderToString(r)
|
||||||
|
result := regexp.MustCompile(`\n+`).ReplaceAllString(rs, "\n")
|
||||||
|
|
||||||
|
c.Assert(result, hqt.IsSameString, `B_STYLE
|
||||||
|
D_STYLE
|
||||||
|
C_STYLE
|
||||||
|
A_STYLE1
|
||||||
|
A_STYLE2
|
||||||
|
LOCAL_STYLE
|
||||||
|
E_STYLE
|
||||||
|
@import "missing.css";`)
|
||||||
|
|
||||||
|
dline := imp.linemap[3]
|
||||||
|
c.Assert(dline, qt.DeepEquals, fileOffset{
|
||||||
|
Offset: 1,
|
||||||
|
Filename: "d.css",
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkImportResolver(b *testing.B) {
|
||||||
|
c := qt.New(b)
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
|
||||||
|
writeFile := func(name, content string) {
|
||||||
|
c.Assert(afero.WriteFile(fs, name, []byte(content), 0777), qt.IsNil)
|
||||||
|
}
|
||||||
|
|
||||||
|
writeFile("a.css", `@import "b.css";
|
||||||
|
@import "c.css";
|
||||||
|
A_STYLE1
|
||||||
|
A_STYLE2
|
||||||
|
`)
|
||||||
|
|
||||||
|
writeFile("b.css", `B_STYLE`)
|
||||||
|
writeFile("c.css", "@import \"d.css\"\nC_STYLE"+strings.Repeat("\nSTYLE", 12))
|
||||||
|
writeFile("d.css", "@import \"a.css\"\n\nD_STYLE"+strings.Repeat("\nSTYLE", 55))
|
||||||
|
writeFile("e.css", "E_STYLE")
|
||||||
|
|
||||||
|
mainStyles := `@import "a.css";
|
||||||
|
@import "b.css";
|
||||||
|
LOCAL_STYLE
|
||||||
|
@import "c.css";
|
||||||
|
@import "e.css";
|
||||||
|
@import "missing.css";`
|
||||||
|
|
||||||
|
logger := loggers.NewErrorLogger()
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
b.StopTimer()
|
||||||
|
imp := newImportResolver(
|
||||||
|
strings.NewReader(mainStyles),
|
||||||
|
"styles.css",
|
||||||
|
fs, logger,
|
||||||
|
)
|
||||||
|
|
||||||
|
b.StartTimer()
|
||||||
|
|
||||||
|
_, err := imp.resolve()
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,7 +90,7 @@ func newTestResourceSpec(desc specDescriptor) *Spec {
|
||||||
filecaches, err := filecache.NewCaches(s)
|
filecaches, err := filecache.NewCaches(s)
|
||||||
c.Assert(err, qt.IsNil)
|
c.Assert(err, qt.IsNil)
|
||||||
|
|
||||||
spec, err := NewSpec(s, filecaches, nil, output.DefaultFormats, media.DefaultTypes)
|
spec, err := NewSpec(s, filecaches, nil, nil, output.DefaultFormats, media.DefaultTypes)
|
||||||
c.Assert(err, qt.IsNil)
|
c.Assert(err, qt.IsNil)
|
||||||
return spec
|
return spec
|
||||||
}
|
}
|
||||||
|
@ -129,7 +129,7 @@ func newTestResourceOsFs(c *qt.C) (*Spec, string) {
|
||||||
filecaches, err := filecache.NewCaches(s)
|
filecaches, err := filecache.NewCaches(s)
|
||||||
c.Assert(err, qt.IsNil)
|
c.Assert(err, qt.IsNil)
|
||||||
|
|
||||||
spec, err := NewSpec(s, filecaches, nil, output.DefaultFormats, media.DefaultTypes)
|
spec, err := NewSpec(s, filecaches, nil, nil, output.DefaultFormats, media.DefaultTypes)
|
||||||
c.Assert(err, qt.IsNil)
|
c.Assert(err, qt.IsNil)
|
||||||
|
|
||||||
return spec, workDir
|
return spec, workDir
|
||||||
|
|
|
@ -28,6 +28,7 @@ import (
|
||||||
|
|
||||||
bp "github.com/gohugoio/hugo/bufferpool"
|
bp "github.com/gohugoio/hugo/bufferpool"
|
||||||
|
|
||||||
|
"github.com/gohugoio/hugo/common/herrors"
|
||||||
"github.com/gohugoio/hugo/common/hugio"
|
"github.com/gohugoio/hugo/common/hugio"
|
||||||
"github.com/gohugoio/hugo/common/maps"
|
"github.com/gohugoio/hugo/common/maps"
|
||||||
"github.com/gohugoio/hugo/helpers"
|
"github.com/gohugoio/hugo/helpers"
|
||||||
|
@ -392,16 +393,24 @@ func (r *resourceAdapter) transform(publish, setContent bool) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
notAvailableErr := func(err error) error {
|
newErr := func(err error) error {
|
||||||
errMsg := err.Error()
|
|
||||||
|
msg := fmt.Sprintf("%s: failed to transform %q (%s)", strings.ToUpper(tr.Key().Name), tctx.InPath, tctx.InMediaType.Type())
|
||||||
|
|
||||||
|
if err == herrors.ErrFeatureNotAvailable {
|
||||||
|
var errMsg string
|
||||||
if tr.Key().Name == "postcss" {
|
if tr.Key().Name == "postcss" {
|
||||||
// This transformation is not available in this
|
// This transformation is not available in this
|
||||||
// Most likely because PostCSS is not installed.
|
// Most likely because PostCSS is not installed.
|
||||||
errMsg += ". Check your PostCSS installation; install with \"npm install postcss-cli\". See https://gohugo.io/hugo-pipes/postcss/"
|
errMsg = ". Check your PostCSS installation; install with \"npm install postcss-cli\". See https://gohugo.io/hugo-pipes/postcss/"
|
||||||
} else if tr.Key().Name == "tocss" {
|
} else if tr.Key().Name == "tocss" {
|
||||||
errMsg += ". Check your Hugo installation; you need the extended version to build SCSS/SASS."
|
errMsg = ". Check your Hugo installation; you need the extended version to build SCSS/SASS."
|
||||||
}
|
}
|
||||||
return fmt.Errorf("%s: failed to transform %q (%s): %s", strings.ToUpper(tr.Key().Name), tctx.InPath, tctx.InMediaType.Type(), errMsg)
|
|
||||||
|
return errors.New(msg + errMsg)
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.Wrap(err, msg)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -411,18 +420,22 @@ func (r *resourceAdapter) transform(publish, setContent bool) error {
|
||||||
tryFileCache = true
|
tryFileCache = true
|
||||||
} else {
|
} else {
|
||||||
err = tr.Transform(tctx)
|
err = tr.Transform(tctx)
|
||||||
|
if err != nil && err != herrors.ErrFeatureNotAvailable {
|
||||||
|
return newErr(err)
|
||||||
|
}
|
||||||
|
|
||||||
if mayBeCachedOnDisk {
|
if mayBeCachedOnDisk {
|
||||||
tryFileCache = r.spec.BuildConfig.UseResourceCache(err)
|
tryFileCache = r.spec.BuildConfig.UseResourceCache(err)
|
||||||
}
|
}
|
||||||
if err != nil && !tryFileCache {
|
if err != nil && !tryFileCache {
|
||||||
return notAvailableErr(err)
|
return newErr(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if tryFileCache {
|
if tryFileCache {
|
||||||
f := r.target.tryTransformedFileCache(key, updates)
|
f := r.target.tryTransformedFileCache(key, updates)
|
||||||
if f == nil {
|
if f == nil {
|
||||||
return notAvailableErr(errors.Errorf("resource %q not found in file cache", key))
|
return newErr(errors.Errorf("resource %q not found in file cache", key))
|
||||||
}
|
}
|
||||||
transformedContentr = f
|
transformedContentr = f
|
||||||
updates.sourceFs = cache.fileCache.Fs
|
updates.sourceFs = cache.fileCache.Fs
|
||||||
|
@ -525,8 +538,12 @@ func (r *resourceAdapter) initTransform(publish, setContent bool) {
|
||||||
|
|
||||||
r.transformationsErr = r.transform(publish, setContent)
|
r.transformationsErr = r.transform(publish, setContent)
|
||||||
if r.transformationsErr != nil {
|
if r.transformationsErr != nil {
|
||||||
|
if r.spec.ErrorSender != nil {
|
||||||
|
r.spec.ErrorSender.SendError(r.transformationsErr)
|
||||||
|
} else {
|
||||||
r.spec.Logger.ERROR.Printf("Transformation failed: %s", r.transformationsErr)
|
r.spec.Logger.ERROR.Printf("Transformation failed: %s", r.transformationsErr)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if publish && r.publishOnce != nil {
|
if publish && r.publishOnce != nil {
|
||||||
|
|
Loading…
Reference in a new issue