The boilerplate I use for creating Hugo projects. It compiles Sass, transpiles JavaScript, optimises images, creates SVG sprites and adds some other extras.
It uses:
- Sass
- Normalize
- PostCSS
- Babel
- stylelint
- prettier
- stylelint-no-unsupported-browser-features
- ESLint
- eslint-plugin-compat
- svgo
- svg-sprite-generator
- npm scripts
- browserslist
- imagemin
- imagemin-pngquant
- imagemin-mozjpeg
- netlify cli
git clone https://github.com/adamrutter/hugo-boilerplate.gitcd hugo-boilerplatenpm installhugo
Use this for development. Changes made to any files will be immediately reflected on http://localhost:1313.
- Starts the Hugo live server (through
netlify dev). - Compiles/minifies Sass to
static/css/main.cssupon any changes insrc/scss. Also generates source maps. - Transpiles JavaScript to
static/jsupon changes insrc/js. Also generates source maps. - Optimises SVG to
static/svgand builds a sprite atstatic/svg/sprite.svgupon changes insrc/svg. - Optimises images recursively to
static/img. Directory structure is maintained. Changes to directories (eg, copying a directory of images) are not reflected instatic/imgThis is because only the directory is detected as having changed.
Use this to build the website.
- Cleans the
publicdirectory. - Builds all source code. See Build Process for more details.
- Builds the website with
hugo. - Minifies the resulting
htmlfiles.
Similar to npm start. Use this when you want the live server to include drafts and future content during development.
- Starts the Hugo live server with the flags
--buildFuture --buildDrafts. - Compiles/minifies Sass to
static/css/main.cssupon any changes insrc/scss. Also generates source maps. - Transpiles JavaScript to
static/jsupon changes insrc/js. Also generates source maps. - Optimises SVG to
static/svgand builds a sprite atstatic/svg/sprite.svgupon changes insrc/svg. - Optimises images recursively to
static/img. Directory structure is maintained. Changes to directories (eg, copying a directory of images) are not reflected instatic/imgThis is because only the directory is detected as having changed.
Similar to npm run build. Use this to include drafts and future content in your build.
- Cleans the
publicdirectory. - Builds all source code. See Build Process for more details.
- Builds the website with
hugo --buildFuture --buildDrafts. - Minifies the resulting
htmlfiles.
Use this to lint your source code. It includes plugins to lint code against target browsers, using browserslist.
- Lints all
.scsscode insrcwith stylelint, usingstylelint-config-standard. - Lints all
.jscode insrcwith ESLint, usingeslint-config-google.
static/cssis cleaned.main.scssis compiled usingnode-sass, compressed and output tostatic/css/main.css.- The compiled
main.cssis run throughpostcss -u autoprefixerwith source maps disabled.
static/jsis cleaned.- All
.jsare transpiled to ES5 and piped touglify-js. uglify-jsminifies and mangles the code, and outputs tostatic/js/main.js.
static/svgis cleaned.- Any files in
src/svgare optimised usingsvgoand output tostatic/svg. - All
.svgfiles instatic/svgare used to build a sprite atstatic/svg/sprite.svg.
Note: src/svg must have no further sub-directories or the build will fail due to errors from svgo. The build will also fail if there are no .svg files in src/svg.
static/imgis cleaned.- All images in
src/imgare optimised usingimagemin, using the pluginsimagemin-pngquantandimagemin-mozjpeg, and output tostatic/img.
Note: This is recursive and the directory structure in src/img will be maintained in static/img.
hugogenerates the.htmlfiles.- All
.htmlfiles are minified usinghtml-minifier.
├── archetypes - For storing archetypes
│ └── default.md - The default archetype
│
├── content - For storing content
| |
│ ├── _index.md - Main homepage .md file; use for front matter
│ ├── home - The homepage directory
│ │ │
│ │ └── sections - For storing homepage content sections
│ │ ├── index.md - Signifies the headless bundle
│ │ └── section-00.md - A section of content
| |
│ ├── page-1 - The directory for page-1
│ ├── _index.md - Main page-1 .md file
│ │ │
│ │ └── sections - For storing page-1 content sections
│ │ ├── index.md - Signifies the headless bundle
│ │ └── section-00.md - A section of content
│ │
│ └── sub-directory - A sub-directory, for related pages
│ ├── _index.html - Front matter/content for the directory list page
│ └── page-2.md - A default page
│
├── functions - For netlify functions
│
├── layouts - For layouts
│ ├── 404.html - 404 template
│ ├── index.html - Homepage template
│ │
│ ├── _default - For default layouts
│ │ ├── baseof.html - Containing everything up to the <body> tag
│ │ ├── list.html - Default list page
│ │ └── single.html - Default single page
│ │
│ ├── partials - For partial templates
│ │ ├── footer.html - The footer
│ │ └── header.html - The header
│ │
│ └── sub-directory - Templates for content of the sub-directory type
│ └── single.html - Single page template for sub-directory
│
├── src - For source code
│ │
│ ├── img - For images
│ │
│ ├── js - For JavaScript source code
│ │ └── main.js - Main JavaScript file
│ │
│ ├── scss - For Sass source code
│ │ ├── main.scss - The main .scss file; imports normalize
│ │ │
│ │ ├── layouts - For layouts; grids etc
│ │ ├── pages - For page specific CSS
│ │ ├── variables - For Sass variables
│ │ └── components - For components; header, footer etc
│ │
│ └── svg - For SVG files
│
└── static - For static assets; images etc.
│
├── img - For optimised images
├── js - For transpiled JavaScript
├── css - For compiled CSS
└── svg - For optimised SVGs and sprites
Pages use templates based on their content type. This means you can override the default layouts (the ones in layouts/_default), if you have defined a custom layout for the page's content type.
Content type is set using the front matter variable type. If the variable is not set, it defaults to the page's parent directory within content.
To use custom layouts for pages in content/sub-directory, you need one or both of:
content/sub-directory/single.htmlcontent/sub-directory/list.html
Pages within sub-directory will now use these layouts by default.
Custom layouts for specific, individual pages follow the same structure. However, the layout must be specified in the front matter.
For example, to use a custom layout for content/sub-directory/page-2.md:
- Create your layout in
content/sub-directory/page-2.html. - Specify
layout: "page-2"in the front matter ofpage-2.md.
Note: The content type must still match.
As pages at the top-level of the content directory (eg. content/page-1.md) have a type of page, their layouts go in layouts/page.
You could also set a custom type.
Homepages (and others) often use multiple sections of content and the boilerplate attempts to provide an "out-of-the-box" solution to this. It provides:
- A headless bundle
content/home/sectionsfor storing these sections. - A
.GetPagemethod in the homepage template to fetch them.
Content directories that are not supposed to be rendered (like these section directories) should have draft: true added to their _index front matter, otherwise blank pages will be rendered.
Ordering of sections can be achieved using the weight variable in the section's front matter.
- Create a new section at
home/sections/section-nn.md. - Reference these sections in the homepage template using
{{ (index $section nn).Content }}.
- Create another headless bundle
content/my-page/sections. - Include
{{ $section := (.Site.GetPage "/my-page/sections").Resources.Match "*.md" }}in the page's template. - Create your content section at
/my-page/sections/section-nn.md. - Reference the sections with
{{ (index $section nn).Content }}.
Two menus are included:
- Header
- Footer
The header menu includes class="active" for the current page.
To make source mapping work properly in Chrome, you need to create a workspace; add the project root directory to Settings > Workspace in Chrome's Developer Tools.
The browsers the project supports can be defined in .browserslistrc. This effects autoprefixer, babel, eslint-plugin-compat and stylelint-no-unsupported-browser-features.
Configuration for image optimisation can be done in imagemin.js.
Quality can be adjusted using the variables under the Image quality heading.
pngquality is a string comprising a range of quality'50-75'.jpgquality is an integer75.