A base project for static sites with templating and compression right out of the box.
- Use plain HTML or template with Nunjucks
- Style in CSS orSASS
- Generate image thumbnails
- Compress, resize, and convett images to jpeg and webp
- Optimize svg files
- Minify Javascipt, CSS, and HTML
All done automatically!
This library uses Node.js; if you don't have that, get it first!
Clone or download this repo, then run npm install.
To make the website your own, you need to add content! Any files you want on the website should go in the assets folder, and the built output will be put in the public folder ready to be hosted!
HTML files are located in assets/html/pages. All html files in this folder and any subfolders are run through the templating engine. The contents of this folder are copied to the top level of the public/ output folder. That means that assets/html/pages/index.html is the default file for yourwebsite.com while assets/html/pages/subdir/about.html would be yourwebsite.com/subdir/about.html.
See the (Templating)[###Templating] section for more detailed information on using templating engine.
Add any css, sass, or scss files in assets/css or a subfolder thereof, then reference them in assets/css/style.sass with @use.
Add any Javascript files in assets/js or a subfolder thereof, then reference them in assets/js/main.js with require.
To compile the contents of the assets folder, run node build.js.
Everything is now built in the public folder. To view the website locally, run
cd public/
npx http-serverThen go to http://localhost:8080 in your web browser.
Editing the options.json file will change what happens during the build process.
shrink_images: Iftrueimages that are too wide will be resized to be smaller. See the Custom Custom Build Settings section for details.create_thumbnails: Iftruecreate smallerjpgversions of all image files. These files will have the same path and name, but with_thumb.jpgreplacing the original file extension.minify_html: Iftrueminify html files.minify_css: Iftrueminify CSS.minify_js: Iftrueminify Javascript.create_js_map: Iftruecreate a source map for the Javascript.optimize_svg: Iftruerun svg files through the svgo optimizer.
Editing the compile.json file will change deails of certain steps during the build process.
quality[1-100]: The quality of jpg to create when optimize_images istrue.width[1+]: The width to resize images to if they are over it when shrink_images istrue.fit[cover,contain,fill,inside, oroutside]: The type of resizing to do when shrink_images istrue. Refer to the sharp documentation for details.
quality[1-100]: The quality of jpg thumbnails when create_thumbnails istrue.width[1+]: The width of thumbnails when create_thumbnails istrue.fit[cover,contain,fill,inside, oroutside]: The type of resizing to do when create_thumbnails istrue. Refer to the sharp documentation for details.
The templating engine used is nunjucks, the documentation for which can be found here. Html files that should not be compiled directly (such as partials) can go anywhere in assets/html or subfolders thereof except the assets/html/pages folder.
data/website.json contains the parameters passed into the templating engine. However, there are several reserved keywords that will be overwritten when building:
options: The contents of (data/options.json)[data/options.json].images: A dictionary with keys being all paths under the (assets/img)[assets/img] folder, starting with 'img' and values being an array of all images under that path (without the filename extension).
Several helper macros have been pre-defined to make life easier. To use them in a template, you must import the (util.html)[assets/html/macros/util/html] file into your template. For example: {% import "../macros/util.html" as util %}.
Displays an image as webp with a jpg fallback, or as a regular image if it is an avg. Inputs:
src: The image path (excludingimg/) without the filename extension.alt: Optional alternate text.
{{ util.img("my_image.png", "Here's my image!") }}Strips the file extension from a string.
{{ "my_file.png" | stripExtension }} <!-- Outputs "my_file" -->Returns the filename within a string.
{{ "path/to/my_file.png" | filename }} <!-- Outputs "my_file.png" -->- When adding custom icons, consider using inline svgs from a site like IconMonstr instead of adding an entire font face.
- nunjucks for HTML templating
- dart-sass for compiling and minifying stylesheets
- webpack for bundling and minifying Javascript
- sharp for image conversion and resizing
- svgo for svg optimization
- node-minify for minifying HTML
Check out my other work at stuffjackmakes.com