@vycital/rehash
v1.0.0
Published
A CLI tool for cache-busting static assets by embedding hashes in filenames and updating references
Readme
Rehash
A CLI tool for cache-busting static assets by embedding hashes in filenames and updating references.
Features
- Renames files by embedding a hash in filename (e.g.,
file.js→file.rehash--abc123...--rehash.js) - Updates references in HTML and JS files to point to the newly renamed files
- Supports glob patterns for selecting files to rehash and update
- Handles source maps (
.mapfiles) automatically - Multiple passes ensure all references are updated correctly
Installation
npm install -g rehashOr use locally in your project:
npm install rehash --save-devUsage
rehash [options] [directory]Arguments
directory- Directory to process (defaults to current working directory). All globs are resolved relative to this directory.
Options
| Option | Short | Description | Default |
| ----------------- | ----- | ------------------------------------------------ | ------------------ |
| --rehash <glob> | -r | Glob pattern for files to rehash | See defaults below |
| --update <glob> | -u | Glob pattern for files to update with new hashes | See defaults below |
| --ignore <glob> | -i | Glob pattern for files to ignore | None |
| --help | -h | Show help message | N/A |
Default Behavior
If no --rehash nor --update options are provided, rehash uses sensible defaults:
Files to rehash:
**/*.css
**/*.eot
**/*.gif
**/*.ico
**/*.jpeg
**/*.jpg
**/*.js
**/*.json
**/*.png
**/*.svg
**/*.swf
**/*.ttf
**/*.wasm
**/*.webp
**/*.woff
**/*.woff2Files to update:
**/*.html
**/*.jsExamples
Basic usage (current directory)
rehashProcess a specific directory
rehash dist/Custom file patterns
# Only rehash JS and CSS files, don't update any files
rehash --rehash "**/*.js" --rehash "**/*.css"
# Rehash all assets and update HTML files
rehash --rehash "**/*.{js,css,png,jpg}" --update "**/*.html"
# Ignore certain files from rehashing and from updating
rehash --ignore "submodule/**"Multiple patterns
rehash \
--rehash "**/*.js" \
--rehash "**/*.css" \
--update "**/*.html" \
--update "**/index.js" \
--ignore "**/node_modules/**"How It Works
- Rehash Phase: Files matching the rehash glob patterns are renamed with a hash embedded in the filename (format:
filename.rehash--[hash]--rehash.extension) - Update Phase: Files matching the update glob patterns have their content scanned for references to the original filenames, which are replaced with the new hashed filenames
- Multiple Passes: The tool runs multiple passes until no more changes are detected, ensuring all references are properly updated
File Naming Format
When a file is rehashed, it follows this naming convention:
original-name.rehash--[sha256-hash]--rehash.extensionFor example:
script.js → script.rehash--a1b2c3d4e5f6...--rehash.js
image.png → image.rehash--f7e8d9c0b1a2...--rehash.pngSupported Reference Formats
The tool updates references in the following formats:
- Double quotes:
"file.js" - Single quotes:
'file.js' - Backticks:
`file.js` - Relative paths with or without
./prefix
FAQ
Rehash fails on my big complex project with many dependencies
Rehash is designed for simple static sites. It may not handle highly complex projects. You're welcome to open a PR if the fix is simple and doesn't introduce significant complexity, but such cases are generally out of scope for this tool.
I have dependency cycles in my project
Rehash will happily follow your dependency cycles indefinitely.
Can I use rehash with build tools like Webpack or Vite?
Please don't. These tools have their own asset handling and cache-busting mechanisms.
License
GNU Affero General Public License v3.0 - see LICENSE for details.
