A Metalsmith plugin to cache build results for later runs.
The plugin identifies repeated builds by generating a checksum of all file names and file contents—so if no input files have changed, then it will skip the build process and use the previously generated files.
Caching the build process is particularly useful when breaking up your build into multiple stages such as:
- Process all CSS (
@metalsmith/sass
,@metalsmith/postcss
, etc.) - Process all JavaScript (
metalsmith-uglify
, etc.) - Process all images (
metalsmith-gravatar
,metalsmith-img-sharp
, etc.) - Process all Markdown (
@metalsmith/collections
,@metalsmith/permalinks
,@metalsmith/markdown
, etc.) - Combine the output of everything above
npm install --save metalsmith-build-cache
metalsmith-build-cache
wraps the root metalsmith
call:
import Metalsmith from 'metalsmith';
import cache from 'metalsmith-build-cache';
cache.metalsmith(Metalsmith(__dirname), {
// options here
})
// .use() every build plugin
.build((err) => {
if (err) {
throw err;
}
});
Type: string
Default: .cache/<destinationDir>
The logger function.
As mentioned, this plugin is particularly powerful when breaking up your build into multiple stages. Example:
import Metalsmith from 'metalsmith';
import sass from '@metalsmith/sass';
import postcss from '@metalsmith/postcss';
import sharp from 'metalsmith-img-sharp';
import include from 'metalsmith-include-files';
// If no files in the ./css directory change then the cached result can be used!
await cache.metalsmith(Metalsmith(__dirname))
.source('css')
.destination('build-css')
.use(sass({
style: 'expanded'
}))
.use(postcss({
plugins: {
autoprefixer: {}
}
}))
// If no files in the ./img directory change then the cached result can be used!
await cache.metalsmith(Metalsmith(__dirname))
.source('img')
.destination('build-img')
.use(sharp({
methods: [
// Sharp processing steps...
]
}))
.build();
await Metalsmith(__dirname)
.source('src')
.destination('build')
.use(include({
directories: {
'static/css': ['build-css/**'],
'static/img': ['build-img/**']
}
}))
// All the rest of your plugins...
.build();