/hugo-webpack-boilerplate

Hugo and Webpack boilerplate.

Primary LanguageJavaScriptMIT LicenseMIT

hugo-webpack-boilerplate

Hugo and Webpack boilerplate.

Install

npm install hugo-webpack-boilerplate

Usage

We all love Hugo, but it is cumbersome to use anything beyond simple HTML, CSS, and JavaScript. What about LESS/SASS, ES6, minification, tree-shaking, or asset revving? Sure, you can roll your own, but these are all things Webpack was designed for.

Given Webpack typically takes a JavaScript file as entry point, but Hugo generates mostly HTML, it is quite complicated to come up with a useful workflow. This repository is an attempt to bind Hugo and Webpack together, giving Webpack full power over the files generated by Hugo. You can continue to use Hugo as you normally would, but enjoy the additional benefits of Webpack.

In your webpack.config.js, use:

// Exports.
module.exports = require('hugo-webpack-boilerplate');

Alternatively, if you are not using Hugo’s default publishDir, use:

// Exports.
module.exports = require('hugo-webpack-boilerplate').fromConfig({
  hugoPublishDir: 'hugo-publish-dir', // Defaults to public/
  outDir: 'my-publish-dir' // Defaults to dist/
});

Currently, only the Hugo publishDir and outDir are customizable. Over time, this module will be extended to allow further customizations.

Example 1

This example considers a simple HTML file, a SASS stylesheet, image, and ES6 JavaScript file. The assets live under static/ (Hugo’s default), so for example, the image should be located at static/img/example.jpg. Note the usage of ~ (tilde), which will make sure Webpack can find the assets. Also, this module relies on interpolation by the html-loader, so make sure to import assets using require, as shown below.

When running webpack -p, Webpack will take all these assets, optimize-minimize them, and put them in the dist/ folder. Alternatively, you can run webpack -w to start the local development server.

Input

layouts/index.html

<!doctype html>
<link rel="stylesheet" href="~scss/style.scss" />
<title>This is an example</title>
<img src="${require(`~img/example.jpg`)}" />
<script src="${require(`~js/script.js`)}"></script>

Output

dist/index.html

<!doctype html>
<link rel="stylesheet" href="/css/style.hash.css" />
<title>This is an example</title>
<img src="/img/example.hash.jpg" />
<script src="/js/script.hash.js"></script>

FAQ

  • I’m getting UnCSS errors, like Could not load script: "file:///~js/script.js"?

UnCSS attempts to load scripts from disk, which will not work if they are referenced by ~. The error is however non-blocking, and the UnCSS output is still useful. Just note that any selectors dynamically added will not be in the resulting stylesheet.

  • The watching task causes Webpack to continuously reload?

There is a bug in watchpack causing the watcher to reload the same files over and over until the files settle. Apart from being an inconvenience, it does not affect your bundles.

  • Can I use configure or add custom Webpack loaders or plugins?

Right now, no, there is no way to do this. Feel free to submit an issue or PR if you’d like to see any additions. It should be pretty simple to add this to the fromConfig function in src/index.js.

Changelog

See the CHANGELOG for a list of changes.

License

The MIT License (MIT)

Copyright (c) 2017 Mark van Seventer

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.