Package detail

rollup-plugin-version-injector

djhouseknecht6.3kISC1.3.3

A simple rollup.js plugin to inject your application's version number and/or today's date into your built js, html, and css files!

rollup, rollupjs, rollup-plugin, version

readme

Build Status codecov npm version dependabot-status semantic-release Commitizen friendly

rollup-plugin-version-injector

A simple rollup.js plugin to inject your application's version number and/or today's date into your built js, html, and css files!

Getting started

Install from npm

npm i --save-dev rollup-plugin-version-injector

Add it to your rollup.config.js build configuration.

import versionInjector from 'rollup-plugin-version-injector';
// or
const versionInjector = require('rollup-plugin-version-injector');

export default {
  // other configuration
  plugins: [
    versionInjector()
    // any other plugins
  ]
};

Then you can add tags to your source code and it will inject the version where you specified. Example:

src/app.js (example file)

export class MyApp {
  getVersion() { 
    return '[VI]{version} - {date}[/VI]';
  }
  // other code
}

version-injector (VI) will replace that code in your built source to return the following:

build/built-app.js (example build file)

export class MyApp {
  getVersion() { 
    return '1.2.1 - June 9, 2007 17:46:12';
  }
  // other code
}

VI will also inject the version into your files as a comment. Example:

build/index.html (example file)

<!-- Version: 1.2.1 - June 9, 2007 17:46:12 -->
<!DOCTYPE html>
<html lang="en">
  ...
</html>

Basic Usage

VI has two uses -- it will Inject the version number and/or today's date into your generated files in:

  1. Defined tags in your source code.
  2. A comment at the top of the file.

VI will replace '{version}' with the version found in your package.json and '{date}' with today's date in the format specified in the configuration.

VI supports injecting the version into all js, html, and css files output by rollup.

Injected in the source code

VI will only look between the configured tagIds. For example, the default tagId is VI so VI is looking for:

// string in your javascript file
const VERSION = '[VI]Version: {version} - built on {date}[/VI]';

VI will only replace the '{version}' and '{date}' found within those tagIds.

// output after VI has run
const VERSION = 'Version: 1.2.1 - built on June 9, 2007 17:46:12';

You can change the date format, tagId, and which files to look in using the configuration object passed into the versionInjector() function.

As a comment

It will replace the '{version}' and '{date}' found in the configured tag. For example, the default configured tag is:

tag: 'Version: {version} - {date}'

You can change the date format, tag, and which files to inject the comment into using the configuration object passed into the versionInjector() function.

Configuration

Anything not specified/overwritten in your versionInjector() configuration will use the default configuration.

default config

{
  injectInComments: {
    fileRegexp: /\.(js|html|css)$/,
    tag: 'Version: {version} - {date}',
    dateFormat: 'mmmm d, yyyy HH:MM:ss'
  },
  injectInTags: {
    fileRegexp: /\.(js|html|css)$/,
    tagId: 'VI',
    dateFormat: 'mmmm d, yyyy HH:MM:ss'
  },
  packageJson: './package.json',
  logLevel: 'info',
  logger: console,
  exclude: []
};

All available date formats can be found at dateformat.

injectInTags

The following properties are available:

versionInjector({
  injectInTags: false /* or */ {
    // Regexp for files to match against
    fileRegexp: Regex 
    // string of the tagId to look for
    // Ex: 'ACK' => VI will look for '[ACK]...[/ACK]'
    tagId: string 
    // string of valid dateformat 
    dateFormat: string 
  }
})

Note: The tagId will be wrapped in opening and closing brackets. Example: [tagId][/tagId]

All available date formats can be found at dateformat.

injectInComments

The following properties are available:

versionInjector({
  injectInComments: false /* or */ {
    // Regexp for files to match against
    fileRegexp: Regex 
    // string of tag to be injected
    tagId: string 
    // string of valid dateformat 
    dateFormat: string 
  }
})

packageJson

This is the relative path to your package.json file from your rollup config file. Default is './package.json'.

logLevel

This is the log levels for verion-injector. Default value is 'info'. Available values are:

'debug', 'info', 'warn', 'error'

logger

Default is the console, but can be any logger you prefer that matches the ILogger interface.

excludes

This is an array of specific files you want to exclude from any processing. This must be the full file name without the path.

Credits

This is essentially a less fancy port of the webpack-auto-inject-version.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased

  • None

1.3.3 - 2021-04-16

Fixed

  • Issue #22: changed version injected into .js files to be multiline comments (/* */) to avoid issues with aggressive minifiers.

Security

  • Upgraded vulnerable dependencies

1.3.2 - 2021-02-10

Fixed

  • Issue #18: converted log() calls to info() calls to respect configured VI logLevel. Got tests back up to 100%.

1.3.1 - 2021-02-10

Fixed

  • Issue #17: corrected rollup warning about source maps not being accurate. VI does not need to generate source maps. Updated VI config to tell rollup that existing source maps are still valid.

1.3.0 - 2020-12-09

Fixed

  • Issue #10: work with chunks and output folders

Added

  • Upgraded to rollup v2

Security

  • Upgraded vulnerable dependencies

1.2.2 - 2020-04-24

Fixed

  • Issue #2: updated file regex to not be global

1.2.1 - 2020-04-24

Security

  • Upgraded vulnerable dependencies

    Added

  • semantic-release, travis, and commitizen for easier maintainability

1.2.0 - 2020-02-03

Security

  • Upgraded vulnerable dependencies

    Changed

  • Changed default DateTimeStamp format from June 9, 2007 to June 9, 2007 17:46:12 dateformat's 'longDate' to 'mmmm d, yyyy HH:MM:ss'
  • README examples to flow better for quicker setup

1.1.3 - 2019-08-27

Security

  • Upgraded vulnerable dependencies

1.1.2 - 2019-07-25

Added

  • Pre-push hooks

    Fixed

  • Typos in README

    Security

  • Upgraded vulnerable dependencies

1.1.0 - 2019-06-28

Added

  • Usage, configuration, and instructions to README
  • Unit tests

    Changed

  • Separated code into multiple files

1.0.0 - 2019-06-26

Added

  • Initial release with working source code