JavaScript Modules
JavaScript Modules
This directory contains site-specific JavaScript modules that are bundled into /assets/site.js.
How It Works
- Individual modules go in this
assets/js/directory - Bundle file at
/assets/site.jsimports all modules - Bundle is processed by Jekyll and output to
_site/assets/site.js - Loaded in head via
/_includes/kankoda/site/head-tagwithdeferattribute
Module Structure
Each module should be a self-contained IIFE (Immediately Invoked Function Expression):
/**
* Module Name
* Description of what this module does
*/
(function() {
// Your module code here
// Private variables and functions
const privateVar = 'value';
function privateFunction() {
// ...
}
// Initialization
privateFunction();
})();
Adding a New Module
- Create a new file in
assets/js/, e.g.,assets/js/my-module.js - Write your JavaScript as an IIFE (see structure above)
- Add it to
/assets/site.js - Jekyll will bundle it automatically on next build
Existing Modules
header.js- Adds ‘scrolled’ class to header when scrollY > 50px
Best Practices
- Keep modules focused on a single responsibility
- Use IIFEs to avoid global namespace pollution
- Add clear documentation comments at the top of each module
- Use
deferorasyncwhen loading scripts (already configured in head-tag) - Test your modules work independently and don’t conflict with each other