AssetGraph-builder: Web Application Build System
AssetGraph-builder is a robust, opinionated build system primarily designed for single-page web applications, leveraging the powerful AssetGraph library. It distinguishes itself by requiring no explicit build manifest; instead, it intelligently discovers all project assets (HTML, CSS, JavaScript, images, fonts, etc.) by traversing the dependency graph from specified HTML entry points. The current stable version is 9.3.0. While no fixed release cadence is stated, the project demonstrates active maintenance with recent updates, with the latest release in December 2023. Key features include bundling, minification (JavaScript with Terser, CSS with cssnano, HTML with html-minifier), advanced image optimization (supporting various tools like Sharp, pngquant, jpegtran), cache busting via MD5-hashed filenames, CDN rewriting, and automatic Content-Security-Policy updates. It excels in optimizing web assets for production, reducing HTTP requests, and improving load times, making it suitable for complex static site and web application deployments without extensive configuration.
Common errors
-
Error: spawn pngquant ENOENT
cause One or more of the optional image optimization CLI tools (like `pngquant`, `optipng`, `jpegtran`) are not installed or not available in the system's PATH.fixInstall the missing command-line tools on your operating system. For example, on Ubuntu/Debian: `sudo apt-get install pngquant optipng jpegtran`. For macOS using Homebrew: `brew install pngquant optipng libjpeg`. -
Error: Input file 'path/to/your/index.html' not found
cause The specified input HTML file does not exist at the given path, or the path is incorrect.fixVerify the absolute or relative path to your primary HTML entry file. Ensure the command is executed from a directory where the relative path is correct, or provide the full absolute path. -
Cannot find module 'assetgraph'
cause The `assetgraph` package, a peer dependency, is not installed or resolvable in the environment where `assetgraph-builder` is run.fixEnsure `assetgraph` is installed alongside `assetgraph-builder`. If using npm, `npm install assetgraph` in your project directory. If `assetgraph-builder` is installed globally, you might need to ensure global peer dependencies are met or install `assetgraph` locally in projects where you run the builder.
Warnings
- gotcha Full image optimization features require external system-level libraries (e.g., pngquant, GraphicsMagick, OptiPNG, JpegTran). While `assetgraph-builder` provides npm wrappers for some (e.g., `pngquant-bin`), others like `GraphicsMagick` require manual installation on the host system to unlock their capabilities, as indicated in the README.
- gotcha AssetGraph-builder operates without a build manifest, automatically discovering assets by traversing the DOM. This can be a paradigm shift for developers used to explicit configuration files (e.g., Webpack, Grunt). Unexpected assets might be included or excluded if the initial HTML entry point doesn't fully represent the application's true dependency graph.
- breaking The package performs cache busting by renaming JavaScript, CSS, and image files with an MD5 hash prefix. This process fundamentally changes asset filenames, which can break existing build pipelines, caching strategies, or CDN configurations that rely on static file paths or more predictable naming conventions.
- gotcha AssetGraph-builder automatically updates existing Content-Security-Policy (CSP) meta tags in HTML to reflect changes made during the build, including adding hashes for inline scripts and stylesheets. While a helpful feature, this can inadvertently relax strict CSPs or conflict with server-side CSP headers if not carefully managed, potentially introducing security vulnerabilities or unexpected content blocking.
Install
-
npm install assetgraph-builder -
yarn add assetgraph-builder -
pnpm add assetgraph-builder
Imports
- buildProduction
import { buildProduction } from 'assetgraph-builder';npm install -g assetgraph-builder
Quickstart
npm install -g assetgraph-builder buildProduction path/to/your/index.html -o path/to/output/directory