2023-01-04 17:24:36 +00:00
// Copyright 2023 The Hugo Authors. All rights reserved.
2017-02-05 03:20:06 +00:00
//
// 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 commands
import (
2023-01-04 17:24:36 +00:00
"context"
2018-10-03 12:58:09 +00:00
"errors"
2022-04-26 17:57:04 +00:00
"fmt"
2023-02-18 22:43:26 +00:00
"io"
2020-12-02 12:23:25 +00:00
"os"
2023-01-04 17:24:36 +00:00
"os/signal"
2020-12-02 12:23:25 +00:00
"path/filepath"
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 07:16:58 +00:00
"sync"
2023-01-04 17:24:36 +00:00
"sync/atomic"
"syscall"
2020-12-02 12:23:25 +00:00
"time"
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 07:16:58 +00:00
2023-01-04 17:24:36 +00:00
jww "github.com/spf13/jwalterweatherman"
2020-03-08 15:33:15 +00:00
2023-05-18 17:32:49 +00:00
"go.uber.org/automaxprocs/maxprocs"
2023-01-04 17:24:36 +00:00
"github.com/bep/clock"
"github.com/bep/lazycache"
"github.com/bep/overlayfs"
"github.com/bep/simplecobra"
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 07:16:58 +00:00
2023-01-04 17:24:36 +00:00
"github.com/gohugoio/hugo/common/hstrings"
2022-04-26 17:57:04 +00:00
"github.com/gohugoio/hugo/common/htime"
2018-10-03 12:58:09 +00:00
"github.com/gohugoio/hugo/common/loggers"
2023-01-04 17:24:36 +00:00
"github.com/gohugoio/hugo/common/paths"
2018-04-07 09:27:22 +00:00
"github.com/gohugoio/hugo/config"
2023-01-04 17:24:36 +00:00
"github.com/gohugoio/hugo/config/allconfig"
2017-06-13 16:42:45 +00:00
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/hugofs"
2023-01-04 17:24:36 +00:00
"github.com/gohugoio/hugo/hugolib"
"github.com/spf13/afero"
"github.com/spf13/cobra"
2017-02-05 03:20:06 +00:00
)
2023-01-04 17:24:36 +00:00
var (
errHelp = errors . New ( "help requested" )
)
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 07:16:58 +00:00
2023-01-04 17:24:36 +00:00
// Execute executes a command.
func Execute ( args [ ] string ) error {
2023-05-18 17:32:49 +00:00
// Default GOMAXPROCS to be CPU limit aware, still respecting GOMAXPROCS env.
maxprocs . Set ( )
2023-01-04 17:24:36 +00:00
x , err := newExec ( )
if err != nil {
return err
}
args = mapLegacyArgs ( args )
cd , err := x . Execute ( context . Background ( ) , args )
if err != nil {
if err == errHelp {
cd . CobraCommand . Help ( )
fmt . Println ( )
return nil
}
if simplecobra . IsCommandError ( err ) {
// Print the help, but also return the error to fail the command.
cd . CobraCommand . Help ( )
fmt . Println ( )
}
}
return err
2018-10-03 12:58:09 +00:00
}
2023-01-04 17:24:36 +00:00
type commonConfig struct {
2023-05-19 10:54:42 +00:00
mu * sync . Mutex
2023-01-04 17:24:36 +00:00
configs * allconfig . Configs
cfg config . Provider
fs * hugofs . Fs
2022-03-18 07:54:44 +00:00
}
2023-01-04 17:24:36 +00:00
// This is the root command.
type rootCommand struct {
Printf func ( format string , v ... interface { } )
Println func ( a ... interface { } )
Out io . Writer
logger loggers . Logger
// The main cache busting key for the caches below.
configVersionID atomic . Int32
// Some, but not all commands need access to these.
// Some needs more than one, so keep them in a small cache.
commonConfigs * lazycache . Cache [ int32 , * commonConfig ]
hugoSites * lazycache . Cache [ int32 , * hugolib . HugoSites ]
commands [ ] simplecobra . Commander
// Flags
2023-05-17 10:45:51 +00:00
source string
buildWatch bool
environment string
2023-01-04 17:24:36 +00:00
2023-05-22 17:00:07 +00:00
// File format to read or write (TOML, YAML, JSON).
format string
2023-05-17 10:45:51 +00:00
// Common build flags.
2023-05-19 07:55:08 +00:00
baseURL string
gc bool
poll string
panicOnWarning bool
forceSyncStatic bool
printPathWarnings bool
printUnusedTemplates bool
2023-05-17 10:45:51 +00:00
// Profile flags (for debugging of performance problems)
cpuprofile string
memprofile string
mutexprofile string
traceprofile string
printm bool
2023-05-17 16:45:23 +00:00
// TODO(bep) var vs string
logging bool
verbose bool
verboseLog bool
debug bool
quiet bool
renderToMemory bool
cfgFile string
cfgDir string
logFile string
2023-05-17 10:45:51 +00:00
}
2023-01-04 17:24:36 +00:00
func ( r * rootCommand ) Build ( cd * simplecobra . Commandeer , bcfg hugolib . BuildCfg , cfg config . Provider ) ( * hugolib . HugoSites , error ) {
h , err := r . Hugo ( cfg )
if err != nil {
return nil , err
}
if err := h . Build ( bcfg ) ; err != nil {
return nil , err
2022-05-15 19:01:36 +00:00
}
2023-01-04 17:24:36 +00:00
return h , nil
2022-05-15 19:01:36 +00:00
}
2023-01-04 17:24:36 +00:00
func ( r * rootCommand ) Commands ( ) [ ] simplecobra . Commander {
return r . commands
2018-10-03 12:58:09 +00:00
}
2023-01-04 17:24:36 +00:00
func ( r * rootCommand ) ConfigFromConfig ( key int32 , oldConf * commonConfig ) ( * commonConfig , error ) {
cc , _ , err := r . commonConfigs . GetOrCreate ( key , func ( key int32 ) ( * commonConfig , error ) {
fs := oldConf . fs
configs , err := allconfig . LoadConfig (
allconfig . ConfigSourceDescriptor {
Flags : oldConf . cfg ,
Fs : fs . Source ,
Filename : r . cfgFile ,
ConfigDir : r . cfgDir ,
2023-05-21 12:25:16 +00:00
Logger : r . logger ,
2023-01-04 17:24:36 +00:00
Environment : r . environment ,
} ,
)
if err != nil {
return nil , err
}
2018-10-03 12:58:09 +00:00
2023-01-04 17:24:36 +00:00
if ! configs . Base . C . Clock . IsZero ( ) {
// TODO(bep) find a better place for this.
htime . Clock = clock . Start ( configs . Base . C . Clock )
}
return & commonConfig {
2023-05-19 10:54:42 +00:00
mu : oldConf . mu ,
2023-01-04 17:24:36 +00:00
configs : configs ,
cfg : oldConf . cfg ,
fs : fs ,
} , nil
2018-10-03 12:58:09 +00:00
2023-01-04 17:24:36 +00:00
} )
2018-10-03 12:58:09 +00:00
2023-01-04 17:24:36 +00:00
return cc , err
2018-10-03 12:58:09 +00:00
2017-02-05 03:20:06 +00:00
}
2023-01-04 17:24:36 +00:00
func ( r * rootCommand ) ConfigFromProvider ( key int32 , cfg config . Provider ) ( * commonConfig , error ) {
if cfg == nil {
panic ( "cfg must be set" )
2017-02-05 03:20:06 +00:00
}
2023-01-04 17:24:36 +00:00
cc , _ , err := r . commonConfigs . GetOrCreate ( key , func ( key int32 ) ( * commonConfig , error ) {
var dir string
if r . source != "" {
dir , _ = filepath . Abs ( r . source )
} else {
dir , _ = os . Getwd ( )
}
2017-02-05 03:20:06 +00:00
2023-01-04 17:24:36 +00:00
if cfg == nil {
cfg = config . New ( )
}
2023-05-18 11:29:33 +00:00
2023-01-04 17:24:36 +00:00
if ! cfg . IsSet ( "renderToDisk" ) {
cfg . Set ( "renderToDisk" , true )
}
if ! cfg . IsSet ( "workingDir" ) {
cfg . Set ( "workingDir" , dir )
2023-05-18 11:29:33 +00:00
} else {
if err := os . MkdirAll ( cfg . GetString ( "workingDir" ) , 0777 ) ; err != nil {
return nil , fmt . Errorf ( "failed to create workingDir: %w" , err )
}
}
// Load the config first to allow publishDir to be configured in config file.
configs , err := allconfig . LoadConfig (
allconfig . ConfigSourceDescriptor {
Flags : cfg ,
Fs : hugofs . Os ,
Filename : r . cfgFile ,
ConfigDir : r . cfgDir ,
Environment : r . environment ,
2023-05-21 12:25:16 +00:00
Logger : r . logger ,
2023-05-18 11:29:33 +00:00
} ,
)
if err != nil {
return nil , err
2023-01-04 17:24:36 +00:00
}
2023-05-18 11:29:33 +00:00
base := configs . Base
cfg . Set ( "publishDir" , base . PublishDir )
cfg . Set ( "publishDirStatic" , base . PublishDir )
cfg . Set ( "publishDirDynamic" , base . PublishDir )
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 07:00:23 +00:00
2023-01-04 17:24:36 +00:00
renderStaticToDisk := cfg . GetBool ( "renderStaticToDisk" )
2017-03-25 18:48:28 +00:00
2023-01-04 17:24:36 +00:00
sourceFs := hugofs . Os
var desinationFs afero . Fs
if cfg . GetBool ( "renderToDisk" ) {
desinationFs = hugofs . Os
} else {
desinationFs = afero . NewMemMapFs ( )
if renderStaticToDisk {
// Hybrid, render dynamic content to Root.
cfg . Set ( "publishDirDynamic" , "/" )
} else {
// Rendering to memoryFS, publish to Root regardless of publishDir.
cfg . Set ( "publishDirDynamic" , "/" )
cfg . Set ( "publishDirStatic" , "/" )
}
}
2022-04-26 17:57:04 +00:00
2023-01-04 17:24:36 +00:00
fs := hugofs . NewFromSourceAndDestination ( sourceFs , desinationFs , cfg )
if renderStaticToDisk {
dynamicFs := fs . PublishDir
publishDirStatic := cfg . GetString ( "publishDirStatic" )
workingDir := cfg . GetString ( "workingDir" )
absPublishDirStatic := paths . AbsPathify ( workingDir , publishDirStatic )
staticFs := afero . NewBasePathFs ( afero . NewOsFs ( ) , absPublishDirStatic )
// Serve from both the static and dynamic fs,
// the first will take priority.
// THis is a read-only filesystem,
// we do all the writes to
// fs.Destination and fs.DestinationStatic.
fs . PublishDirServer = overlayfs . New (
overlayfs . Options {
Fss : [ ] afero . Fs {
dynamicFs ,
staticFs ,
} ,
} ,
)
fs . PublishDirStatic = staticFs
2022-05-07 12:10:32 +00:00
2023-01-04 17:24:36 +00:00
}
2022-04-26 17:57:04 +00:00
2023-01-04 17:24:36 +00:00
if ! base . C . Clock . IsZero ( ) {
// TODO(bep) find a better place for this.
htime . Clock = clock . Start ( configs . Base . C . Clock )
}
2018-03-18 10:07:24 +00:00
2023-01-04 17:24:36 +00:00
if base . LogPathWarnings {
// Note that we only care about the "dynamic creates" here,
// so skip the static fs.
fs . PublishDir = hugofs . NewCreateCountingFs ( fs . PublishDir )
}
commonConfig := & commonConfig {
2023-05-19 10:54:42 +00:00
mu : & sync . Mutex { } ,
2023-01-04 17:24:36 +00:00
configs : configs ,
cfg : cfg ,
fs : fs ,
}
return commonConfig , nil
} )
2018-03-18 10:07:24 +00:00
2023-01-04 17:24:36 +00:00
return cc , err
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 09:02:14 +00:00
}
2023-01-04 17:24:36 +00:00
func ( r * rootCommand ) HugFromConfig ( conf * commonConfig ) ( * hugolib . HugoSites , error ) {
h , _ , err := r . hugoSites . GetOrCreate ( r . configVersionID . Load ( ) , func ( key int32 ) ( * hugolib . HugoSites , error ) {
depsCfg := deps . DepsCfg { Configs : conf . configs , Fs : conf . fs , Logger : r . logger }
return hugolib . NewHugoSites ( depsCfg )
} )
return h , err
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 09:02:14 +00:00
}
2023-01-04 17:24:36 +00:00
func ( r * rootCommand ) Hugo ( cfg config . Provider ) ( * hugolib . HugoSites , error ) {
h , _ , err := r . hugoSites . GetOrCreate ( r . configVersionID . Load ( ) , func ( key int32 ) ( * hugolib . HugoSites , error ) {
conf , err := r . ConfigFromProvider ( key , cfg )
if err != nil {
return nil , err
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 09:02:14 +00:00
}
2023-01-04 17:24:36 +00:00
depsCfg := deps . DepsCfg { Configs : conf . configs , Fs : conf . fs , Logger : r . logger }
return hugolib . NewHugoSites ( depsCfg )
} )
return h , err
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 09:02:14 +00:00
}
2023-01-04 17:24:36 +00:00
func ( r * rootCommand ) Name ( ) string {
return "hugo"
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 09:02:14 +00:00
}
2023-01-04 17:24:36 +00:00
func ( r * rootCommand ) Run ( ctx context . Context , cd * simplecobra . Commandeer , args [ ] string ) error {
if ! r . buildWatch {
defer r . timeTrack ( time . Now ( ) , "Total" )
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 09:02:14 +00:00
}
2023-01-04 17:24:36 +00:00
b := newHugoBuilder ( r , nil )
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 09:02:14 +00:00
2023-05-24 07:26:30 +00:00
if err := b . loadConfig ( cd , false ) ; err != nil {
2023-01-04 17:24:36 +00:00
return err
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 09:02:14 +00:00
}
2023-01-04 17:24:36 +00:00
err := func ( ) error {
if r . buildWatch {
defer r . timeTrack ( time . Now ( ) , "Built" )
}
err := b . build ( )
return err
} ( )
if err != nil {
return err
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 09:02:14 +00:00
}
2023-01-04 17:24:36 +00:00
if ! r . buildWatch {
// Done.
return nil
2018-03-18 10:07:24 +00:00
}
2023-01-04 17:24:36 +00:00
watchDirs , err := b . getDirList ( )
if err != nil {
return err
2018-10-26 15:02:53 +00:00
}
2023-01-04 17:24:36 +00:00
watchGroups := helpers . ExtractAndGroupRootPaths ( watchDirs )
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 07:00:23 +00:00
2023-01-04 17:24:36 +00:00
for _ , group := range watchGroups {
r . Printf ( "Watching for changes in %s\n" , group )
2018-03-18 10:07:24 +00:00
}
2023-01-04 17:24:36 +00:00
watcher , err := b . newWatcher ( r . poll , watchDirs ... )
if err != nil {
return err
2018-03-18 10:07:24 +00:00
}
2023-01-04 17:24:36 +00:00
defer watcher . Close ( )
2018-11-15 08:28:02 +00:00
2023-01-04 17:24:36 +00:00
r . Println ( "Press Ctrl+C to stop" )
2018-03-18 10:07:24 +00:00
2023-01-04 17:24:36 +00:00
sigs := make ( chan os . Signal , 1 )
signal . Notify ( sigs , syscall . SIGINT , syscall . SIGTERM )
2018-03-18 10:07:24 +00:00
2023-01-04 17:24:36 +00:00
<- sigs
2018-03-18 10:07:24 +00:00
2023-01-04 17:24:36 +00:00
return nil
}
2018-04-07 09:27:22 +00:00
2023-05-17 16:45:23 +00:00
func ( r * rootCommand ) PreRun ( cd , runner * simplecobra . Commandeer ) error {
2023-01-04 17:24:36 +00:00
r . Out = os . Stdout
if r . quiet {
r . Out = io . Discard
}
r . Printf = func ( format string , v ... interface { } ) {
if ! r . quiet {
fmt . Fprintf ( r . Out , format , v ... )
2021-08-31 10:08:11 +00:00
}
2018-03-18 10:07:24 +00:00
}
2023-01-04 17:24:36 +00:00
r . Println = func ( a ... interface { } ) {
if ! r . quiet {
fmt . Fprintln ( r . Out , a ... )
}
2022-04-26 17:57:04 +00:00
}
2023-01-04 17:24:36 +00:00
_ , running := runner . Command . ( * serverCommand )
var err error
r . logger , err = r . createLogger ( running )
2022-05-07 12:10:32 +00:00
if err != nil {
return err
2018-03-18 10:07:24 +00:00
}
2023-05-22 17:00:07 +00:00
switch r . format {
case "json" , "toml" , "yaml" :
// OK
default :
return fmt . Errorf ( "unsupported format %q; must be one of json, toml or yaml" , r . format )
}
2018-03-18 10:07:24 +00:00
2023-01-04 17:24:36 +00:00
loggers . PanicOnWarning . Store ( r . panicOnWarning )
r . commonConfigs = lazycache . New [ int32 , * commonConfig ] ( lazycache . Options { MaxEntries : 5 } )
r . hugoSites = lazycache . New [ int32 , * hugolib . HugoSites ] ( lazycache . Options { MaxEntries : 5 } )
2018-10-03 12:58:09 +00:00
2023-01-04 17:24:36 +00:00
return nil
}
func ( r * rootCommand ) createLogger ( running bool ) ( loggers . Logger , error ) {
var (
logHandle = io . Discard
logThreshold = jww . LevelWarn
outHandle = r . Out
stdoutThreshold = jww . LevelWarn
)
if r . verboseLog || r . logging || ( r . logFile != "" ) {
var err error
if r . logFile != "" {
logHandle , err = os . OpenFile ( r . logFile , os . O_RDWR | os . O_APPEND | os . O_CREATE , 0666 )
if err != nil {
return nil , fmt . Errorf ( "Failed to open log file %q: %s" , r . logFile , err )
}
} else {
logHandle , err = os . CreateTemp ( "" , "hugo" )
if err != nil {
return nil , err
}
2018-04-07 09:27:22 +00:00
}
2023-01-04 17:24:36 +00:00
} else if r . verbose {
stdoutThreshold = jww . LevelInfo
2018-03-18 10:07:24 +00:00
}
2023-01-04 17:24:36 +00:00
if r . debug {
stdoutThreshold = jww . LevelDebug
2018-03-18 10:07:24 +00:00
}
2023-01-04 17:24:36 +00:00
if r . verboseLog {
logThreshold = jww . LevelInfo
if r . debug {
logThreshold = jww . LevelDebug
}
2020-05-27 11:50:13 +00:00
}
2018-04-07 09:27:22 +00:00
2023-01-04 17:24:36 +00:00
loggers . InitGlobalLogger ( stdoutThreshold , logThreshold , outHandle , logHandle )
helpers . InitLoggers ( )
return loggers . NewLogger ( stdoutThreshold , logThreshold , outHandle , logHandle , running ) , nil
}
2018-03-18 10:07:24 +00:00
2023-01-04 17:24:36 +00:00
func ( r * rootCommand ) Reset ( ) {
r . logger . Reset ( )
}
2018-03-18 10:07:24 +00:00
2023-01-04 17:24:36 +00:00
// IsTestRun reports whether the command is running as a test.
func ( r * rootCommand ) IsTestRun ( ) bool {
return os . Getenv ( "HUGO_TESTRUN" ) != ""
}
2018-03-18 10:07:24 +00:00
2023-05-17 16:45:23 +00:00
func ( r * rootCommand ) Init ( cd * simplecobra . Commandeer ) error {
cmd := cd . CobraCommand
2023-01-04 17:24:36 +00:00
cmd . Use = "hugo [flags]"
cmd . Short = "hugo builds your site"
cmd . Long = ` hugo is the main command , used to build your Hugo site .
Hugo is a Fast and Flexible Static Site Generator
built with love by spf13 and friends in Go .
Complete documentation is available at https : //gohugo.io/.`
// Configure persistent flags
cmd . PersistentFlags ( ) . StringVarP ( & r . source , "source" , "s" , "" , "filesystem path to read files relative from" )
2023-05-22 17:00:07 +00:00
cmd . PersistentFlags ( ) . StringVar ( & r . format , "format" , "toml" , "preferred file format (toml, yaml or json)" )
2023-01-04 17:24:36 +00:00
cmd . PersistentFlags ( ) . SetAnnotation ( "source" , cobra . BashCompSubdirsInDir , [ ] string { } )
2023-05-17 10:45:51 +00:00
cmd . PersistentFlags ( ) . StringP ( "destination" , "d" , "" , "filesystem path to write files to" )
cmd . PersistentFlags ( ) . SetAnnotation ( "destination" , cobra . BashCompSubdirsInDir , [ ] string { } )
2023-01-04 17:24:36 +00:00
cmd . PersistentFlags ( ) . StringVarP ( & r . environment , "environment" , "e" , "" , "build environment" )
cmd . PersistentFlags ( ) . StringP ( "themesDir" , "" , "" , "filesystem path to themes directory" )
cmd . PersistentFlags ( ) . StringP ( "ignoreVendorPaths" , "" , "" , "ignores any _vendor for module paths matching the given Glob pattern" )
cmd . PersistentFlags ( ) . String ( "clock" , "" , "set the clock used by Hugo, e.g. --clock 2021-11-06T22:30:00.00+09:00" )
cmd . PersistentFlags ( ) . StringVar ( & r . cfgFile , "config" , "" , "config file (default is hugo.yaml|json|toml)" )
cmd . PersistentFlags ( ) . StringVar ( & r . cfgDir , "configDir" , "config" , "config dir" )
cmd . PersistentFlags ( ) . BoolVar ( & r . quiet , "quiet" , false , "build in quiet mode" )
// Set bash-completion
_ = cmd . PersistentFlags ( ) . SetAnnotation ( "config" , cobra . BashCompFilenameExt , config . ValidConfigFileExtensions )
cmd . PersistentFlags ( ) . BoolVarP ( & r . verbose , "verbose" , "v" , false , "verbose output" )
cmd . PersistentFlags ( ) . BoolVarP ( & r . debug , "debug" , "" , false , "debug output" )
cmd . PersistentFlags ( ) . BoolVar ( & r . logging , "log" , false , "enable Logging" )
cmd . PersistentFlags ( ) . StringVar ( & r . logFile , "logFile" , "" , "log File path (if set, logging enabled automatically)" )
cmd . PersistentFlags ( ) . BoolVar ( & r . verboseLog , "verboseLog" , false , "verbose logging" )
cmd . Flags ( ) . BoolVarP ( & r . buildWatch , "watch" , "w" , false , "watch filesystem for changes and recreate as needed" )
cmd . Flags ( ) . BoolVar ( & r . renderToMemory , "renderToMemory" , false , "render to memory (only useful for benchmark testing)" )
// Set bash-completion
_ = cmd . PersistentFlags ( ) . SetAnnotation ( "logFile" , cobra . BashCompFilenameExt , [ ] string { } )
// Configure local flags
2023-05-17 16:45:23 +00:00
applyLocalBuildFlags ( cmd , r )
2023-05-17 10:45:51 +00:00
// Set bash-completion.
// Each flag must first be defined before using the SetAnnotation() call.
_ = cmd . Flags ( ) . SetAnnotation ( "source" , cobra . BashCompSubdirsInDir , [ ] string { } )
return nil
}
2023-05-17 16:45:23 +00:00
func applyLocalBuildFlags ( cmd * cobra . Command , r * rootCommand ) {
2023-01-04 17:24:36 +00:00
cmd . Flags ( ) . Bool ( "cleanDestinationDir" , false , "remove files from destination not found in static directories" )
cmd . Flags ( ) . BoolP ( "buildDrafts" , "D" , false , "include content marked as draft" )
cmd . Flags ( ) . BoolP ( "buildFuture" , "F" , false , "include content with publishdate in the future" )
cmd . Flags ( ) . BoolP ( "buildExpired" , "E" , false , "include expired content" )
cmd . Flags ( ) . StringP ( "contentDir" , "c" , "" , "filesystem path to content directory" )
cmd . Flags ( ) . StringP ( "layoutDir" , "l" , "" , "filesystem path to layout directory" )
cmd . Flags ( ) . StringP ( "cacheDir" , "" , "" , "filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/" )
cmd . Flags ( ) . BoolP ( "ignoreCache" , "" , false , "ignores the cache directory" )
cmd . Flags ( ) . StringSliceP ( "theme" , "t" , [ ] string { } , "themes to use (located in /themes/THEMENAME/)" )
2023-05-17 16:45:23 +00:00
cmd . Flags ( ) . StringVarP ( & r . baseURL , "baseURL" , "b" , "" , "hostname (and path) to the root, e.g. https://spf13.com/" )
2023-01-04 17:24:36 +00:00
cmd . Flags ( ) . Bool ( "enableGitInfo" , false , "add Git revision, date, author, and CODEOWNERS info to the pages" )
2023-05-17 16:45:23 +00:00
cmd . Flags ( ) . BoolVar ( & r . gc , "gc" , false , "enable to run some cleanup tasks (remove unused cache files) after the build" )
cmd . Flags ( ) . StringVar ( & r . poll , "poll" , "" , "set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes" )
cmd . Flags ( ) . BoolVar ( & r . panicOnWarning , "panicOnWarning" , false , "panic on first WARNING log" )
2023-01-04 17:24:36 +00:00
cmd . Flags ( ) . Bool ( "templateMetrics" , false , "display metrics about template executions" )
cmd . Flags ( ) . Bool ( "templateMetricsHints" , false , "calculate some improvement hints when combined with --templateMetrics" )
2023-05-17 16:45:23 +00:00
cmd . Flags ( ) . BoolVar ( & r . forceSyncStatic , "forceSyncStatic" , false , "copy all files when static is changed." )
2023-01-04 17:24:36 +00:00
cmd . Flags ( ) . BoolP ( "noTimes" , "" , false , "don't sync modification time of files" )
cmd . Flags ( ) . BoolP ( "noChmod" , "" , false , "don't sync permission mode of files" )
cmd . Flags ( ) . BoolP ( "noBuildLock" , "" , false , "don't create .hugo_build.lock file" )
cmd . Flags ( ) . BoolP ( "printI18nWarnings" , "" , false , "print missing translations" )
2023-05-19 07:55:08 +00:00
cmd . Flags ( ) . BoolVarP ( & r . printPathWarnings , "printPathWarnings" , "" , false , "print warnings on duplicate target paths etc." )
cmd . Flags ( ) . BoolVarP ( & r . printUnusedTemplates , "printUnusedTemplates" , "" , false , "print warnings on unused templates." )
2023-05-17 16:45:23 +00:00
cmd . Flags ( ) . StringVarP ( & r . cpuprofile , "profile-cpu" , "" , "" , "write cpu profile to `file`" )
cmd . Flags ( ) . StringVarP ( & r . memprofile , "profile-mem" , "" , "" , "write memory profile to `file`" )
cmd . Flags ( ) . BoolVarP ( & r . printm , "printMemoryUsage" , "" , false , "print memory usage to screen at intervals" )
cmd . Flags ( ) . StringVarP ( & r . mutexprofile , "profile-mutex" , "" , "" , "write Mutex profile to `file`" )
cmd . Flags ( ) . StringVarP ( & r . traceprofile , "trace" , "" , "" , "write trace to `file` (not useful in general)" )
2023-01-04 17:24:36 +00:00
// Hide these for now.
cmd . Flags ( ) . MarkHidden ( "profile-cpu" )
cmd . Flags ( ) . MarkHidden ( "profile-mem" )
cmd . Flags ( ) . MarkHidden ( "profile-mutex" )
cmd . Flags ( ) . StringSlice ( "disableKinds" , [ ] string { } , "disable different kind of pages (home, RSS etc.)" )
cmd . Flags ( ) . Bool ( "minify" , false , "minify any supported output format (HTML, XML etc.)" )
_ = cmd . Flags ( ) . SetAnnotation ( "cacheDir" , cobra . BashCompSubdirsInDir , [ ] string { } )
_ = cmd . Flags ( ) . SetAnnotation ( "destination" , cobra . BashCompSubdirsInDir , [ ] string { } )
_ = cmd . Flags ( ) . SetAnnotation ( "theme" , cobra . BashCompSubdirsInDir , [ ] string { "themes" } )
2019-03-14 20:39:00 +00:00
2023-01-04 17:24:36 +00:00
}
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 09:02:14 +00:00
2023-01-04 17:24:36 +00:00
func ( r * rootCommand ) timeTrack ( start time . Time , name string ) {
elapsed := time . Since ( start )
r . Printf ( "%s in %v ms\n" , name , int ( 1000 * elapsed . Seconds ( ) ) )
}
2019-01-02 11:33:26 +00:00
2023-01-04 17:24:36 +00:00
type simpleCommand struct {
use string
name string
short string
long string
run func ( ctx context . Context , cd * simplecobra . Commandeer , rootCmd * rootCommand , args [ ] string ) error
withc func ( cmd * cobra . Command )
initc func ( cd * simplecobra . Commandeer ) error
2019-01-02 11:33:26 +00:00
2023-01-04 17:24:36 +00:00
commands [ ] simplecobra . Commander
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 14:01:25 +00:00
2023-01-04 17:24:36 +00:00
rootCmd * rootCommand
}
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 14:01:25 +00:00
2023-01-04 17:24:36 +00:00
func ( c * simpleCommand ) Commands ( ) [ ] simplecobra . Commander {
return c . commands
}
2022-03-18 07:54:44 +00:00
2023-01-04 17:24:36 +00:00
func ( c * simpleCommand ) Name ( ) string {
return c . name
}
2018-03-18 10:07:24 +00:00
2023-01-04 17:24:36 +00:00
func ( c * simpleCommand ) Run ( ctx context . Context , cd * simplecobra . Commandeer , args [ ] string ) error {
if c . run == nil {
return nil
2018-03-18 10:07:24 +00:00
}
2023-01-04 17:24:36 +00:00
return c . run ( ctx , cd , c . rootCmd , args )
}
2018-03-18 10:07:24 +00:00
2023-05-17 16:45:23 +00:00
func ( c * simpleCommand ) Init ( cd * simplecobra . Commandeer ) error {
cmd := cd . CobraCommand
2023-01-04 17:24:36 +00:00
cmd . Short = c . short
cmd . Long = c . long
if c . use != "" {
cmd . Use = c . use
}
if c . withc != nil {
c . withc ( cmd )
2018-03-18 10:07:24 +00:00
}
2023-01-04 17:24:36 +00:00
return nil
}
2018-03-18 10:07:24 +00:00
2023-05-17 16:45:23 +00:00
func ( c * simpleCommand ) PreRun ( cd , runner * simplecobra . Commandeer ) error {
2023-01-04 17:24:36 +00:00
c . rootCmd = cd . Root . Command . ( * rootCommand )
if c . initc != nil {
return c . initc ( cd )
}
2018-03-18 10:07:24 +00:00
return nil
2017-02-05 03:20:06 +00:00
}
2023-01-04 17:24:36 +00:00
func mapLegacyArgs ( args [ ] string ) [ ] string {
if len ( args ) > 1 && args [ 0 ] == "new" && ! hstrings . EqualAny ( args [ 1 ] , "site" , "theme" , "content" ) {
// Insert "content" as the second argument
args = append ( args [ : 1 ] , append ( [ ] string { "content" } , args [ 1 : ] ... ) ... )
}
return args
}