Giter VIP home page Giter VIP logo

grunt-userevvd's Introduction

grunt-userevvd Build Status NPM version

Replaces <script>, <link>, and <img> HTML tags with their rev'd version for client and edge server caching.

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-userevvd --save-dev

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

grunt.loadNpmTasks('grunt-userevvd');

The "userevvd" task

This is a companion task for grunt-filerev. You must run the filerev task before running the userevvd task.

grunt-userevvd is a very lightweight and fast plugin that replaces the src/href attribute value for <script> or <link> tags to their revv'd version.

The task reads the summary grunt.filerev.summary object that grunt-filerev creates. It replaces all the references to the newly revv'd files in the .html files you specify.

Overview

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

An html task name replaces script and link references in .html files. Currently only .html files are supported, but .css will come in the future.

You should use the files array format.

grunt.initConfig({
  userevvd: {
    html: {
      files: [
        {
          cwd: 'dist/',
          src: '**/*.html',
          expand: true,
          dest: 'dist'
        }
      ]
    },
  },
});

Options

####options.formatNewPath (optional)

A function that formats the new src or href value for the script/link tag. The argument is passed a string containing the original path from the grunt.filerev.summary object.

####options.formatOriginalPath (optional)

A function that formats the original file path from grunt-filerev. This plugin will look for the a tag with the return value from this function to replace.

Usage Examples

Assuming you have a directory struture like this:

|
+- dist
|    +- assets
|         +- js
|              +- main.js
|    +- index.html

The contents of dist/index.html are:

<!doctype html>
<html>
  <head>
    <script src="dist/assets/js/main.js"></script>
  </head>
</html>

With the following Grunt configuration:

grunt.initConfig({
  filerev: {
    dist: {
      src: 'dist/assets/js/**/*.js'
    }
  }
  userevvd: {
    html: {
      options: {
        formatPath: function(path){
          return path.replace(/^dist/, 'https://cdn.domain.com');
        }
      },
      files: [
        {
          cwd: 'dist/',
          expand: true,
          src: '**/*.html',
          dest: 'dist'
        }
      ]
    }
  },
});

grunt-filerev will add a hash to the main.js file name:

|
+- dist
|    +- assets
|         +- js
|              +- main.k8dj3h45.js
|    +- index.html

And grunt-userevvd will change the reference to main.js in dist/index.html:

<!doctype html>
<html>
  <head>
    <script src="https://cdn.domain.com/assets/js/main.k8dj3h45.js"></script>
  </head>
</html>

If the formatPath option were ommitted, dist/index.html would look like this:

<!doctype html>
<html>
  <head>
    <script src="dist/assets/js/main.k8dj3h45.js"></script>
  </head>
</html>

Summary

Just like grunt-filerev, grunt-userevvd produces a grunt.userevvd.summary object containing the original path and the new path for each file. Given the example above, the grunt.userevvd.summary file would look like:

{
  "dist/assets/js/main.js": "https://cdn.domain.com/assets/js/main.k8dj3h45.js"
}

Or, if you omitted the formatPath option:

{
  "dist/assets/js/main.js": "dist/assets/js/main.k8dj3h45.js"
}

In other words, if you don't use the formatPath option, grunt.userevvd.summary is exactly the same as grunt.filerev.summary.

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

(Nothing yet)

grunt-userevvd's People

Contributors

kylerush avatar

Watchers

Jessica Ross McKinnie avatar Pete Koomen avatar Jennifer Adventure Lin avatar Marco Sgrignuoli avatar Rama Ranganath avatar Logan Linn avatar Ron Severdia avatar Jordan Garcia avatar Yeeland Chen avatar Kyle Randolph avatar Matt Carroll avatar Nick DiRienzo avatar Suneet Shah avatar James Cloos avatar  avatar Natarajan Chakrapani avatar  avatar Adam Tong avatar Sonesh Surana avatar Matt Auerbach avatar Tommy Giglio avatar Waylon Janowiak avatar Adam Panzer avatar Joe avatar Trent Robbins avatar Ozgur Dogan Ugurlu avatar Jon Noronha avatar Matt Moravec avatar Byron Jones avatar Stewart Smith avatar Ejede avatar Jon Gaulding avatar Jamie Connolly avatar HASIBA CHOUDHURY avatar Adrian Gregory avatar Bryce avatar Lucas Swartsenburg avatar Cyrus Dorosti avatar Becca Bruggman avatar David Fox-Powell avatar Ben McKown avatar Jon avatar Esha Verma avatar Ayesha Mazrana avatar Quinton Dang avatar Dana Yakoobinsky avatar Spencer Smitherman avatar Leo Pekelis avatar Darcey Lachtman avatar Darwish Gani avatar Pamela Ongchin avatar Ajith Mascarenhas avatar John Provine avatar Christopher Dee avatar Ian Thiel avatar Nate Goldman avatar Christopher Tiutan avatar Whelan Boyd avatar Teja Shah avatar Neha Saigal avatar David Walsh avatar Fergal Reid avatar Brendan O'Rourke avatar Anuraj Pandey avatar Michael Wei avatar  avatar Michael Sample avatar linda hong avatar Michael Twardos avatar  avatar Karen Loughrey avatar Ian Hinsdale avatar Jonathan Traupman avatar Eduardo Rivera avatar Erika Palmer avatar Jimmy Jin avatar Amy Chiu avatar  avatar Takeshi Young avatar Bill Caplan avatar Daina Lightfoot avatar James Chen avatar Michael avatar Andreas Bloomquist avatar Hadjer Yala avatar Shashank Paliwal avatar Johanna avatar Zach Power avatar Jack Kelly avatar  avatar Alda Luong avatar Ifi Akpandak avatar Aamir Farooq avatar Dmitriy Dvoryanchuk avatar Aamir Farooq avatar Allison Rodriguez avatar Will K avatar  avatar  avatar  avatar

Forkers

isabella232

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.