npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

metalsmith-incremental

v0.1.1

Published

Faster incremental builds for MetalSmith

Downloads

17

Readme

metalsmith-incremental

Faster incremental builds for MetalSmith

Install

npm i metalsmith-incremental

Usage

  1. Find out which is the slow part of your build. Hint: Go and checkout metalsmith-timer This will give you insights in your concrete bottleneck.

  2. Wrap your plugin middleware with metalsmith-incremental, like:

import metalsmith from 'metalsmith'
// import any plugins here
// ...
import incremental from 'metalsmith-incremental'

// filter unmodified files
metalsmith.use(incremental({ plugin: 'filter' }))
// run slow plugins
metalsmith.use(slowPlugin())
// restore unmodified files
metalsmith.use(incremental({ plugin: 'cache' }))

// optionally enable watching
if(process.env.NODE_ENV === 'development') {
  metalsmith.use(incremental({ plugin: 'watch' }))
}

// in case you have restored all files with cache plugin
// call filter plugin as last middleware
metalsmith.use(incremental({ plugin: 'filter' }))

// build metalsmith
metalsmith.build((err) => {
  if (err) throw err
})
  1. In case your plugin wraps content which could include other content (dependencies), you can specify custom RegExp or Function, which should extract those depended files and occasionally rebuild them too (.jade and .pug is supported by default).
// dependencies with RegEx
metalsmith.use(incremental({
  depResolver: /^import ["'](.*)['"]$/mg
}))
metalsmith.use(slowPlugin())

Important: Your RegEx has to define one capturing group (which holds the dependency path data), match global and multiline.

// dependencies with Function
metalsmith.use(incremental({
  depResolver: (file, baseDir) => {
    const dependencies = []
    // do your custom magic to find dependencies
    return dependencies
  }
}))
metalsmith.use(slowPlugin())

Note: You can also pass a hash of RegEx or Function by file extension.

  1. Don't forget to enable file watching (if your are in dev mode)
// optionally enable watching
if(process.env.NODE_ENV === 'development') {
  metalsmith.use(incremental({ plugin: 'watch' }))
}
  1. Make sure to write only modified files to disk, by calling filter at the last middleware
metalsmith.use(incremental({ plugin: 'filter' }))

Important: This plugin is designed to be used only with MetalSmith plugins who operate on file basis. Other plugins who depend on metadata, etc may break.

Edge Cases

Special circumstances like dependencies, plugins renaming, deleting, adding files should be considered carefully.

Dependencies

Let's consider you are using a template engine like PugJS and you are changing a partial, mixin or extended layout. This means each file which includes those dependencies needs to be rebuild too, even if they did not change itself.

To solve these you have basically two methods to chose from:

Note

The Paths-Map makes especially sense if you remove some files by metalsmith-branch or metalsmith-ignore temporarily from the pipeline (which makes them unavailable for dependency resolver) but still want to trigger updates on other files if one of those ignored files has changed.

Dynamic Dependencies

If you dynamically include dependencies then your best bet is again Paths-Map config, or your write your own very clever dependency resolver function.

Renaming

If you are using any plugin like metalsmith-markdonw or any template engine like PugJS it's very likely that the original file extension changes from .md or .pug to .html.

To solve these just let the cache plugin know those renaming rules:

Circular Dependencies and metadata

We recommend to always build metadata from scratch. But if you really have an intensive metadata plugin. You can force updates of file's metadata (not global metadata):

Trouble with metalsmith-collections?

Check https://github.com/segmentio/metalsmith-collections/issues/27

API

Check our API documentation.

Inspiration

After we had very long metalsmith builds during development, it was time to seek for change. We have found this inspiring blog post http://www.mograblog.com/2016/11/speed-up-metalsmith.html. Though it was far from complete, not mentioning circular references, dependencies, metadata and more very specific stuff, we decided to take the next step.