Feature: Allow metalsmith methods to be destructured

Author: webketjeCreated Dec 29, 2021Updated Sep 5, 2023
Labelsstage:refinetype:feature

Please add a thumbs up if this is important to you

Allow metalsmith methods to be destructured like

javascript
const { use, build } = require('metalsmith')(__dirname)

Currently this is not possible as the this context is not bound to destructured methods, it is inherited from the executing context, in Node's case, module

Binding the methods automatically in the constructor effectively makes the prototype-sharing of the methods useless. That opens up the path for using the factory-pattern to replace the module.exports function, or alternatively rename it to Metalsmith.create.

Modern (& TS-enabled) packages often allow access to enums & "static class methods" (~ functions that are properties of a constructor) like const { Metalsmith } = require('metalsmith'). Returning Metalsmith.create in 2.4 could be a bridge to that path. But binding the methods as an opt-out (for the purists among us) in a factory func (in the current codebase Metalsmith func is actually a factory + a constructor in 1) looks cleaner as it allows the Metalsmith prototype to remain pure