@druidvav/grunt-assets-versioning

1.1.1 • Public • Published

grunt-assets-versioning Build Status NPM version

![Gitter](https://badges.gitter.im/Join Chat.svg)

Versioning static assets with Grunt

Getting Started

This plugin requires Grunt ~0.4.1

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install @druidvav/grunt-assets-versioning --save-dev

One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('@druidvav/grunt-assets-versioning');

The "assets_versioning" task

Overview

In your project's Gruntfile, add a section named assets_versioning to the data object passed into grunt.initConfig().

grunt.initConfig({
  assets_versioning: {
    options: {
      // Task-specific options go here.
    },
    your_target: {
      options: {
        tasks: ['uglify:targetTask']
      }
    },
  },
})

This task will version the files output by the task uglify:targetTask.

Options

options.tag

Type: String Possible values: date, hash Default value: hash

Should the revision marker be a md5 hash or a date ?

options.hashLength

Type: Integer Default value: 8

If you choose to version your files using a hash, hashLength let you set how long the hash is going to be.

options.dateFormat

Type: String Default value: YYYYMMDDHHmmss

If you choose to version your files using a date, you can specify a dateformat. grunt-assets-versioning is using moment.js to format date.

options.timezoneOffset

Type: Number Default value: 0

Only works if you choose to version your files using a date. Timezone offset (in hours) to take into account when generating the date version tag. By default, set to 0 (GMT time).

options.tasks

Type: Array or Boolean Default value: false

The tasks you want to run while versioning their destination files.

options.versionsMapFile

Type: String Path to the file in which the versions map will be dumped.

The assets versioning files outputs a map of versions of all the files processed. Here's how that map looks like:

[
  {
    version: '3d04f375',
    originalPath: 'path/to/bundle-a.js',
    versionedPath: 'path/to/bundle-a.3d04f375.js'
  },
  {
    version: '92jdi2j1',
    originalPath: 'path/to/bundle-b.js',
    versionedPath: 'path/to/bundle-b.92jdi2j1.js'
  }
]

By default you can retrieve the map of versions by accessing this configuration variable. grunt.config('assets_versioning.yourTask.versionsMap') The versionsMapFile gives you the possibility to also output that map to a file.

options.versionsMapTemplate

Type: String Default value: null

Path to a lodash template file that is going to be used to generate the versions map file (options.versionsMapFile)

By default, when no template is indicated, the task will output a json file.

The lo-dash template may reuse the keys from the version maps (version, originalPath, versionedPath). Here's an example of a lo-dash template to generate a php dictionary.

<?php

class MyDict
{
  public static $myDict = array(
<% _.forEach(files, function(file) { %>
    "<%= file.originalPath %>" => "<%= file.versionedPath %>",
<% }); %>
  );

options.versionsMapTrimPath

Type: String This gives you the possibility to trim the path output in the version map.

For example, if you set options.versionsMapTrimPath to be 'super/long/path/to/', instead of getting this map:

[
  {
    version: '3d04f375',
    originalPath: 'super/long/path/to/bundle-a.js',
    versionedPath: 'super/long/path/to/bundle_a.3d04f375.js'
  },
  {
    version: '92jdi2j1',
    originalPath: 'super/long/path/to/bundle-b.js',
    versionedPath: 'super/long/path/to/bundle-b.92jdi2j1.js'
  }
]

you will get this one:

[
  {
    version: '3d04f375',
    originalPath: 'bundle-a.js',
    versionedPath: 'bundle_a.3d04f375.js'
  },
  {
    version: '92jdi2j1',
    originalPath: 'bundle-b.js',
    versionedPath: 'bundle-b.92jdi2j1.js'
  }
]

options.skipExisting

Type: Boolean or Array Default value: false

If true, will skip the task if the destination file already exists. If type Array, will skip the task if the destination file, once versioned, is listed in the array.

options.post

Type: Boolean Default value: false

By default, the grunt-assets-versioning task uses the content of the source files to create a version hash. Combined with the skipExisting option, it allows to speed up the deployment process by skipping tasks that would output an already-existing destination file.

If ever you're trying to version a task that doesn't expose all its source files but only an entry point (less, requirejs), you should set the post options to true.

less: {
  production: {
    files: {
      "path/to/result.css": "path/to/source.less"
    }
  }
},
assets_versioning: {
  css: {
    options: {
      post: true,
      tasks: ["less:production"]
    }
  }
}

Usage Examples

Versioning using a hash

In this example, dest.bundle.js is going to be versioned with a hash. All sources files are going to be hashed and those hashes are also going to be hashed. The generated result should be dest/bundle.2j4h2kds.js

grunt.initConfig({
  assets_versioning: {
    options: {
      tag: 'hash',
      hashLength: 6,
    },
    files: {
      'dest/bundle.js': ['src/file1.js', 'src/file2.js'],
    },
  },
})

Versioning using a date

In this example, dest.bundle.js is going to be versioned with a date, using the default format YYYYMMDDHHmmss. The newest modification dates of all src files is going to be used to create this timestamp. The generated result should be dest/bundle.20130413004500.js

grunt.initConfig({
  assets_versioning: {
    options: {
      tag: 'date',
    },
    files: {
      'dest/bundle.js': ['src/file1.js', 'src/file2.js'],
    },
  },
})

Readme

Keywords

Package Sidebar

Install

npm i @druidvav/grunt-assets-versioning

Weekly Downloads

16

Version

1.1.1

License

none

Unpacked Size

68.7 kB

Total Files

36

Last publish

Collaborators

  • druidvav