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

grunt-version-assets

v1.1.0

Published

Use node-version-assets to rename static assets and update references

Downloads

861

Readme

grunt-version-assets

This Grunt plug-in renames static assets (JavaScript files, CSS, ...) and at the same time updates references to these assets in other files (for example, HTML files). This enables you to deliver static assets with HTTP caching headers telling the browser to cache them forever, so the file will only be downloaded once. If the file changes, its file name changes and the new version will be downloaded, so users never see stale JavaScript or CSS files.

  • Uses node-version-assets.
  • The md5hash of the file content will be added to the assets's file name. (app.min.js -> app.min.44d0440440442524c6d667900275e.js)
  • Unchanged assets will keep the same file name and not blow the browser cache.
  • Greps your HTML and other files and updates filenames of versioned files.

Getting Started

This plugin requires Grunt.

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-version-assets --save-dev

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

grunt.loadNpmTasks('grunt-version-assets');

The "versioning" task

Overview

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

grunt.initConfig({

  versioning: {
    options: {
      grepFiles: [
        'public/**/*.html',
      ]
    },
    js: {
      src: [
        'public/js/app.min.js',
        'public/js/bundle.min.js',
      ]
    },
    css: {
      src: [
        'public/css/master.css',
        'public/css/bundle.min.js',
      ]
    },
  },

});

Options

options.grepFiles

Type: An array of file names, paths and patterns. Default value: []

The list of files in which the plug-in replaces/updates the references to the asset files.

options.keepOriginal

Type: boolean Default value: true

If true, the original source file will be kept, otherwise it will be deleted.

More options

node-version-assets actually offers a number of other options, that have not been implemented in this plug-in. If you need any of these options, file an issue or better yet, a pull request (delegating options to node-version-assets is actually trivial).

Usage Examples

Basic Usage

In this example, a copy of the file public/js/app.js is created with a versioned file name (something like public/js/app.17781b077d5a9c60c6504e4d1467e2b0.js). The same is done for public/css/app.css. The file index.html is searched for references to app.js and app.css. The references will be updated to match the renamed files. The original files (app.js and app.css) will not be deleted. If there are versioned asset files from a former run of this plug-in, those will be deleted.

grunt.initConfig({
  versioning: {
    options: {
      grepFiles: [
        'public/index.html',
      ]
    },
    app: {
      src: [
        'public/js/app.js',
        'public/css/app.css'
      ]
    },
})

Multiple Assets Sources

In this example, a all JavaScript files under public/js are renamed to a versioned file name (something like public/js/whatever.17781b077d5a9c60c6504e4d1467e2b0.js). The same is done for all CSS files under public/css. A couple of HTML files are searched for references to the renamed asset files. The references will be updated to match the renamed files. The original asset files will be deleted. If there are versioned asset files from a former run of this plug-in, those will also be deleted.

grunt.initConfig({
  versioning: {
    options: {
      grepFiles: [
        'public/index.html',
        'public/index2.html',
        'public/pages/*.html',
      ],
      keepOriginal: false
    },
    js: {
      src: [
        'public/js/**/*.js'
      ]
    },
    css: {
      src: [
        'public/css/**/*.css'
      ]
    },
})

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • 2014-11-08: 1.1.0 - Bubble up errors from node-version-assets
  • 2014-11-02: 1.0.0 - Initial Release

License

Copyright (c) 2014 Bastian Krol. Licensed under the MIT license.