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

rollup-plugin-git-info

v1.0.0

Published

Rollup plugin to export git/build information

Downloads

423

Readme

rollup-plugin-git-info

Plugin for including git version information into package.json imports.

Requirements

This plugin requires:

  • Node v12+ (the latest LTS or newer)
  • Rollup v2+ (tested against 2.32.0+)

Install

Using npm:

npm install rollup-plugin-git-info --save-dev

Usage

In your rollup.config.js, import the plugin:

import gitInfo from 'rollup-plugin-git-info';

export default {
  input: 'src/index.js',
  output: {
    dir: 'dist',
    format: 'es',
  },
  plugins: [gitInfo()],
};

Then in your source code, import the package.json file, and use the fields:

// src/index.js
import pkg from './package.json';
console.log(`running version ${pkg.version}`);

Then use rollup like normal!

Fields

The following fields are injected into the JSON import.

Note: version is updated by default to equal gitVersion.

| Field | Meaning | Example | | --------------- | -------------------------------- | ------------------------------------------ | | buildDate | Date the bundled was made. | Mon, 26 Oct 2020 06:58:09 GMT | | gitAbbrevHash | Abbreviated (short) git hash. | f347dcd | | gitBranch | Active git branch. | main | | gitCommitHash | Full git hash. | f347dcd8c8c7b5923fd8459eaf5fddc44f31acc6 | | gitDate | Latest commit date. 1 | Sat, 24 Oct 2020 01:32:35 -0400 | | gitVersion | SemVer version with git info. | 1.0.0-main+gf347dcd |

1: By default, the git date is the git commit date which comes from the git commit itself and might not be the same as the author date or when the commit was actually pushed to the branch.

Options

Since this loads a JSON file using the rollup-plugin-json package, all of its options are respected here too. They aren't documented here though, so see that plugin's documentation.

abbrev

Type: Number Default: 7

How many leading characters to use when creating gitAbbrevHash.

branchCommand

Type: String Default: rev-parse --abbrev-ref HEAD

The git command to display the active branch. If no branch is active (i.e. a detached head due to checking out a specific commit), then this will simply be HEAD.

commitHashCommand

Type: String Default: rev-parse HEAD

Git subcommand to extract the commit hash from the repository.

cwd

Type: String Default: undefined

The directory to run git commands under. By default, git information is extracted from the directory where the imported package.json file lives.

dateFormat

Type: String Default: %cD ("git commit date, RFC2822 style")

The git pretty format for extracting the date of the git repository. This is appended to dateCommand to form the complete git command, but is broken out for convience as most people only want to tweak the format.

Another common option is %aD for the RFC2822 git author date.

If none of the stock formats work for you, consider using %ad or %cd with --date=<format>. In particular, the --date=format:... gets access to strftime.

dateCommand

Type: String Default: log -1 --format=

The git command to extract the commit date. The dateFormat option is appended to this command first to control which date field to extract from the git repository.

enableBuildDate

Type: Boolean Default: false

Whether to export buildDate at all.

This can be useful for development, but it can also be detrimental to caching and reproducible builds. Hence it is disabled by default.

transformFilename

Type: String Default: package.json

The JSON file to inject git fields into when importing.

This might be useful if, for some reason, you want to keep the standard package.json content unmodified, and place all the generated git fields in a unique file/namespace.

updateVersion

Type: Boolean Default: true

Whether to set the package.json's version to the generated gitVersion.

Note: The gitVersion is always available separately, so this is more of a convience of just referring to pkg.version everywhere.

versionFormat

Type: String Default: [version]-[branch]+g[abbrevHash]

The format of the generated gitVersion.

It is strongly recommended to keep it SemVer compliant.

Note: The g prefix (short for "git") on the commit hash is a standard convention in the git world when using commit hashes in version strings.

Available placeholders (see the Fields section for details):

  • abbrevHash: Abbreviated commit hash.
  • commitHash: Full commit hash.
  • branch: Git branch.
  • version: The input file's version field (or 0.0.0 if unavailable).