/grunt-static-timestamp

Add a timestamp to the static files, but only if files have actually changed

Primary LanguageJavaScriptMIT LicenseMIT

grunt-static-timestamp

Add a timestamp to the static files, but only if they have actually changed

Getting Started

This plugin requires Grunt ~0.4.5

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 grunt-static-timestamp --save-dev

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

grunt.loadNpmTasks('grunt-static-timestamp');

Alternatively, you can automate the task loading with the load-grunt-tasks task.

The "static_timestamp" task

Overview

In your project's Gruntfile, add a section named static_timestamp to the data object passed into grunt.initConfig().(note the underscore instead of the dash):

grunt.initConfig({
    'static_timestamp': {
        your_target: {
        // Target-specific file lists and/or options go here.
        },
    },
});

The main idea of this task is this:

  1. We should have some other grunt task and/or tool that produce the "tentative build files" (or "temporary build files").

For instace, browserify or webpack could be called to produce the bundle file(s) of the application, or we could have the concat -> uglify -> compress tasks already defined.

The paths to these temporary build files should be given in the src property (see the example below).

  1. We give the directory where the temporary build files will be copied over (with a timestap prepended) in the dest property. This directory should be the one to be publicly exposed in the server.

  2. The temporary build files given in src are compared with their counterparts from dest (using the md5 hash). If they don't match the task assumes that the file src is more recent, so it is copied to dest with the current timestamp prepended. The old file is deleted.

Options

These task doesn't have any options. Everything is configured in the files property of the targets. See example below:

Usage Examples

grunt.initConfig({
    'static_timestamp': {
        myApp: {
            files: [{
                src: ['/path/to/build_files'], // can use globbing patterns
                dest: '/path/to/public_dir',
                filter: 'isFile'
            }]
        },

        myApp2: {
            files: [{
                src: ['/path/to/other/build_files'], 
                dest: '/path/to/public_dir', // doesn't have to be same
                filter: 'isFile'
            }]
        }
    },
});

Note that the files property must use the "files array format" (not the "compact format" nor the "files object format"). For more details see: http://gruntjs.com/configuring-tasks#files-array-format