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

@fidian/metalsmith-sugar

v1.8.0

Published

Additional helpers to make your Metalsmith experience even sweeter.

Downloads

142

Readme

Metalsmith Sugar

Syntactic sugar to make it sweeter to use Metalsmith.

This is not a plugin! Instead, you use this to load other plugins.

npm version Build Status Dependencies Dev Dependencies codecov.io

Overview

This is easier to describe in code.

// Before:
var Metalsmith = require("Metalsmith");
var myPlugin = require("metalsmith-my-plugin");
var metalsmith = new Metalsmith(__dirname);
metalsmith.use(myPlugin({
    configuration: true
}));

// After:
var sugar = require("metalsmith-sugar")();
sugar.use("metalsmith-my-plugin", {
    configuration: true
});

This handles the loading of Metalsmith for you and also makes it easier to use many plugins in your build without having to call require() on each of them. You still maintain full control over all of the aspects of the build and you can skip tedium. One does not need to require modules to capture the factory functions and then call the factory.

All of the typical Metalsmith configuration is supported. The exported factory can accept several options. These names are essentially all taken directly from how you would initialize Metalsmith on your own. If you define any of the properties, they will be sent along to Metalsmith as expected. Many of these should work as-is without being defined in your project.

var sugar = require("metalsmith-sugar")({
    clean: true,
    concurrency: Infinity,
    destination: "build",
    directory: __dirname, // Defaults to module's `${__dirname}/../../..`
    frontmatter: true,
    metalsmith: require("metalsmith"), // Lets you override the constructor
    source: "src"
});

In addition, the metalsmith-timer module is added for each plugin so you are able to get detailed timing information. Three other events are logged under the metalsmith-timer debug flag; the completion of loading the JavaScript, the time it takes to read all source files and when the build finishes. You enable debug by setting an environment variable during the build. Assuming you have a "build" script in your package.json, the command could look like this.

DEBUG=metalsmith-timer npm run build

Use With metalsmith-debug-ui

There's a beautiful UI that can help follow your files and their metadata during your build process. It's part of metalsmith-debug-ui and it simply builds a bunch of extra files in the ${destination}/debug-ui/ folder. You can easily use metalsmith-sugar with this wonderful plugin. In fact, it is even a little sweeter to use these together because the middleware functions will have reasonable names assigned and your UI will look better.

var sugar = require("metalsmith-sugar")({
    // configuration goes here for metalsmith-sugar
});

// This line is what you need to get the debug UI to work really well
// with metalsmith-sugar.
require("metalsmith-debug-ui").patch(sugar.metalsmith());

Installation

Use npm to install this package easily.

$ npm install --save metalsmith-sugar

Alternately you may edit your package.json and add this to your dependencies object:

{
    ...
    "dependencies": {
        ...
        "metalsmith-sugar": "*"
        ...
    }
    ...
}

This already depends on the metalsmith module so you don't need to list it separately in your package.json file.

API

The factory accepts configuration. All of these properties are optional. What you see below are the default options. Many of these are the defaults from Metalsmith itself.

var sugar = require("metalsmith-sugar")({
    // If true, cleans the build directory before running a build.
    // When false, does not clean and just overwrites as needed.
    // Boolean. Passed to metalsmith.clean().
    clean: true,

    // If set, limits the maximum number of concurrent file reads that
    // happen. Number. Passed to metalsmith.concurrency().
    concurrency: Infinity,

    // This is where the generated files are placed after the build.
    // It is relative to the .directory property. String. Passed to
    // metalsmith.destination().
    destination: "build",

    // The root of the project. This holds the source and destination
    // directories. String. Passed to the Metalsmith constructor.
    // The default value here should automatically find the project's
    // root folder. If not, please set this to `__dirname` or any other
    // working value for your structure.
    directory: path.resolve(__dirname, "..", "..", ".."),

    // Enable or disable frontmatter loading. Boolean. Passed to
    // metalsmith.frontmatter().
    frontmatter: true,

    // The Metalsmith object constructor. If you want to make an instance
    // of another object, override this constructor. Function.
    metalsmith: require("metalsmith"),

    // The folder containing the source files. String. Passed to
    // metalsmith.source().
    source: "src"
});

This factory creates a Metalsmith instance and configures it. A metalsmith-timer message is added indicating when all of the source files have been loaded.

The factory returns a sugar object with the following methods.

sugar = sugar.build([callback])

Run a build, nearly identical to metalsmith.build(). If no callback function is passed, this will create a callback that will simply throw if there are any errors.

The build will also add an event to metalsmith-timer showing when all of the JavaScript modules have been loaded and another event when the build completes successfully.

metadata = sugar.metadata()

Calls metalsmith.metadata() and returns the metadata object. Simply a convenience function to have the sugar operate more like Metalsmith.

metalsmith = sugar.metalsmith()

Returns the Metalsmith instance being used. It should be considered a bug if you need to call this method because the sugar module isn't doing enough work for you.

sugar = sugar.use(middlewareFunction)

sugar = sugar.use(middlewareString, arguments...)

The first syntax is just like how you would call metalsmith.use(). The second is the sugar version that will call require() on your behalf and pass along extra arguments to the module's factory. There's an example at the top explaining how to use this.

In addition to loading the module, calls to metalsmith-timer will indicate when the plugin has finished. This will say the middleware's name when you pass a module name (string). When you pass a middleware function, the timer message will say the function's name when available, or indicate that it is an anonymous function. With this extra logging you can determine how long it takes each plugin to run.

License

This software is licensed under a MIT license that contains additional non-advertising and patent-related clauses. Read full license terms