Package Exports
- @toolbuilder/rollup-plugin-commands
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@toolbuilder/rollup-plugin-commands) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Rollup Plugin Commands
Configurable Rollup plugin to run async functions in sequence. Also provides a shell command for convenience.
Features:
- Runs one or more commands in sequence, waiting for each to finish
- Run commands when
generateBundleorwriteBundleis called as specified by options - Each command is passed the parameters
outputOptionsandbundlethat are passed to the plugin - Run commands only once, or each time
generateBundleorwriteBundleis called - Run async or synchronous functions
- Run shell commands with parameters in a single string (i.e. 'npm test'), courtesy execa and cross-spawn.
Installation
Using npm:
npm install --save-dev @toolbuilder/rollup-plugin-commandsUse
If you want just the plugin, but not the shellCommand, import like this:
import runCommands from '@toolbuilder/rollup-plugin-commands'If you want the shellCommand, import like this:
import runCommands, { shellCommand } from '@toolbuilder/rollup-plugin-commands'Alternately, import like this if you don't like the syntax above:
import { runCommands, shellCommand } from '@toolbuilder/rollup-plugin-commands'Example
The Rollup config rollup.test.config.js tests the pack file for this package. It uses @toolbuilder/rollup-plugin-commands to install dependencies and run the tests in a temporary directory.
shellCommand
This function creates an async function that executes a shell command with option { stdio: 'inherit' } so that the output is part of the Rollup stream. It uses execa to execute the script. If the script does not return 0, an exception is thrown.
Typical use of shellCommand looks like this:
import runCommands, { shellCommand } from '@toolbuilder/rollup-plugin-commands'
export default [{
/* other configuration here */
plugins: [
runCommands({
commands: [
shellCommand(`npm run something`) // runs when 'writeBundle' is called
]
})
]
}]Options
The plugin does nothing without options.
commands
- Type:
[Function|AsyncFunction] - Default:
[]
The commands option specifies the commands you want to run. They are run in the sequence provided, and each function must finish before the next function is called. Each function is called with the outputOptions and bundle parameters passed to the plugin. If a function throws, the thrown value is passed to Rollup's context function (i.e. this.error(yourError)) so that Rollup reports it normally. However, the command is not passed Rollup's context object - you might as well write a plugin if you want that.
If you want to run async functions in parallel, just call them from sync functions that do not return the Promise.
const options = {
commands: [
async (outputOptions, bundle) => "do something",
() => "do something else next"
]
}
runOn
- Type:
String - Default:
writeBundle
Set the runOn option to generateBundle if you want to run the commands when that method is called. Depending on the option value, the commands will be run either when writeBundle is called or when generateBundle is called - not both times. If you want to run for both writeBundle and generateBundle, use two plugin instances.
const options = {
runOn: 'generateBundle' // tells plugin to run commands when generateBundle is called
}once
- Type:
Boolean - Default:
true
Set this option to false if you want to run the commands every time the function you selected (either generateBundle or writeBundle) is called.
const options = {
once: false
}Contributing
Contributions are welcome. Please create a pull request or write up an issue. This package uses the pnpm package manager. Run pnpm check to run all the unit tests and validation scripts.
Issues
This project uses Github issues.
License
MIT