1.0.0 • Published 4 years ago
metalsmith-select v1.0.0
metalsmith-select
A metalsmith plugin for conditionally running plugins based on modes.
This plugin allows the developer to conditionally choose which plugins run based on developer defined modes.
Installation
npm install metalsmith-select
Usage
Below is a basic example of how to use this plugin to conditionally select which plugins will run in each of the developer defined modes.
const Metalsmith = require('metalsmith');
const select = require('metalsmith-select');
// Setting a mode. You can create any modes you like,
// it's up to the developer to define modes.
const mode = 'development';
// Creating a plugin to run in development
function devPlugin(files, metalsmith, done) {
setImmediate(done);
console.log('Running in development mode');
}
// Creating a plugin to run in production
function prodPlugin(files, metalsmith, done) {
setImmediate(done);
console.log('Running in production mode');
}
Metalsmith(__dirname)
// Conditionally selects which plugin to run based
// on which mode is chosen above. In this case, the
// `development` mode has been chosen, so only the
// `devPlugin` will run. If you change the mode to
// `production` above, then the `prodPlugin` will run.
// Finally, note the `testing` key contains a string
// to an existing mode. If you run in `testing` mode
// in this case, the `development` mode plugin will
// run, as the testing mode configuration has been
// linked to the development mode.
.use(select(mode, {
development: devPlugin,
production: prodPlugin,
testing: 'development',
}))
// Since we are in `development` mode and it is not
// found in the list of modes here, `select()` will
// essentially return an empty plugin (no operation),
// effectively skipping this `.use()` call entirely.
.use(select(mode, {
nonexistentMode: devPlugin,
}))
.build((err, files) => {
if (err) { throw err; }
});
License
1.0.0
4 years ago