Git Product home page Git Product logo

rollup-plugin-version-injector's Introduction

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.

rollup-plugin-version-injector's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar djhouseknecht avatar rednil avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.