build_unified_assets.php
Path: build/build_unified_assets.php | Language: PHP | Lines: ~130
Concatenates individual CSS and JS modules into unified bundles for production deployment.
Overview
This script is the primary asset bundler for gwern.net's frontend code. It reads lists of individual CSS and JavaScript files and concatenates them into unified bundles that get loaded in the browser. This approach balances developer ergonomics (working with modular source files) with production performance (minimizing HTTP requests).
The script produces four main bundles: head-GENERATED.css and head-GENERATED.js (critical resources loaded synchronously in the document head), and style-GENERATED.css and script-GENERATED.js (deferred resources loaded after initial paint). It also generates a special transclude-templates-GENERATED.js file that embeds HTML templates as JavaScript template literals.
This script runs early in the build process (typically invoked by sync.sh) and must complete before any HTML generation that references these bundled assets.
Key Functions/Variables
Asset Lists
$head_css: Array of CSS files loaded in<head>(initial paint styles: inlined-images, initial.css, special-occasions.css, fonts, reader-mode)$head_js: Array of JS files loaded in<head>(core utilities: utility.js, initial.js, layout.js, color.js, dark-mode-initial.js, etc.)$css: Array of main CSS files loaded after initial paint (inlined-images-GENERATED.css, fonts-VERSIONED.css, default.css, links.css)$js: Array of main JS modules loaded deferred (popups, annotations, transclude, extracts, typography, collapse, sidenotes, etc.)
Template Processing
$templates: Glob result from{$template_dir}/*.tmpl- all HTML template files- Template files are read, escaped (backticks and backslashes), and embedded as JavaScript template literals in the
Transclude.templatesobject
Generated Files
head-GENERATED.css- Concatenated critical CSShead-GENERATED.js- Concatenated critical JavaScriptstyle-GENERATED.css- Concatenated main CSSscript-GENERATED.js- Concatenated main JavaScripttransclude-templates-GENERATED.js- Embedded HTML templates as JS object
Input/Output
Input Files
CSS:
/css/inlined-images-initial-GENERATED.css/css/initial.css/css/special-occasions.css/css/initial-fonts-VERSIONED.css/css/reader-mode-initial.css/css/inlined-images-GENERATED.css/css/fonts-VERSIONED.css/css/default.css/css/links.css
JavaScript:
/js/utility.js/js/initial.js/js/layout.js/js/color.js/js/rewrite-initial.js/js/special-occasions.js/js/dark-mode-initial.js/js/reader-mode-initial.js/js/asset-versions-GENERATED.js/js/misc.js/js/popups.js,/js/popins.js/js/annotations.js/js/content.js,/js/transclude.js/js/extracts.js,/js/extracts-annotations.js,/js/extracts-content.js,/js/extracts-options.js,/js/extracts-load.js/js/typography.js/js/Hyphenopoly_Loader.js/js/rewrite.js,/js/collapse.js,/js/sidenotes.js/js/image-focus.js/js/dark-mode.js,/js/reader-mode.js
Templates:
/template/include/*.tmpl(all template files)
Output Files
/css/head-GENERATED.css/js/head-GENERATED.js/css/style-GENERATED.css/js/script-GENERATED.js/js/transclude-templates-GENERATED.js
All output file paths are appended to $updated_files[] for potential downstream tracking.
Usage
Invoked during the build process, typically by sync.sh:
php /path/to/build/build_unified_assets.php
No command-line arguments required. The script:
- Requires
build_paths.php,build_variables.php, andbuild_functions.php - Reads and concatenates files according to the hardcoded arrays
- Writes generated bundles to disk
- Exits silently on success, or errors if files are missing
See Also
- sync.sh - Main build orchestrator that invokes this script
- pre-commit-hook.php - Git hook that triggers asset rebuilds
- build_asset_versions.php - Generates cache-busting version hashes for assets
- build_head_includes.php - Creates inlined HTML includes that reference these bundles
- build_body_includes.php - Creates body-end includes with versioned asset references
- build_functions.php - Utility functions for versioning and file operations
- transclude.js - Consumes the generated
transclude-templates-GENERATED.js