Giter Club home page Giter Club logo

metalsmith-defile's Introduction

metalsmith-defile

This plugin is for metalsmith plugin developer not users of metalsmith (though you may be both).

Metalsmith-Defile (henceforth defile) is middleware that let's you write your plugin as a pure function rather than by mutating files and metalsmith in place. You can then call defile on your pure function to make it compatible with the Metalsmith API. This is primarily useful if you are writing in a functional style using something like ramda.js.

This is best shown by example. You would write:

const defile = require("metalsmith-defile")

const myPlugin = options => (files, metalsmith, done) => {
  done({ files: "bar", metalsmith })
}

module.exports = defile(myPlugin)

instead of:

const myPlugin = options => (files, metalsmith, done) => {
  files.foo = "bar"
  done()
}

module.exports = myPlugin

Details

If you don't pass files and/or metalsmith, the original object is passed through. This object may have been mutated by the plugin. This means that defiling an existing (impure) plugin should have no effect at all.

// These are equivalent
options => (files, metalsmith, done) => {
  done({ files: "bar", metalsmith })
}

options => (files, metalsmith, done) => {
  done({ files: "bar" })
}

To delete a key/value pair on an object, omit it from the object you return. To remove all files, you would pass { files: {} }.

Testing

By default defile does not throw if you mutate in place. Because this throwing may be desirable in the development process, you can enable it by passing { throwOnMutation: true } to defile as a second argument. Enabling this behavior isn't free however, so I discourage you from shipping your plugin with it on.

// myPlugin.js
const myPlugin = options => (files, metalsmith, done) => { done() }

module.exports = defile(myPlugin)
module.exports.pure = myPlugin


// myPlugin.test.js
test("throws if files are directly mutated", t => {
  const defiled = defile(myPlugin.pure, { throwOnMutation: true })

  t.throws(() => {
    defiled()({}, {}, () => {})
  }, TypeError)
})

metalsmith-defile's People

Contributors

henrymarshall avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.