Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

yarn-plugin-lifecycle-hooks

Yarn Berry plugin that restores npm-style pre<script> and post<script> lifecycle hooks.

Yarn Berry (v2+) intentionally dropped automatic execution of pre/post scripts. This plugin brings them back.

Install

From GitHub (recommended)

yarn plugin import https://raw.githubusercontent.com/RoiDayan1/yarn-plugin-lifecycle-hooks/main/index.cjs

This downloads the plugin into .yarn/plugins/ and updates .yarnrc.yml automatically.

From a local file

yarn plugin import ./index.cjs

Usage

Once installed, define pre<script> and post<script> entries in your package.json and they will run automatically:

{
  "scripts": {
    "prebuild": "echo 'before build'",
    "build": "tsc",
    "postbuild": "echo 'after build'",
    "pretest": "echo 'before test'",
    "test": "jest",
    "posttest": "echo 'after test'"
  }
}
yarn build
# Runs: prebuild → build → postbuild

yarn test
# Runs: pretest → test → posttest

yarn build --watch
# Runs: prebuild → build --watch → postbuild
# Arguments are forwarded only to the main script

How it works

  • Before the main script runs, if pre<scriptName> exists in package.json, it is executed.
  • After the main script succeeds, if post<scriptName> exists, it is executed.
  • CLI arguments are forwarded only to the main script — pre/post hooks receive no arguments.
  • If a pre-script or the main script fails (non-zero exit), execution stops and the exit code is propagated.
  • Built-in lifecycle scripts that Yarn already handles (preinstall, postinstall, prepublish, etc.) are skipped to avoid double-firing.
  • Works with colon-separated script names too (e.g. premy:task / postmy:task).

Uninstall

Remove the plugin entry from .yarnrc.yml and delete the file from .yarn/plugins/.

License

MIT

About

Yarn Berry plugin that restores npm-style pre/post lifecycle script hooks

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages